Class NumberShape

The shape of a number value.

Hierarchy

Constructors

Properties

_applyCoerce: ((input) => number) = coerceToNever

Type declaration

_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.

_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.

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.

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<number>

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

  • Asynchronously parses the input.

    Parameters

    • input: unknown

      The shape input to parse.

    • options: ApplyOptions

      Parsing options.

    • nonce: number

      The globally unique number that identifies the parsing process.

    Returns Promise<Result<number>>

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

  • Coerces an input value to another type.

    Override this method along with Shape._getInputs to implement custom type coercion.

    Parameters

    • input: unknown

      The input value to coerce.

    Returns number

    The coerced value, or NEVER if coercion isn't possible.

  • 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

  • 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.

  • 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: number

            The shape output value.

          • options: ApplyOptions

            Parsing options.

          Returns ConvertedValue

    Returns Shape<number, 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: number

            The shape output value.

          • options: ApplyOptions

            Parsing options.

          Returns PromiseLike<ConvertedValue>

    Returns Shape<number, ConvertedValue>

    The ConvertShape instance.

  • 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<NumberShape, ExcludedShape>

  • Synchronously parses the value.

    Parameters

    • input: unknown

      The value to parse.

    • Optional options: ParseOptions

      Parsing options.

    Returns number

    The value that conforms the output type of the shape.

    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<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 | number

    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 number | 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 | 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.

    • Optional options: ApplyOptions

      Parsing options.

    Returns Promisify<number | 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<NumberShape, InputValue, OutputValue>

  • 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<NumberShape, 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<number>

    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<number>>

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

Plugin Methods

  • Constrains the number to be between the inclusive minimum and maximum.

    Parameters

    • minValue: number

      The inclusive minimum value.

    • maxValue: number

      The inclusive maximum value.

    • Optional options: IssueOptions | Message

      The issue options or the issue message.

    Returns NumberShape

    The clone of the shape.

  • Constrains the number to be a multiple of the divisor.

    Parameters

    • divisor: number

      The positive number by which the input should be divisible without a remainder.

    • Optional options: Message | MultipleOfOptions

      The check options or the issue message.

    Returns NumberShape

    The clone of the shape.

Accessors

  • get isCoercing(): boolean
  • true if this shapes coerces input values to the required type during parsing, or false otherwise.

    Returns boolean