Aller au contenu

Book builder

Le builder Book crée une entité Book, 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 { Book } from '@unschema-graph/astro';
// Svelte 5
import { Book } from '@unschema-graph/svelte';
// Core / Node.js
import { Book } from '@unschema-graph/core';
import { BookSchema } from '@unschema-graph/core';

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

import {
BookSchema,
type SchemaInput,
type SchemaOutput,
} from '@unschema-graph/core';
type BookInput = SchemaInput<typeof BookSchema>;
type BookOutput = SchemaOutput<typeof BookSchema, 'Book'>;
Propriété Type d’entrée Obligatoire Valeur par défaut / contraintes
@id string Non non-empty
name string Oui non-empty
author string | Person | Organization | EntityReference | Array<string | Person | Organization | EntityReference> Oui —
isbn string Non —
bookFormat string Non —
datePublished string | number | Date Non non-empty
publisher string | Organization | EntityReference Non —
inLanguage string Non —
numberOfPages number Non integer; greater than 0; maximum: 9007199254740991
description string Non —
aggregateRating AggregateRating Non —
review Review | Array<Review> 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 Book.

import { Book } from '@unschema-graph/core';
const entity = Book({
"name": "The Astro Handbook",
"author": "Ada Lovelace"
});
{
"@type": "Book",
"name": "The Astro Handbook",
"author": "Ada Lovelace"
}
  • 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 Book.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.