SchemaOrg (SchemaOrg v0.1.0)

Copy Markdown View Source

Strictly-typed builder for Schema.org JSON-LD.

Each Schema.org Class is a generated struct module under SchemaOrg.* (e.g. SchemaOrg.Product, SchemaOrg.Offer). Build a graph with ordinary struct literals — your editor auto-completes the valid fields and the compiler rejects the rest — then serialise it with to_json_ld/1.

%SchemaOrg.Product{
  name: "MacBook Pro",
  offers: %SchemaOrg.Offer{price: 1999.0}
}
|> SchemaOrg.to_map()
#=> %{
#     "@type" => "Product",
#     "name" => "MacBook Pro",
#     "offers" => %{"@type" => "Offer", "price" => 1999.0}
#   }

A property field is untyped, so it accepts Schema.org's loose value model directly: a scalar or a nested struct (brand: "Apple" or brand: %SchemaOrg.Brand{}), and a single value or a list (offers: %SchemaOrg.Offer{} or offers: [%SchemaOrg.Offer{}, ...]).

Summary

Functions

Returns true if module is a generated SchemaOrg type module.

Serialises a generated SchemaOrg struct into a JSON-LD string.

Like to_json_ld/1 but returns the bare JSON-LD map (no @context, not encoded).

Functions

schema_struct?(module)

@spec schema_struct?(module()) :: boolean()

Returns true if module is a generated SchemaOrg type module.

to_json_ld(thing)

@spec to_json_ld(struct()) :: String.t()

Serialises a generated SchemaOrg struct into a JSON-LD string.

Adds the top-level @context, recurses into nested structs, drops unset (nil) properties, and re-keys each field to its Schema.org camelCase name.

to_map(thing)

@spec to_map(struct()) :: map()

Like to_json_ld/1 but returns the bare JSON-LD map (no @context, not encoded).

Useful for embedding inside a larger document or for assertions in tests.