Libraries
    Preparing search index...

    Interface StringBuiltinExtensions<T>

    Methods added to StringSchemaBuilder by the built-in string extension pack.

    WORKAROUND: This interface duplicates the method signatures from stringExtensions 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 StringBuiltinExtensions<T extends string = string> {
        email(
            errorMessage?: ValidationErrorMessageProvider<
                StringSchemaBuilder<string, true, false, false, {}>,
            >,
        ): StringExtReturn<T>;
        ip(
            opts?: { version?: "v4" | "v6" },
            errorMessage?: ValidationErrorMessageProvider<
                StringSchemaBuilder<string, true, false, false, {}>,
            >,
        ): StringExtReturn<T>;
        nonempty(
            errorMessage?: ValidationErrorMessageProvider<
                StringSchemaBuilder<string, true, false, false, {}>,
            >,
        ): StringExtReturn<T>;
        oneOf<V extends string>(...values: [V, ...V[]]): StringExtReturn<V>;
        oneOf<V extends string>(
            ...args: [
                V,
                ...V[],
                ValidationErrorMessageProvider<
                    StringSchemaBuilder<string, true, false, false, {}>,
                >,
            ],
        ): StringExtReturn<V>;
        oneOf<V extends string>(
            values: readonly [V, V],
            errorMessage?: ValidationErrorMessageProvider<
                StringSchemaBuilder<string, true, false, false, {}>,
            >,
        ): StringExtReturn<V>;
        toLowerCase(): StringExtReturn<T>;
        trim(): StringExtReturn<T>;
        url(
            errorMessage?: ValidationErrorMessageProvider<
                StringSchemaBuilder<string, true, false, false, {}>,
            >,
        ): StringExtReturn<T>;
        url(
            opts?: { protocols?: string[] },
            errorMessage?: ValidationErrorMessageProvider<
                StringSchemaBuilder<string, true, false, false, {}>,
            >,
        ): StringExtReturn<T>;
        uuid(
            errorMessage?: ValidationErrorMessageProvider<
                StringSchemaBuilder<string, true, false, false, {}>,
            >,
        ): StringExtReturn<T>;
    }

    Type Parameters

    • T extends string = string
    Index

    Methods

    • Validates that the string is a well-formed email address.

      Uses the pattern ^[^\s@]+@[^\s@]+\.[^\s@]+$ for validation.

      Parameters

      Returns StringExtReturn<T>

      a new schema builder with the email validator applied

      string().email();
      string().email('Please enter a valid email');
      string().email((val) => `"${val}" is not a valid email`);
    • Validates that the string is a valid IP address (IPv4 or IPv6).

      Pass opts.version to restrict validation to a specific IP version.

      Parameters

      • Optionalopts: { version?: "v4" | "v6" }

        optional configuration

        • Optionalversion?: "v4" | "v6"

          restrict to 'v4' or 'v6' (default: accept both)

      • OptionalerrorMessage: ValidationErrorMessageProvider<
            StringSchemaBuilder<string, true, false, false, {}>,
        >

        custom error message or function to generate one

      Returns StringExtReturn<T>

      a new schema builder with the IP validator applied

      string().ip();
      string().ip({ version: 'v4' });
      string().ip(undefined, 'Bad IP address');
    • Validates that the string is not empty (length > 0).

      Parameters

      Returns StringExtReturn<T>

      a new schema builder with the nonempty validator applied

      string().nonempty();
      string().nonempty('Name is required');
    • Constrains the string to one of the specified literal values.

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

      Type Parameters

      • V extends string

      Parameters

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

        the allowed string literals

      Returns StringExtReturn<V>

      a new schema builder restricted to the given values

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

      const role = string().oneOf('admin', 'user', 'guest');
      type Role = InferType<typeof role>; // 'admin' | 'user' | 'guest'

      role.validate('admin'); // valid
      role.validate('other'); // invalid — "must be one of: admin, user, guest"
    • Constrains the string to one of the specified literal values, with a custom error message or factory as the last argument.

      Type Parameters

      • V extends string

      Parameters

      Returns StringExtReturn<V>

      const role = string().oneOf('admin', 'user', (val) => `"${val}" is not allowed`);
      
    • Constrains the string to one of the specified literal values, with an optional custom error message or factory.

      Type Parameters

      • V extends string

      Parameters

      Returns StringExtReturn<V>

      a new schema builder restricted to the given values

      const role = string().oneOf(['admin', 'user', 'guest'], 'Invalid role');
      
    • Preprocessor that converts the string to lowercase before validation.

      Returns StringExtReturn<T>

      a new schema builder with the toLowerCase preprocessor applied

      string().toLowerCase(); // 'HELLO' → 'hello'
      
    • Preprocessor that trims leading and trailing whitespace before validation.

      Returns StringExtReturn<T>

      a new schema builder with the trim preprocessor applied

      string().trim().minLength(1); // '  hi  ' → 'hi'
      
    • Validates that the string is a well-formed URL.

      By default only http and https protocols are accepted. Pass opts.protocols to restrict or expand the allowed set.

      Parameters

      Returns StringExtReturn<T>

      a new schema builder with the URL validator applied

      string().url();
      string().url({ protocols: ['https'] });
      string().url('Must be a valid URL');
      string().url({ protocols: ['https'] }, 'Must be a valid URL');
    • Parameters

      Returns StringExtReturn<T>

    • Validates that the string is a valid UUID (versions 1–5).

      Parameters

      Returns StringExtReturn<T>

      a new schema builder with the UUID validator applied

      string().uuid();
      string().uuid('Invalid identifier');