@grafctory/blue - v0.17.0
    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, nested resources and whole collections. The scope is closed by default. A template stating no bounds may ask only for the values a resource holds singly, and for each link only as the identifier naming its target. Each option opens one part of the query language up to the bound it states. A bound of Infinity lifts the cap altogether. It belongs only to a caller the service trusts with whatever it may ask for, as it leaves the cost of a request entirely to the client.

      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. A depth of 0 thus admits a link as the identifier naming its target, but not the resource behind it. It refuses an embedded resource altogether, as only a nested template may ask for one, and a depth of 0 admits no nested template.

      Tip

      The depth counts the resources a template descends into, whether linked or embedded. A nested template descends into one, and each step of the path a column or a constraint follows descends into one more. A collection may thus be paged, ordered and filtered at depth 0, as long as its constraints test the values it holds as they stand.

      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;
            stats?: 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 Readonlystats?: boolean

          Whether to admit the transforms combining several values into one, such as count or sum; omitted, it refuses them, while a transform applied to each value on its own is always admitted

        • Optional Readonlydepth?: number

          The nesting a template may descend to, counting each resource it enters through a nested template or a path step, linked or embedded alike. A depth of 0, like an omitted one, admits a link only as the identifier naming its target, while Infinity admits any nesting and any path length

        • 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, or for the whole set, is refused. 0, like omitting it, refuses every collection, while Infinity admits any page and leaves the paging of each collection to the caller, who may then ask for the whole set

      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