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

    Function validate

    Validates resources and templates against shapes.

    • Validates a resource against a shape.

      Enforces all shape constraints including type, cardinality, closed-shape checks, and custom validators. Unknown and missing entries are both rejected; all declared entries are required unless marked optional by the shape. The return value is narrowed to Instance<T> where T is the projection Template bonded to the shape's model slot.

      Caution

      By default, resources accept captive reference expansion to unbounded depth. To enforce a strict update process that admits only bare references, set depth to 0 to reject all expansion; set it to a positive value to cap the nesting depth admitted.

      Tip

      When the projection template is not bonded to the shape (for example, at API boundaries where shape and the requested projection arrive as independent inputs) use the projection-form overload that takes model as a separate argument.

      Tip

      The function is idempotent on a specific shape: on re-validation against the same shape, the previous association is trusted without repeating the validation process, so that you can safely re-validate defensively.

      Type Parameters

      • T extends Template

        The projection Template inferred from the shape's bonded model slot

      Parameters

      • value: unknown

        The value to validate as a resource

      • opts: {
            shape: Lazy<ResourceShape & { model: T }>;
            model?: false;
            entry?: string;
            depth?: number;
        }

        Validation options

        • Readonlyshape: Lazy<ResourceShape & { model: T }>

          The Lazy ResourceShape defining validation constraints

        • Optional Readonlymodel?: false

          Omit (or pass false) to validate value as a full resource instance against the shape

        • Optional Readonlyentry?: string

          Expected Reference for the resource's id entry; if provided and the resource contains an id property, the id value must match this reference exactly; ignored if the resource has no id entry

        • Optional Readonlydepth?: number

          Maximum nesting depth for expanding captive reference values as inline target resource states; each expansion level counts against the budget; 0 rejects all expansion, accepting bare IRI references only; if omitted, no depth limit is enforced

      Returns Relay<{ value: Instance<T>; trace: Trace | undefined }>

      A Relay resolving to either { value } on success or { trace } on failure; on success, the value is an immutable copy validated against a verified and flattened copy of the shape (see resource); on failure, the trace describes constraint violations

      If the shape is malformed (see resource)

    • Validates a retrieval result against a shape under an explicit projection template.

      Narrows the admissibility check to the surface projected by model: constraints declared in shape are enforced only for keys named in model, recursing into nested shapes for nested templates and dropping any shape leaf without a counterpart in model. Intended for call sites that receive shape and the requested projection as independent inputs, such as response-validating adapters or API boundary validators where the admissible surface is fixed and the projection varies per request.

      Differs from the bonded-shape resource overload in three ways:

      • Partial resources — constraints on keys absent from model are not enforced; unrequested required fields do not trigger minCount violations.
      • Expanded nested references — slots of ReferenceShape kind accept an expanded nested resource in addition to a bare Reference, validated against the linked resource's target shape narrowed by the nested projection in model.
      • Projection results — the return value is narrowed to Instance<T> where T is inferred from model.
      Tip

      The function is idempotent on a specific (shape, model) combination: on re-validation against the same shape and model the previous association is trusted without repeating the validation process, so that you can safely re-validate defensively.

      Type Parameters

      Parameters

      • value: unknown

        The value to validate as a retrieval result

      • opts: { shape: Lazy<ResourceShape>; model: T; entry?: string }

        Validation options

        • Readonlyshape: Lazy<ResourceShape>

          The Lazy ResourceShape defining the admissible surface

        • Readonlymodel: T

          Projection Template narrowing the admissibility check to the projected surface and the return value to Instance<T>

        • Optional Readonlyentry?: string

          Expected Reference for the resource's id entry; if provided and the resource contains an id property, the id value must match this reference exactly; ignored if the resource has no id entry

      Returns Relay<{ value: Instance<T>; trace: Trace | undefined }>

      A Relay resolving to either { value } on success or { trace } on failure; on success, the value is an immutable copy validated against a verified and flattened copy of the shape narrowed by model (see resource); on failure, the trace describes constraint violations

      If the shape is malformed (see resource)

    • Validates a template against a shape.

      Enforces type and structural constraints; value constraints are skipped as query values are placeholders. Cardinality is checked for shape consistency (scalar if maxCount is 1, singleton tuple otherwise); missing entries are accepted as not requested. Bindings whose probe — path and pipe — fails to resolve against the shape are rejected with an atomic trace under the binding key.

      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.

      Tip

      Retrieval forms for linked resources differ by shape kind:

      Shape kind IRI reference Nested template
      Embedded resource — direct ResourceShape
      Standalone resource — ReferenceShape wrapper

      An IRI reference is a bare IRI reference placeholder retrieving only the identifier; as a placeholder it is never resolved on decoding, so it admits any IRI reference (the empty string, a root-relative or relative reference, or an absolute IRI). A nested template is a Template retrieving the requested subset of the linked resource, validated against its target shape and subject to the depth budget (if any). Setting depth to 0 disables the nested-template form for references while still accepting IRI references.

      Tip

      The function is idempotent on a specific shape: on re-validation against the same shape, the previous association is trusted without repeating the validation process, so that you can safely re-validate defensively.

      Type Parameters

      Parameters

      • value: unknown

        The value to validate as a template

      • opts: {
            shape: Lazy<ResourceShape>;
            model: true;
            plain?: boolean;
            depth?: number;
            limit?: number;
        }

        Validation options

        • Readonlyshape: Lazy<ResourceShape>

          The Lazy ResourceShape defining the expected structure

        • Readonlymodel: true

          Must be true to validate value as a retrieval template rather than as an instance

        • Optional Readonlyplain?: boolean

          Whether to reject aggregate transforms (count, sum, min, max, avg); true rejects any binding containing aggregate transforms; defaults to false

        • Optional Readonlydepth?: number

          Maximum depth for nested Template expansion and property paths in query probes, where each nesting level or path segment counts against the budget; 0 rejects any nested Template while still accepting IRI references; if omitted, no depth constraint is enforced

        • Optional Readonlylimit?: number

          Maximum value for the # pagination constraint in queries; a positive value caps the result set: a query whose # exceeds it, or is 0 (unbounded), is rejected, and a query omitting # has the limit injected as a default; a value of 0, like omitting the option, is itself unbounded, enforcing no limit and injecting no #

      Returns Relay<{ value: T; trace: Trace | undefined }>

      A Relay resolving to either { value } on success or { trace } on failure; on success, the value is an immutable copy validated against a verified and flattened copy of the shape (see resource); on failure, the trace describes constraint violations

      If the shape is malformed (see resource)