Class FunctionShape<ArgsShape, ReturnShape, ThisShape>

The shape of a function.

Type Parameters

  • ArgsShape extends Shape<readonly any[], readonly any[]>

    The shape of the array of arguments.

  • ReturnShape extends AnyShape | null

    The return value shape, or null if unconstrained.

  • ThisShape extends AnyShape | null

    The shape of this argument, or null if unconstrained.

Hierarchy

  • Shape<((this, ...args) => InferOrDefault<ReturnShape, INPUT>), ((this, ...args) => InferOrDefault<ReturnShape, OUTPUT>)>
    • FunctionShape

Constructors

  • Creates a new FunctionShape instance.

    Type Parameters

    • ArgsShape extends Shape<readonly any[], readonly any[]>

      The shape of the array of arguments.

    • ReturnShape extends null | AnyShape

      The return value shape, or null if unconstrained.

    • ThisShape extends null | AnyShape

      The shape of this argument, or null if unconstrained.

    Parameters

    • argsShape: ArgsShape

      The shape of the array of arguments.

    • returnShape: ReturnShape

      The return value shape, or null if unconstrained.

    • thisShape: ThisShape

      The shape of this argument, or null if unconstrained.

    • Optional options: IssueOptions | Message

      The issue options or the issue message.

    Returns FunctionShape<ArgsShape, ReturnShape, ThisShape>

Properties

_applyOperations: ApplyOperationsCallback

The callback that applies Shape.operations to the shape output value.

This method returns a promise if there are async Shape.operations.

If the shape overrides only Shape._apply and doesn't override Shape._applyAsync then it's only safe to call this method as the last statement in Shape._apply. Otherwise, it may return an unexpected promise.

If the shape overrides both Shape._apply and Shape._applyAsync then this method would always synchronously return a Result inside Shape._apply.

_parseOptions: undefined | ParseOptions

Parsing options that are used by a wrapper.

_typeIssueFactory: ((input, options) => Issue)

Type declaration

    • (input, options): Issue
    • Returns issues associated with an invalid input value type.

      Parameters

      Returns Issue

annotations: Dict<any> = {}

The dictionary of shape annotations.

argsShape: ArgsShape

The shape of the array of arguments.

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.

isStrict: boolean = false

true if input functions are wrapped during parsing to ensure runtime signature type-safety, or false otherwise.

operations: readonly Operation[] = []

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

returnShape: ReturnShape

The return value shape, or null if unconstrained.

thisShape: ThisShape

The shape of this value, or null if unconstrained.

messages: Messages = defaultMessages

The mapping from an issue type to a corresponding issue message.

