@metreeca/blue - v0.9.1
    Preparing search index...

    Module resource

    Resource shape model and factories.

    Provides shapes for validating linked data resources with property definitions, cardinality constraints, and inheritance. Resource shapes define the expected structure of linked data resources using a SHACL-based model with compile-time type inference.

    Warning

    Factories check structural integrity of constraints but not their logical consistency: contradictory constraints like minCount > maxCount won't be rejected, nor will inconsistencies with inherited definitions.

    Important

    Resource shapes are closed: validated resources may only contain properties explicitly defined in the shape. Any additional properties will cause validation to fail.

    Important

    All IRI values in validated resources must be absolute. When decoding client input, relative references may be auto‑resolved using the base option in decodeResource, decodePatch, or decodeQuery.

    Defining Resource Shapes

    Combine property definitions with value ranges to define resource structures:

    import { resource, id, required, optional, repeatable } from '@metreeca/blue';
    import { string, integer, boolean, reference } from '@metreeca/blue';

    const Product = resource({
    id: id(),
    name: required(string({ minLength: 1 })),
    price: required(integer({ minInclusive: 0 })),
    available: optional(boolean()),
    tags: repeatable(string())
    });

    Properties and Ranges

    Ranges define cardinality constraints for property values:

    import { resource, property, required, optional, multiple, repeatable, cardinality } from '@metreeca/blue';
    import { string } from '@metreeca/blue';

    const Shape = resource({
    name: required(string()), // 1..1
    alias: optional(string()), // 0..1
    tags: repeatable(string()), // 1..*
    notes: multiple(string()), // 0..*
    codes: cardinality(2, 5)(string()) // 2..5
    });

    Naked ranges are automatically wrapped in a property; use explicit property when IRI mappings or labels are needed:

    import { resource, property, required, string } from '@metreeca/blue';
    import { createNamespace } from '@metreeca/core/resource';

    const schema = createNamespace("http://schema.org/");

    const Person = resource({
    name: property({ forward: schema }, required(string()))
    });

    Resource References and Embedding

    Resource properties link to other resources in two ways. A reference wrapper links to a standalone resource — an independently identified and managed entity. A direct shape inclusion defines an embedded resource — a nested object with no independent identity, created and managed together with its parent.

    Note

    In state and patch validation, embedded resources are always validated as complete states — patch semantics (missing properties accepted, custom validators skipped) apply only at the top level.

    import { resource, id, required, optional, reference } from '@metreeca/blue';
    import { string, number } from '@metreeca/blue';

    const Rating = resource({
    average: required(number({ minInclusive: 0, maxInclusive: 5 })),
    reviews: required(number({ minInclusive: 0 }))
    });

    const Vendor = resource({
    id: id(),
    name: required(string())
    });

    const Product = resource({
    id: id(),
    name: required(string()),
    rating: optional(Rating), // embedded
    vendor: required(reference(Vendor)) // standalone
    });

    Use backlink for reverse links managed by the target resource. Backlinks are read-only from the source resource perspective: included in responses but rejected in state updates and patches.

    Self-referential shapes use lazy factories:

    function Category() {
    return resource({
    id: id(),
    name: required(string()),
    parent: optional(reference(Category))
    });
    }

    Inheritance

    Extend parent shapes to inherit properties and constraints:

    import { resource, id, required, string, integer, reference } from '@metreeca/blue';

    const NamedEntity = resource({
    id: id(),
    name: required(string({ minLength: 1 }))
    });

    const Employee = resource({

    extends: NamedEntity

    }, {
    department: required(string()),
    salary: required(integer({ minInclusive: 0 }))
    });

    Polymorphic Properties

    Use union for properties accepting multiple value types. Unions are pure type discriminators — cardinality constraints belong on the enclosing Range, not on individual variants. At runtime, union values are represented as Indexed records mapping variant names to their values, corresponding to JSON-LD indexed containers (@container: @index):

    import { resource, property, union, optional, reference, string } from '@metreeca/blue';

    const PostalAddress = resource({
    street: required(string()),
    city: required(string())
    });

    const Contact = resource({
    address: optional(union({
    text: string(),
    PostalAddress: reference(PostalAddress)
    }))
    });

    Variant keys act as discriminators in runtime values:

    {
    "address": {
    "text": "123 Main St"
    }
    }

    {
    "address": {
    "PostalAddress": {
    "id": "https://data.example.com/addresses/456",
    "streetAddress": "12 Harbour Street",
    "addressLocality": "Copenhagen"
    }
    }
    }

    Variables

    defaultNamespace

    Default application namespace for property IRI resolution (app:/#).

    Type Aliases

    Entries

    Property definitions for a ResourceShape.

    Entry

    A property definition entry.

    Overrides

    Checks that child property overrides are assignable to inherited types.

    Inheritance

    Extracts inherited model types from ResourceConstraints.extends.

    DeclaredProperties

    Extracts explicitly declared properties, stripping index signatures.

    Intersection

    Converts a union type to an intersection type.

    Composition

    Builds a resource type from Entries.

    Projection

    Strips the =expression suffix from a property key.

    Content

    Extracts the content type from an Entry.

    RequiredKeys

    Extracts keys of required properties from Entries.

    OptionalKeys

    Extracts keys of optional properties from Entries.

    Cardinality

    Maps SHACL cardinality constraints to TypeScript types.

    Interfaces

    ReferenceShape

    Shape definition for resource references.

    ResourceShape

    Shape definition for linked data resources.

    ResourceConstraints

    Constraints for resource shape factories.

    Id

    Marker interface for the resource identifier property.

    Type

    Marker interface for the resource type property.

    Property

    Shape definition for a resource property.

    PropertyConstraints

    Constraints for property shape factories.

    Range

    Shape for a set of values linked from a resource by a property.

    Union

    Discriminated type alternatives for polymorphic property values.

    Guards

    isReferenceShape

    Checks whether a value is a ReferenceShape.

    isResourceShape

    Checks whether a value is a valid ResourceShape.

    isResourceConstraints

    Checks whether a value is a valid ResourceConstraints object.

    isId

    Checks whether a value is a valid Id.

    isType

    Checks whether a value is a valid Type.

    isProperty

    Checks whether a value is a valid Property.

    isPropertyConstraints

    Checks whether a value is a valid PropertyConstraints object.

    isRange

    Checks whether a value is a valid Range.

    isUnion

    Checks whether a value is a valid Union.

    isEntries

    Checks whether a value is a valid Entries object.

    isEntry

    Checks whether a value is a valid Entry.

    Factories

    reference

    Creates a reference shape for the given target resource shape.

    Creates a backlink reference shape for the given target resource shape.

    resource

    Creates resource shapes.

    id

    Creates a property shape for the resource identifier.

    type

    Creates a property shape for the resource type.

    property

    Creates property shapes.

    union

    Creates a union of named value shapes.

    multiple

    Creates a value range with no cardinality constraints (0..*).

    repeatable

    Creates a value range requiring at least one value (1..*).

    optional

    Creates a value range for at most one value (0..1).

    required

    Creates a value range for exactly one value (1..1).

    cardinality

    Creates a value range factory with custom cardinality constraints.