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

    Module index

    Linked data validation API.

    Provides a type-safe DSL for defining resource shapes with validation constraints based on the Shapes Constraint Language (SHACL). Shapes define both the expected structure and validation rules for resources, enabling compile-time type inference and runtime validation.

    Shape Types

    ValueShape is a discriminated union of all concrete shape types:

    Defining Shapes

    Define resource shapes with property constraints and value ranges:

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

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

    Validating Resources

    Validate values against shapes using pattern matching on the Relay result:

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

    const name = validate(data, Product)({
    value: product => product.name
    }); // undefined if validation fails

    Validation Modes

    Beyond complete resource states, validate supports partial updates, projections, and queries:

    // validate a complete resource state (default)
    validate(data, Product);

    // validate a partial update (null values = deletions)
    validate(data, Product, { mode: "patch" });

    // validate a projection model
    validate(data, Product, { mode: "model" });

    // validate a query with filtering and pagination
    validate(data, Product, { mode: "query" });

    Custom Validators

    Implement custom resource-level constraints using Validator functions. Use collect to merge multiple facets into a single canonical trace:

    import { collect } from '@metreeca/blue';
    import type { Trace } from '@metreeca/blue';

    interface Product { minPrice?: number; maxPrice?: number; startDate?: string; endDate?: string }

    function checkProduct(value: Product): Trace {
    return collect([
    value.minPrice !== undefined && value.maxPrice !== undefined
    && value.minPrice > value.maxPrice
    ? ["minPrice must not exceed maxPrice"]
    : [],
    value.startDate !== undefined && value.endDate !== undefined
    && value.startDate > value.endDate
    ? ["startDate must not follow endDate"]
    : []
    ]);
    }

    const Product = resource({ validators: [checkProduct] }, {
    minPrice: optional(integer()),
    maxPrice: optional(integer()),
    startDate: optional(date()),
    endDate: optional(date())
    });

    Type Aliases

    Trace

    Validation trace.

    Validator

    Value validator.

    ValueShape

    Discriminated union of all concrete shape types for validating individual node values.

    Infer

    Infers the model type from a Lazy shape.

    Guards

    isTrace

    Checks whether a value is a valid Trace.

    isValidator

    Checks whether a value is a valid Validator.

    isValueShape

    Checks whether a value is a valid ValueShape.

    Functions

    collect

    Collects multiple validation traces into a single canonical trace.

    validate

    Validates a value against a shape.