Methods

  • Synchronously parses the input.

    Parameters

    • input: any

      The shape input to parse.

    • options: ApplyOptions

      Parsing options.

    • nonce: number

      The globally unique number that identifies the parsing process.

    Returns Result<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>

    null if input matches the output, Ok that wraps the output, or an array of captured issues.

  • Asynchronously parses the input.

    Parameters

    • input: any

      The shape input to parse.

    • options: ApplyOptions

      Parsing options.

    • nonce: number

      The globally unique number that identifies the parsing process.

    Returns Promise<Result<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>>

    null if input matches the output, Ok that wraps the output, or an array of captured issues.

  • 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 an asynchronous operation to the shape.

    Type Parameters

    • Param

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

    Parameters

    • cb: OperationCallback<PromiseLike<Result<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>>, ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>), Param>

      The callback that asynchronously applies an operation to the shape output value.

    • Optional options: ParameterizedOperationOptions<Param>

      The operation options.

    Returns FunctionShape<ArgsShape, ReturnShape, ThisShape>

    The clone of the shape.

  • Adds an asynchronous operation to the shape.

    Parameters

    • cb: OperationCallback<PromiseLike<Result<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>>, ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>

      The callback that asynchronously applies an operation to the shape output value.

    • Optional options: OperationOptions

      The operation options.

    Returns FunctionShape<ArgsShape, ReturnShape, ThisShape>

    The clone of the shape.

  • Adds a synchronous operation to the shape.

    Type Parameters

    • Param

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

    Parameters

    • cb: OperationCallback<Result<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>, ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>), Param>

      The callback that synchronously applies an operation to the shape output value.

    • Optional options: ParameterizedOperationOptions<Param>

      The operation options.

    Returns FunctionShape<ArgsShape, ReturnShape, ThisShape>

    The clone of the shape.

  • Adds a synchronous operation to the shape.

    Parameters

    • cb: OperationCallback<Result<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>, ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>

      The callback that synchronously applies an operation to the shape output value.

    • Optional options: OperationOptions

      The operation options.

    Returns FunctionShape<ArgsShape, ReturnShape, ThisShape>

    The clone of the shape.

  • Allows an input value, so it is passed directly to the output.

    Type Parameters

    • AllowedValue extends Any

      The allowed value.

    Parameters

    • value: AllowedValue

      The allowed value.

    Returns AllowShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, AllowedValue>

  • Adds annotations to the shape.

    Parameters

    • annotations: ReadonlyDict<any>

      Annotations to add.

    Returns FunctionShape<ArgsShape, ReturnShape, ThisShape>

    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

      The key for which the sub-shape must be retrieved.

    Returns null | AnyShape

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

  • Returns a shape that adds a brand to the output type.

    Type Parameters

    • Brand extends string | number | symbol

      The brand value.

    Returns RefineShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, Branded<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>), Brand>>

    A shape with the branded output type.

  • Returns undefined if parsing fails.

    Returns CatchShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, undefined>

  • Returns the fallback value if parsing fails.

    Type Parameters

    • FallbackValue extends Any

      The fallback value.

    Parameters

    • fallback: FallbackValue | ((input, issues, options) => FallbackValue)

      The value or a callback that returns a value that is returned if parsing has failed. A callback receives an input value, an array of raised issues, and parsing options.

    Returns CatchShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, FallbackValue>

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

    The callback must return null or undefined if value is valid, or an issue or an array of issues if value is invalid. If a callback returns 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 FunctionShape<ArgsShape, ReturnShape, ThisShape>

    The clone of the shape.

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

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

    Parameters

    • cb: OperationCallback<CheckResult, ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>

      The callback that checks that a value satisfies a requirement and returns issues if it doesn't.

    • Optional options: OperationOptions

      The operation options.

    Returns FunctionShape<ArgsShape, ReturnShape, ThisShape>

    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.

    Type Parameters

    • Param

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

    Parameters

    • cb: OperationCallback<PromiseLike<CheckResult>, ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>), Param>

      The callback that checks that a value satisfies a requirement and returns issues if it doesn't.

    • options: ParameterizedOperationOptions<Param>

      The operation options.

    Returns FunctionShape<ArgsShape, ReturnShape, ThisShape>

    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

    • cb: OperationCallback<PromiseLike<CheckResult>, ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>

      The callback that checks that a value satisfies a requirement and returns issues if it doesn't.

    • Optional options: OperationOptions

      The operation options.

    Returns FunctionShape<ArgsShape, ReturnShape, ThisShape>

    The clone of the shape.

  • Synchronously converts the output value of the shape.

    If you want to don't want to change the base type, consider using Shape.alter.

    Type Parameters

    • ConvertedValue

      The value returned from the callback that converts the output value of this shape.

    Parameters

    • cb: ((value, options) => ConvertedValue)

      The callback that converts the input value. Throw a ValidationError to notify that the conversion cannot be successfully completed.

        • (value, options): ConvertedValue
        • Parameters

          • value: ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)

            The shape output value.

              • (this, ...args): InferOrDefault<ReturnShape, typeof OUTPUT, any>
              • Parameters

                • this: InferOrDefault<ThisShape, typeof INPUT, any>
                • Rest ...args: Input<ArgsShape>

                Returns InferOrDefault<ReturnShape, typeof OUTPUT, any>

          • options: ApplyOptions

            Parsing options.

          Returns ConvertedValue

    Returns Shape<((this, ...args) => InferOrDefault<ReturnShape, typeof INPUT, any>), ConvertedValue>

    The ConvertShape instance.

  • Asynchronously converts the output value of the shape.

    If you want to don't want to change the base type, consider using Shape.alterAsync.

    Type Parameters

    • ConvertedValue

      The value returned from the callback that converts the output value of this shape.

    Parameters

    • cb: ((value, options) => PromiseLike<ConvertedValue>)

      The callback that converts the input value asynchronously. The returned promise can be rejected with a ValidationError to notify that the conversion cannot be successfully completed.

        • (value, options): PromiseLike<ConvertedValue>
        • Parameters

          • value: ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)

            The shape output value.

              • (this, ...args): InferOrDefault<ReturnShape, typeof OUTPUT, any>
              • Parameters

                • this: InferOrDefault<ThisShape, typeof INPUT, any>
                • Rest ...args: Input<ArgsShape>

                Returns InferOrDefault<ReturnShape, typeof OUTPUT, any>

          • options: ApplyOptions

            Parsing options.

          Returns PromiseLike<ConvertedValue>

    Returns Shape<((this, ...args) => InferOrDefault<ReturnShape, typeof INPUT, any>), ConvertedValue>

    The ConvertShape instance.

  • Excludes value from both input and output.

    Type Parameters

    • DeniedValue extends ((this, ...args) => InferOrDefault<ReturnShape, typeof INPUT, any>) | ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)

      The denied value.

    Parameters

    • value: DeniedValue

      The excluded value.

    • Optional options: IssueOptions | Message

      The issue options or the issue message.

    Returns DenyShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, DeniedValue>

  • Creates a function that ensures that fn receives arguments and this value that conform the FunctionShape.argsShape and the FunctionShape.thisShape respectively, and synchronously returns the value that conforms the FunctionShape.returnShape.

    Type Parameters

    • F extends ((this, ...args) => InferOrDefault<ReturnShape, typeof INPUT, any>)

      The function to which signature must be ensured.

    Parameters

    Returns ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, ReturnType<F>>)

    The wrapper function.

      • (this, ...args): InferOrDefault<ReturnShape, typeof OUTPUT, ReturnType<F>>
      • Creates a function that ensures that fn receives arguments and this value that conform the FunctionShape.argsShape and the FunctionShape.thisShape respectively, and synchronously returns the value that conforms the FunctionShape.returnShape.

        Parameters

        • this: InferOrDefault<ThisShape, typeof INPUT, ThisType<F>>
        • Rest ...args: Input<ArgsShape>

        Returns InferOrDefault<ReturnShape, typeof OUTPUT, ReturnType<F>>

        The wrapper function.

  • Creates a function that ensures that fn receives arguments and this value that conform the FunctionShape.argsShape and the FunctionShape.thisShape respectively, and asynchronously returns the value that conforms the FunctionShape.returnShape.

    Use this method if some shapes that describe the function signature are async.

    Type Parameters

    • F extends ((this, ...args) => Awaitable<InferOrDefault<ReturnShape, typeof INPUT, any>>)

      The function to which signature must be ensured.

    Parameters

    Returns ((this, ...args) => Promisify<InferOrDefault<ReturnShape, typeof OUTPUT, ReturnType<F>>>)

    The wrapper function.

  • Checks that the input doesn't match the shape.

    Type Parameters

    • ExcludedShape extends AnyShape

      The shape to which the output must not conform.

    Parameters

    • shape: ExcludedShape

      The shape to which the output must not conform.

    • Optional options: IssueOptions | Message

      The issue options or the issue message.

    Returns ExcludeShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, ExcludedShape>

  • Checks that the input doesn't match the shape.

    This method works exactly as Shape.exclude at runtime, but it doesn't perform the exclusion on the type level.

    Type Parameters

    • ExcludedShape extends AnyShape

      The shape to which the output must not conform.

    Parameters

    • shape: ExcludedShape

      The shape to which the output must not conform.

    • Optional options: IssueOptions | Message

      The issue options or the issue message.

    Returns NotShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, ExcludedShape>

  • Replaces null input value with an null output value.

    Returns AllowShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, null>

  • Replaces null input value with a default output value.

    Type Parameters

    • DefaultValue extends Any

      The value that is used as the replacement for null.

    Parameters

    • defaultValue: DefaultValue

      The value that should be used if an input value is null.

    Returns ReplaceShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, null, DefaultValue>

  • Passes null and undefined input values directly to the output without parsing.

    Returns AllowShape<AllowShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, null>, undefined>

  • Replaces null and undefined input value with a default output value.

    Type Parameters

    • DefaultValue extends Any

      The value that is used as the replacement for undefined and null.

    Parameters

    • Optional defaultValue: DefaultValue

      The value that should be used if an input value is undefined or null.

    Returns ReplaceShape<ReplaceShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, null, DefaultValue>, undefined, DefaultValue>

  • Replaces undefined input value with an undefined output value.

    Returns AllowShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, undefined>

  • Replaces undefined input value with a default output value.

    Type Parameters

    • DefaultValue extends Any

      The value that is used as the replacement for undefined.

    Parameters

    • defaultValue: DefaultValue

      The value that should be used if an input value is undefined.

    Returns ReplaceShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, undefined, DefaultValue>

  • Synchronously parses the value.

    Parameters

    • input: unknown

      The value to parse.

    • Optional options: ParseOptions

      Parsing options.

    Returns ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)

    The value that conforms the output type of the shape.

      • (this, ...args): InferOrDefault<ReturnShape, typeof OUTPUT, any>
      • Parameters

        • this: InferOrDefault<ThisShape, typeof INPUT, any>
        • Rest ...args: Input<ArgsShape>

        Returns InferOrDefault<ReturnShape, typeof OUTPUT, any>

    Throws

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

    Throws

    ValidationError if any issues occur during parsing.

  • Asynchronously parses the value.

    Parameters

    • input: unknown

      The value to parse.

    • Optional options: ParseOptions

      Parsing options.

    Returns Promisify<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>

    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 | ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)

    The value that conforms the output type of the shape.

    Throws

    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.

    • Optional options: ApplyOptions

      Parsing options.

    Returns ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>) | DefaultValue

    The value that conforms the output type of the shape.

    Throws

    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 | ((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>

    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.

    • Optional options: ApplyOptions

      Parsing options.

    Returns Promisify<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>) | DefaultValue>

    The value that conforms the output type of the shape.

  • Replaces an input value with an output value.

    Type Parameters

    • InputValue extends Any

      The input value to replace.

    • OutputValue extends Any

      The output value that is used as the replacement for an input value.

    Parameters

    • inputValue: InputValue

      The input value to replace.

    • outputValue: OutputValue

      The output value that is returned if an inputValue is received.

    Returns ReplaceShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, InputValue, OutputValue>

  • Constrains the function return value with the given shape.

    Type Parameters

    • S extends null | AnyShape

      The return value shape.

    Parameters

    • shape: S

      The return value shape, or null if unconstrained.

    Returns FunctionShape<ArgsShape, S, ThisShape>

    The new function shape.

  • Enables input function wrapping during parsing to ensure runtime signature type-safety. Wrapper ensures that input function receives arguments and this values that conform FunctionShape.argsShape and FunctionShape.thisShape respectively, and returns the value that conforms FunctionShape.returnShape.

    Parameters

    • Optional options: ParseOptions

      Options that are used by the wrapper. If omitted then default options are applied: no early-return, no type coercion.

    Returns FunctionShape<ArgsShape, ReturnShape, ThisShape>

    The new function shape.

  • Constrains the type of this inside the function.

    Type Parameters

    • S extends null | AnyShape

      The shape of this argument.

    Parameters

    • shape: S

      The shape of this argument, or null if unconstrained.

    Returns FunctionShape<ArgsShape, ReturnShape, S>

    The new function shape.

  • Pipes the output of this shape to the input of another shape.

    Type Parameters

    • OutputShape extends AnyShape

      The output value.

    Parameters

    • shape: OutputShape

      The shape that validates the output if this shape.

    Returns PipeShape<FunctionShape<ArgsShape, ReturnShape, ThisShape>, OutputShape>

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

    Parameters

    • input: unknown

      The value to parse.

    • Optional options: ApplyOptions

      Parsing options.

    Returns Err | Ok<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>

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

    Throws

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

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

    Parameters

    • input: unknown

      The value to parse.

    • Optional options: ApplyOptions

      Parsing options.

    Returns Promise<Err | Ok<((this, ...args) => InferOrDefault<ReturnShape, typeof OUTPUT, any>)>>

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

Accessors

  • get isAsyncFunction(): boolean
  • true if some shapes that describe the function signature are async, or false otherwise.

    Returns boolean