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

    Function validate

    Validates a value against a shape.

    • Validates a resource against a shape.

      Reports what a resource states wrongly, so that a caller may refuse it before it reaches the store holding it, and hands back the resource itself where it passes, typed as the shape describes it. Every member the shape declares is held to it and one it doesn't declare is rejected, the shape being closed; a member the shape admits no value for may be left out.

      Caution

      A captive member is expanded to whatever depth it reaches. Where a caller must be held to bare links, state depth as 0 to refuse every expansion, or as the nesting it may ask for.

      Tip

      Revalidating a value costs nothing where the second call asks for no more than the first: the earlier verdict is read off the value itself, so a caller may validate defensively wherever it is unsure.

      Type Parameters

      Parameters

      • value: unknown

        The value to validate as a resource

      • opts: { shape: S; model?: false; entry?: string; depth?: number }

        Validation options

        • Readonlyshape: S

          The shape the resource is matched against, possibly deferred to break definition cycles

        • Optional Readonlymodel?: false

          Omitted, or false, to validate value as a resource in its own right

        • Optional Readonlyentry?: string

          The identifier the resource is expected to be named by, where it states one at all

        • Optional Readonlydepth?: number

          The nesting a captive member may be expanded to, counting each resource it descends into; 0 refuses every expansion while still admitting the identifier naming the resource

      Returns Relay<{ value: State<S>; trace: Optional<Trace> }>

      A Relay carrying the resource where it passes and a trace of the violations where it doesn't

      TraceError Where shape is malformed

    • Validates a retrieved resource against a shape, narrowed by the template that requested it.

      Reports what a retrieval brought back wrongly, so that a caller may refuse a malformed response before reading it, and hands back the resource typed as the template asked for it. Only the members the template names are held to the shape, so a partial retrieval passes on its own terms, and a member the template didn't ask for is rejected, the caller having nowhere to put it. Reach for this where the shape fixes what may be asked for and the template varies from one request to the next.

      Important

      The custom checks a shape states never run against a retrieved resource, even one the template asked for whole: they run only where a whole resource is validated in its own right.

      Note

      The template states which values are wanted and no longer what they are, so every type comes from the shape: the resource comes back as Match resolves it, keyed down to the members the template named. A polymorphic member, a projection column and a localised member come back as wide as the shape describes them.

      Tip

      Revalidating a value against the same shape and template costs nothing, as validating a resource explains.

      Type Parameters

      • S extends Lazy<ResourceShape>

        The shape the resource is matched against

      • M extends Template

        The template the resource was requested by

      Parameters

      • value: unknown

        The value to validate as a retrieved resource

      • opts: { shape: S; model: M; entry?: string }

        Validation options

        • Readonlyshape: S

          The shape the resource is matched against, possibly deferred to break definition cycles

        • Readonlymodel: M

          The template that requested it, narrowing both what is checked and what comes back

        • Optional Readonlyentry?: string

          The identifier the resource is expected to be named by, where it states one at all

      Returns Relay<{ value: Match<S, M>; trace: Optional<Trace> }>

      A Relay carrying the resource where it passes and a trace of the violations where it doesn't

      TraceError Where shape is malformed

    • Validates a retrieval template against a shape.

      Reports each slot of a template that asks for something the shape cannot give, so that a caller may refuse a request before issuing it, and hands back the template itself where it passes, held to what the service guarantees. A template describes what to retrieve rather than what is held, so the value-domain constraints are left alone and a slot the template omits is simply not asked for.

      Caution

      A template may draw on the full query language, including aggregate transforms and nested expansion. Hold an untrusted caller to what the service will serve: plain refuses the aggregate transforms, depth caps the nesting, and limit caps the page.

      Tip

      A member reaching a standalone resource may be asked for as the identifier naming it or as a template standing for the resource behind it; an embedded one, having no identifier of its own, only as a template. Stating depth as 0 thus leaves a link retrievable while refusing the resource behind it.

      Tip

      Revalidating a value costs nothing where the second call asks for no more than the first, as validating a resource explains.

      Type Parameters

      • T extends Template

        The template the shape admits

      Parameters

      • value: unknown

        The value to validate as a retrieval template

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

        Validation options

        • Readonlyshape: Lazy<ResourceShape>

          The shape the template is matched against, possibly deferred to break definition cycles

        • Readonlymodel: true

          Must be true to validate value as a template rather than as a resource

        • Optional Readonlyplain?: boolean

          Whether to refuse the transforms combining several values into one, leaving a template that retrieves rather than computes

        • Optional Readonlydepth?: number

          The nesting a template may ask for, counting each resource it descends into; 0 refuses every nested template while still admitting the identifier naming the resource

        • Optional Readonlylimit?: number

          The largest page a template may ask for; a collection asking for none is held to it, and one asking for more is refused. 0, like omitting it, leaves the page to the caller

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

      A Relay carrying the template where it passes and a trace of the violations where it doesn't

      TraceError Where shape is malformed