Libraries
    Preparing search index...

    Interface NumberBuiltinExtensions<T>

    Methods added to NumberSchemaBuilder by the built-in number extension pack.

    WORKAROUND: This interface duplicates the method signatures from numberExtensions so that JSDoc survives into the published .d.ts files. TypeScript strips JSDoc when method signatures are reconstructed through the FixedMethods mapped type (conditional infer loses comments). Remove this interface once TypeScript preserves JSDoc through mapped types / conditional type inference.

    interface NumberBuiltinExtensions<T extends number = number> {
        finite(
            errorMessage?: ValidationErrorMessageProvider<
                NumberSchemaBuilder<number, true, false, false, {}>,
            >,
        ): NumberExtReturn<T>;
        multipleOf(
            n: number,
            errorMessage?: ValidationErrorMessageProvider<
                NumberSchemaBuilder<number, true, false, false, {}>,
            >,
        ): NumberExtReturn<T>;
        negative(
            errorMessage?: ValidationErrorMessageProvider<
                NumberSchemaBuilder<number, true, false, false, {}>,
            >,
        ): NumberExtReturn<T>;
        oneOf<V extends number>(...values: [V, ...V[]]): NumberExtReturn<V>;
        oneOf<V extends number>(
            ...args: [
                V,
                ...V[],
                ValidationErrorMessageProvider<
                    NumberSchemaBuilder<number, true, false, false, {}>,
                >,
            ],
        ): NumberExtReturn<V>;
        oneOf<V extends number>(
            values: readonly [V, V],
            errorMessage?: ValidationErrorMessageProvider<
                NumberSchemaBuilder<number, true, false, false, {}>,
            >,
        ): NumberExtReturn<V>;
        positive(
            errorMessage?: ValidationErrorMessageProvider<
                NumberSchemaBuilder<number, true, false, false, {}>,
            >,
        ): NumberExtReturn<T>;
    }

    Type Parameters

    • T extends number = number
    Index

    Methods

    • Validates that the number is finite (rejects Infinity and -Infinity).

      Parameters

      Returns NumberExtReturn<T>

      a new schema builder with the finite validator applied

      number().finite();
      number().finite('No infinities allowed');
    • Validates that the number is an exact multiple of n.

      Uses a relative tolerance of 1e-10 for float-safe comparison.

      Parameters

      Returns NumberExtReturn<T>

      a new schema builder with the multipleOf validator applied

      number().multipleOf(5);
      number().multipleOf(0.1, 'Must be a multiple of 0.1');
    • Validates that the number is strictly less than zero.

      Parameters

      Returns NumberExtReturn<T>

      a new schema builder with the negative validator applied

      number().negative();
      number().negative('Must be below zero');
    • Constrains the number to one of the specified literal values.

      Narrows the inferred type from number to the union of the provided literals.

      Type Parameters

      • V extends number

      Parameters

      • ...values: [V, ...V[]]

        the allowed number literals

      Returns NumberExtReturn<V>

      a new schema builder restricted to the given values

      import { number, InferType } from '@cleverbrush/schema';

      const priority = number().oneOf(1, 2, 3);
      type Priority = InferType<typeof priority>; // 1 | 2 | 3

      priority.validate(1); // valid
      priority.validate(4); // invalid — "must be one of: 1, 2, 3"
    • Constrains the number to one of the specified literal values, with a custom error message or factory as the last argument.

      Type Parameters

      • V extends number

      Parameters

      Returns NumberExtReturn<V>

      const priority = number().oneOf(1, 2, 3, 'Priority must be 1, 2, or 3');
      const priority2 = number().oneOf(1, 2, 3, (val) => `${val} is not a valid priority`);
    • Constrains the number to one of the specified literal values, with an optional custom error message or factory.

      Type Parameters

      • V extends number

      Parameters

      Returns NumberExtReturn<V>

      a new schema builder restricted to the given values

      const priority = number().oneOf([1, 2, 3], 'Must be 1, 2, or 3');
      
    • Validates that the number is strictly greater than zero.

      Parameters

      Returns NumberExtReturn<T>

      a new schema builder with the positive validator applied

      number().positive();
      number().positive('Must be greater than zero');