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

    Module index

    Linked data validation API.

    Provides validation for linked data resources, retrieval templates, and individual values against SHACL-derived shapes.

    Defining Shapes

    Define resource shapes with property constraints and cardinality ranges from the value module:

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

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

    Validating Resources

    Validate resources using validate, pattern matching on the Relay result:

    import { validate } from '@metreeca/blue';

    validate(data, { shape: Product })({
    value: product => console.log(product.name),
    trace: trace => console.error(trace)
    });

    Validating Projections

    When the projection Template is not bonded to the shape (typically at API boundaries where shape defines the admissible surface and the projection arrives per request), pass model as a separate argument. The return value is narrowed to Instance<T> where T is inferred from model:

    const model = { id: "", name: "" };       // projection requested by the caller

    validate(response, { shape: Product, model })({
    value: product => console.log(product.name), // typed as { readonly id: Reference; readonly name: string }
    trace: trace => console.error(trace)
    });

    Validating Templates

    Validate a retrieval template using validate with model: true:

    validate(data, { shape: Product, model: true });
    validate(data, { shape: Product, model: true, plain: true });
    validate(data, { shape: Product, model: true, depth: 0 });
    validate(data, { shape: Product, model: true, limit: 100 });

    The model option answers "are we validating a model, or validating against one?" and selects between the three modes shown above:

    • omitted or false — validate value as an instance against the shape's bonded model
    • a projection Template value — validate value as an instance against that explicit projection; narrows the return to Instance<T> where T is inferred from model
    • true — validate value as a retrieval template (the model itself, not an instance of it)
    Caution

    By default, templates support the full query language, including aggregate transforms and nested expansion. When exposing endpoints to untrusted clients, restrict query complexity as required by setting plain to true, depth to 0 or a positive value, and/or limit to a maximum result set size.

    Variables

    sh

    SHACL vocabulary namespace.

    Functions

    validate

    Validates resources and templates against shapes.