Aller au contenu

Recipe builder

Le builder Recipe crée une entité Recipe, injecte son @type, valide les données de manière synchrone et rejette les propriétés inconnues.

// Astro — shown first when Astro is selected
import { Recipe } from '@unschema-graph/astro';
// Svelte 5
import { Recipe } from '@unschema-graph/svelte';
// Core / Node.js
import { Recipe } from '@unschema-graph/core';
import { RecipeSchema } from '@unschema-graph/core';

Le schéma Zod RecipeSchema est également exporté pour la composition et la validation avancées.

import {
RecipeSchema,
type SchemaInput,
type SchemaOutput,
} from '@unschema-graph/core';
type RecipeInput = SchemaInput<typeof RecipeSchema>;
type RecipeOutput = SchemaOutput<typeof RecipeSchema, 'Recipe'>;
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> Oui non-empty
recipeIngredient Array Oui minimum items: 1
recipeInstructions Array<string | object> | string Oui —
author string | Person | Organization | EntityReference Non non-empty
datePublished string | number | Date Non non-empty
description string Non —
prepTime string | number | DurationObject Non non-empty; greater than 0
cookTime string | number | DurationObject Non non-empty; greater than 0
totalTime string | number | DurationObject Non non-empty; greater than 0
recipeYield string | number Non —
recipeCategory string | Array Non —
recipeCuisine string | Array Non —
keywords string | Array Non —
nutrition object Non —
aggregateRating AggregateRating Non —

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 Recipe.

import { Recipe } from '@unschema-graph/core';
const entity = Recipe({
"name": "Tomato pasta",
"image": "/images/pasta.jpg",
"recipeIngredient": [
"200 g pasta",
"2 tomatoes"
],
"recipeInstructions": [
"Cook the pasta.",
"Add the tomatoes."
]
});
{
"@type": "Recipe",
"name": "Tomato pasta",
"image": "/images/pasta.jpg",
"recipeIngredient": [
"200 g pasta",
"2 tomatoes"
],
"recipeInstructions": [
{
"@type": "HowToStep",
"text": "Cook the pasta."
},
{
"@type": "HowToStep",
"text": "Add the tomatoes."
}
]
}
  • 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.

Utilisez Recipe.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.