Aller au contenu

Service builder

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

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

import {
ServiceSchema,
type SchemaInput,
type SchemaOutput,
} from '@unschema-graph/core';
type ServiceInput = SchemaInput<typeof ServiceSchema>;
type ServiceOutput = SchemaOutput<typeof ServiceSchema, 'Service'>;
Propriété Type d’entrée Obligatoire Valeur par défaut / contraintes
@id string Non non-empty
name string Oui non-empty
provider string | Person | Organization | LocalBusiness | EntityReference Non non-empty
serviceType string Non —
description string Non —
areaServed string | Array | object Non —
offers Offer | AggregateOffer | Array<Offer | AggregateOffer> Non —
aggregateRating AggregateRating Non —
review Review | Array<Review> Non —
termsOfService string 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 Service.

import { Service } from '@unschema-graph/core';
const entity = Service({
"name": "Astro consulting",
"provider": "Acme"
});
{
"@type": "Service",
"name": "Astro consulting",
"provider": {
"@type": "Organization",
"name": "Acme"
}
}
  • 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 Service.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.