Product builder
Le builder Product crée une entité Product, injecte son @type,
valide les données de manière synchrone et rejette les propriétés inconnues.
// Astro — shown first when Astro is selectedimport { Product } from '@unschema-graph/astro';
// Svelte 5import { Product } from '@unschema-graph/svelte';
// Core / Node.jsimport { Product } from '@unschema-graph/core';import { ProductSchema } from '@unschema-graph/core';Le schéma Zod ProductSchema est également exporté pour la composition et la validation avancées.
Types TypeScript
Section intitulée « Types TypeScript »import { ProductSchema, type SchemaInput, type SchemaOutput,} from '@unschema-graph/core';
type ProductInput = SchemaInput<typeof ProductSchema>;type ProductOutput = SchemaOutput<typeof ProductSchema, 'Product'>;Propriétés d’entrée
Section intitulée « Propriétés d’entrée »| Propriété | Type d’entrée | Obligatoire | Valeur par défaut / contraintes |
|---|---|---|---|
@id |
string | Non | non-empty |
name |
string | Oui | non-empty |
image |
string | ImageObject | Array<string | ImageObject> | Non | non-empty |
description |
string | Non | — |
brand |
string | Brand | Organization | EntityReference | Non | non-empty |
offers |
Offer | AggregateOffer | Array<Offer | AggregateOffer> | Non | — |
aggregateRating |
AggregateRating | Non | — |
review |
Review | Array<Review> | Non | — |
sku |
string | Non | — |
gtin |
string | Non | — |
gtin8 |
string | Non | — |
gtin13 |
string | Non | — |
gtin14 |
string | Non | — |
mpn |
string | Non | — |
category |
string | Non | — |
color |
string | Non | — |
material |
string | Non | — |
releaseDate |
string | number | Non | non-empty |
Les alias ci-dessus restent la référence exacte, notamment pour les objets imbriqués. Le builder
accepte aussi une configuration de validation en second argument et possède une sortie dont le
@type vaut toujours Product.
Exemple minimal
Section intitulée « Exemple minimal »import { Product } from '@unschema-graph/core';
const entity = Product({ "name": "Mechanical keyboard", "sku": "KB-001", "brand": "Acme"});{ "@type": "Product", "name": "Mechanical keyboard", "brand": { "@type": "Brand", "name": "Acme" }, "sku": "KB-001"}Relations et recettes
Section intitulée « Relations et recettes »- Builders liés :
Offer,AggregateOffer,Service,JobPosting - Utilisé par : E-commerce
- Sources externes : Schema.org Product · Google Search Central
Erreurs fréquentes
Section intitulée « Erreurs fréquentes »- Passer une propriété inconnue au builder strict.
- Utiliser une donnée source privée d’une propriété obligatoire.
- Supposer qu’un Schema.org valide garantit un affichage dans la recherche.
Validation
Section intitulée « Validation »Utilisez Product.safeParse(input) pour les données externes. Pour une extension
Schema.org non encore modélisée, validez d’abord l’entité puis utilisez
withAdditionalProperties(). N’ajoutez jamais une propriété inventée au builder.