Class UnionShape<Shapes>

The shape that requires an input to conform at least one of shapes.

Type Parameters

  • Shapes extends readonly AnyShape[]

    The array of shapes that comprise a union.

Hierarchy (view full)

Implements

Constructors

Properties

_options: undefined | IssueOptions | Message

The union constraint options or an issue message.

annotations: Dict<any> = {}

The dictionary of shape annotations.

inputs: readonly unknown[]

The array of unique input types and values that are accepted by the shape.

isAsync: boolean

true if the shape allows only Shape.parseAsync and throws an error if Shape.parse is called, or false if the shape can be used in both sync and async contexts.

operations: readonly Operation[] = []

The array of operations that are applied to the shape output.

shapes: Shapes

The array of shapes that comprise a union.

Methods

  • Returns input types and literal values that this shape can accept as an input.

    Returns readonly unknown[]

  • Must return true if the shape must be used in async context only, otherwise the shape can be used in both sync and async contexts. Override this method to implement a custom shape.

    Returns boolean

  • Returns true if the shape accepts given input type or value, or false otherwise.

    Parameters

    • input: unknown

      The type or value that must be checked.

    Returns boolean

  • Adds annotations to the shape.

    Parameters

    • annotations: ReadonlyDict<any>

      Annotations to add.

    Returns this

    The clone of the shape with the updated annotations.

  • Returns a sub-shape that describes a value associated with the given property name, or null if there's no such sub-shape.

    Parameters

    • key: unknown

    Returns null | AnyShape

    The sub-shape or null if there's no such key in the shape.

  • Adds an asynchronous operation that checks that the shape output satisfies a requirement.

    The callback must return a promise that is resolved with null or undefined if value is valid, or an issue or an array of issues if value is invalid. If promise resolves with an empty array, it is considered that no issues have occurred.

    Type Parameters

    • Param

      The param that is passed to the operation when it is applied.

    Parameters

    Returns this

    The clone of the shape.

  • Adds an asynchronous operation that checks that the shape output satisfies a requirement.

    The callback must return a promise that is resolved with null or undefined if value is valid, or an issue or an array of issues if value is invalid. If promise resolves with an empty array, it is considered that no issues have occurred.

    Parameters

    Returns this

    The clone of the shape.

  • Converts the shape and its child shapes to deep partial alternatives.

    Note: This method returns a shape without any operations.

    Returns DeepPartialUnionShape<Shapes>

    The deep partial clone of the shape.

  • Synchronously parses the value.

    Parameters

    • input: unknown

      The value to parse.

    • Optionaloptions: ParseOptions

      Parsing options.

    Returns Output<Shapes[number]>

    The value that conforms the output type of the shape.

    Error if the shape doesn't support the sync parsing, see Shape.isAsync.

    ValidationError if any issues occur during parsing.

  • Asynchronously parses the value.

    Parameters

    • input: unknown

      The value to parse.

    • Optionaloptions: ParseOptions

      Parsing options.

    Returns Promisify<Output<Shapes[number]>>

    The promise that resolves with the value that conforms the output type of the shape, or rejects with a ValidationError if any issues occur during parsing.

  • Synchronously parses the value and returns undefined if parsing fails.

    Parameters

    • input: unknown

      The value to parse.

    Returns undefined | Output<Shapes[number]>

    The value that conforms the output type of the shape.

    Error if the shape doesn't support the sync parsing, see Shape.isAsync.

  • Synchronously parses the value and returns the default value if parsing fails.

    Type Parameters

    • DefaultValue

      The default value that is returned if parsing fails.

    Parameters

    • input: unknown

      The value to parse.

    • defaultValue: DefaultValue

      The default value that is returned if parsing fails.

    • Optionaloptions: ParseOptions

      Parsing options.

    Returns Output<Shapes[number]> | DefaultValue

    The value that conforms the output type of the shape.

    Error if the shape doesn't support the sync parsing, see Shape.isAsync.

  • Asynchronously parses the value and returns undefined value if parsing fails.

    Parameters

    • input: unknown

      The value to parse.

    Returns Promisify<undefined | Output<Shapes[number]>>

    The value that conforms the output type of the shape.

  • Asynchronously parses the value and returns the default value if parsing fails.

    Type Parameters

    • DefaultValue

      The default value that is returned if parsing fails.

    Parameters

    • input: unknown

      The value to parse.

    • defaultValue: DefaultValue

      The default value that is returned if parsing fails.

    • Optionaloptions: ParseOptions

      Parsing options.

    Returns Promisify<Output<Shapes[number]> | DefaultValue>

    The value that conforms the output type of the shape.

  • Synchronously parses the value and returns Ok or Err object that wraps the result.

    Parameters

    • input: unknown

      The value to parse.

    • Optionaloptions: ParseOptions

      Parsing options.

    Returns Err | Ok<Output<Shapes[number]>>

    The Ok instance if parsing has succeeded or Err if parsing has failed.

    Error if the shape doesn't support the sync parsing, see Shape.isAsync.