Skip to content

ephemeris ​

@c9up/ephemeris: schema.org structured data: the whole vocabulary typed, a page's entities linked into one @graph, JSON-LD that cannot break out of its script tag, and Google's rich-result requirements checked as each node is built. Guide: Structured data.

Installation ​

bash
pnpm add @c9up/ephemeris

ephemeris has no provider, no configuration and no dependency at run time.

Entry points ​

ImportContents
@c9up/ephemerisSchemaGraph, the node helpers, their rules, serialization, errors, the Schema type namespace
@c9up/ephemeris/schemaThe schema.org vocabulary (release 30.1) as TypeScript types, one per type

Exports ​

@c9up/ephemeris ​

ExportKindDescription
SchemaGraphclassOne page's nodes, linked by generated @ids, written as a single @graph.
toJsonLdfunctiontoJsonLd(data: object): JsonLdValue: plain data, undefined left out, dates as ISO strings; anything JSON cannot hold is refused.
stringifyJsonLdfunctionstringifyJsonLd(data: object): string: the JSON text with <, >, &, U+2028 and U+2029 escaped.
jsonLdScriptTagfunctionjsonLdScriptTag(data: object, options?: ScriptTagOptions): string: <script type="application/ld+json"> for <head>, with the request's CSP nonce when given.
errorsnamespaceThe package's errors, one class per code, see Errors. errors.E_EPHEMERIS_MISSING_REQUIRED carries type, property and source (the Search Central page).
MAX_TEXT_LENGTHconst10000: the longest text value accepted.
MAX_BREADCRUMB_ITEMSconst32.
Schematype namespace@c9up/ephemeris/schema, as Schema.Product, Schema.Article…
SchemaGraphOptionsinterfaceCheckOptions & { url: string; site?: string }.
CheckOptionsinterface{ onWarning?: WarningHandler }. Without it, each kind of warning is written once per process with console.warn; () => {} silences them.
WarningHandlertype(warning: RequirementWarning) => void.
RequirementWarninginterface{ code; type; property; message; source }. code is W_EPHEMERIS_MISSING_RECOMMENDED, W_EPHEMERIS_LOW_PRECISION, W_EPHEMERIS_TOO_LONG_FOR_GOOGLE or W_EPHEMERIS_NO_TIMEZONE.
Ruleinterface{ type; source; read; required?; recommended?; checks? }: what a helper checks, with its Search Central page and the date it was read.
Requirementtypestring | readonly [string, ...string[]]: a property, dotted for a nested one, or alternatives.
Crumbinterface{ name: string; item?: string }.
JsonLdValuetypeWhat JSON.parse would give back.
ScriptTagOptionsinterface{ nonce?: string }.

Node helpers ​

Each helper checks a node and returns it unchanged, typed as given: helper<T>(node: T, options?: CheckOptions): T. A missing required property throws errors.E_EPHEMERIS_MISSING_REQUIRED; a missing recommended one is a warning. Every helper also bounds the node (depth, text length, URLs, dates, prices, currencies).

Helperschema.org typeRule
organizationOrganizationORGANIZATION
localBusinessLocalBusinessLOCAL_BUSINESS
personPerson (bounded only; person(node) takes no options)—
websiteWebSiteWEBSITE
webPageWebPageWEB_PAGE
profilePageProfilePagePROFILE_PAGE
breadcrumbBreadcrumbList, built from crumbs: readonly Crumb[], two to 32 itemsBREADCRUMB
articleArticle and its subtypesARTICLE
productProduct (product snippet; offers, reviews and rating checked)PRODUCT
merchantListingProduct sold by the pageMERCHANT_LISTING
reviewReviewREVIEW
aggregateRatingAggregateRatingAGGREGATE_RATING
eventEventEVENT
jobPostingJobPostingJOB_POSTING
recipeRecipeRECIPE
videoVideoObjectVIDEO
softwareApplicationSoftwareApplicationSOFTWARE_APPLICATION
courseCourseCOURSE
itemListItemListITEM_LIST
datasetDatasetDATASET
discussionForumPostingDiscussionForumPosting (comments checked at every depth)DISCUSSION_FORUM_POSTING
quizQuizQUIZ
employerAggregateRatingEmployerAggregateRatingEMPLOYER_AGGREGATE_RATING
imageObjectImageObject with license metadataIMAGE_LICENSE
memberProgramMemberProgramMEMBER_PROGRAM
mathSolverMathSolverMATH_SOLVER
movieMovieMOVIE
qaPageQAPage (question and answers checked)QA_PAGE
merchantReturnPolicyMerchantReturnPolicyMERCHANT_RETURN_POLICY
shippingServiceShippingServiceSHIPPING_SERVICE
vacationRentalVacationRentalVACATION_RENTAL
productGroupProductGroupPRODUCT_GROUP

The rules applied to nested nodes are exported too: NESTED_REVIEW, NESTED_AGGREGATE_RATING, OFFER, AGGREGATE_OFFER, MERCHANT_OFFER, OFFER_SHIPPING_DETAILS, COMMENT, QUESTION, ANSWER, SPEAKABLE.

SchemaGraph ​

new SchemaGraph(options: SchemaGraphOptions): url is the page's absolute URL; site, the site's home, defaults to its origin.

MemberSignatureDescription
identityidentity(node: Organization | Person): thisThe organization or person behind the site, <site>#identity.
websitewebsite(node: WebSite): this<site>#website, published by the identity.
webPagewebPage(node: WebPage): this<url>#webpage, part of the website.
breadcrumbbreadcrumb(node: BreadcrumbList): this<url>#breadcrumb.
mainEntitymainEntity(node: Node): thisWhat the page is about, <url>#main-entity.
addadd(node: Node, options?: { id?: string | number }): thisAny other node; it keeps its own @id, or gets <url>#/schema/<type>/<id>.
toJsonLdtoJsonLd(): { '@context': 'https://schema.org'; '@graph': JsonLdValue[] }The graph, root nodes first, ready for jsonLdScriptTag.

Errors ​

CodeRaised when
E_EPHEMERIS_INVALID_NODEA node has no @type.
E_EPHEMERIS_INVALID_URLA URL is not absolute.
E_EPHEMERIS_INVALID_DATEA date is not ISO 8601.
E_EPHEMERIS_INVALID_VALUEA value cannot be serialized.
E_EPHEMERIS_INVALID_PRICEA price is neither a number nor digits with a decimal point.
E_EPHEMERIS_INVALID_CURRENCYA currency is not an ISO 4217 code.
E_EPHEMERIS_MISSING_REQUIREDA node lacks a property Google requires for its rich result.
E_EPHEMERIS_UNSAFE_KEYA key such as __proto__.
E_EPHEMERIS_ID_TAKENAn @id the graph generated for one node was given to another.
E_EPHEMERIS_SLOT_TAKENA graph slot (identity, website…) is already taken.
E_EPHEMERIS_CYCLEA node contains itself.
E_EPHEMERIS_TOO_DEEPThe data is nested deeper than 64 levels.
E_EPHEMERIS_TOO_LONGA text value is longer than MAX_TEXT_LENGTH.
E_EPHEMERIS_TOO_MANYA list, such as a breadcrumb, has more items than allowed.

Migration ​

  • EphemerisError is gone: test error.code, or error instanceof errors.E_EPHEMERIS_* with import { errors } from '@c9up/ephemeris'.
  • RequirementError becomes errors.E_EPHEMERIS_MISSING_REQUIRED, with the same type, property and source.

See also ​