Chatoyant - v0.2.1
    Preparing search index...

    Class SchemaAbstract

    Abstract base class for defining type-safe schemas.

    Extend this class and define fields using the static type methods. Use static functions to create, parse, validate, and serialize instances.

    class Product extends Schema {
    name = Schema.String({ minLength: 1 });
    price = Schema.Number({ minimum: 0 });
    inStock = Schema.Boolean({ default: true });
    }
    Index

    Constructors

    Properties

    String: (options?: StringFieldOptions) => FieldDescriptor<string> = descriptors.String

    Create a string field.

    Type Declaration

      • (options?: StringFieldOptions): FieldDescriptor<string>
      • Create a string field descriptor.

        Parameters

        Returns FieldDescriptor<string>

        Field descriptor for string values

        class User extends Schema {
        name = String({ description: "User's name", minLength: 1 });
        email = String({ format: "email", optional: true });
        }

    StringFieldOptions for available options

    Number: (options?: NumberFieldOptions) => FieldDescriptor<number> = descriptors.Number

    Create a number field (floating point).

    Type Declaration

      • (options?: NumberFieldOptions): FieldDescriptor<number>
      • Create a number field descriptor.

        Parameters

        Returns FieldDescriptor<number>

        Field descriptor for number values

        class Product extends Schema {
        price = Number({ minimum: 0, description: "Price in cents" });
        rating = Number({ minimum: 0, maximum: 5, optional: true });
        }

    NumberFieldOptions for available options

    Integer: (options?: IntegerFieldOptions) => FieldDescriptor<number> = descriptors.Integer

    Create an integer field (whole numbers only).

    Type Declaration

      • (options?: IntegerFieldOptions): FieldDescriptor<number>
      • Create an integer field descriptor. Integers are whole numbers (no decimal places).

        Parameters

        Returns FieldDescriptor<number>

        Field descriptor for integer values

        class User extends Schema {
        age = Integer({ minimum: 0, maximum: 150 });
        level = Integer({ minimum: 1, default: 1 });
        }

    IntegerFieldOptions for available options

    Boolean: (options?: BooleanFieldOptions) => FieldDescriptor<boolean> = descriptors.Boolean

    Create a boolean field.

    Type Declaration

      • (options?: BooleanFieldOptions): FieldDescriptor<boolean>
      • Create a boolean field descriptor.

        Parameters

        Returns FieldDescriptor<boolean>

        Field descriptor for boolean values

        class Task extends Schema {
        completed = Boolean({ default: false });
        urgent = Boolean({ optional: true });
        }

    BooleanFieldOptions for available options

    Null: (options?: BaseFieldOptions) => FieldDescriptor<null> = descriptors.Null

    Create a null field.

    Type Declaration

      • (options?: BaseFieldOptions): FieldDescriptor<null>
      • Create a null field descriptor. Represents a field that can only be null.

        Parameters

        Returns FieldDescriptor<null>

        Field descriptor for null values

        class DeleteRequest extends Schema {
        deletedAt = Null({ description: "Deletion marker" });
        }
    Array: <T>(
        items: FieldDescriptor<T>,
        options?: ArrayFieldOptions,
    ) => FieldDescriptor<T[]> = descriptors.Array

    Create an array field with typed items.

    Type Declaration

      • <T>(
            items: FieldDescriptor<T>,
            options?: ArrayFieldOptions,
        ): FieldDescriptor<T[]>
      • Create an array field descriptor.

        Type Parameters

        • T

        Parameters

        Returns FieldDescriptor<T[]>

        Field descriptor for array values

        class Post extends Schema {
        tags = Array(String(), { minItems: 1, maxItems: 10 });
        scores = Array(Number(), { uniqueItems: true });
        authors = Array(Object(Author), { minItems: 1 });
        }

    ArrayFieldOptions for available options

    Object: <S extends SchemaConstructor<SchemaInstance>>(
        schema: S,
        options?: ObjectFieldOptions,
    ) => FieldDescriptor<InferSchemaType<S>> = descriptors.Object

    Create a nested object field from another Schema class.

    Type Declaration

    ObjectFieldOptions for available options

    Enum: <const T extends readonly unknown[]>(
        values: T,
        options?: EnumFieldOptions,
    ) => FieldDescriptor<T[number]> = descriptors.Enum

    Create an enum field with specific allowed values. Use as const for literal type inference.

    Type Declaration

      • <const T extends readonly unknown[]>(
            values: T,
            options?: EnumFieldOptions,
        ): FieldDescriptor<T[number]>
      • Create an enum field descriptor. The value must be one of the specified allowed values.

        Type Parameters

        • const T extends readonly unknown[]

        Parameters

        • values: T

          Array of allowed values (use as const for literal types)

        • options: EnumFieldOptions = {}

          Enum field configuration

        Returns FieldDescriptor<T[number]>

        Field descriptor for enum values

        class User extends Schema {
        role = Enum(["admin", "user", "guest"] as const);
        priority = Enum([1, 2, 3] as const, { default: 2 });
        status = Enum(["active", "inactive"] as const, { description: "Account status" });
        }

        const user = User.create();
        user.role; // Type: "admin" | "user" | "guest"

    EnumFieldOptions for available options

    Literal: <const T>(
        value: T,
        options?: LiteralFieldOptions,
    ) => FieldDescriptor<T> = descriptors.Literal

    Create a literal (const) field with an exact value.

    Type Declaration

      • <const T>(value: T, options?: LiteralFieldOptions): FieldDescriptor<T>
      • Create a literal field descriptor. The value must be exactly the specified constant.

        Type Parameters

        • const T

        Parameters

        • value: T

          The exact value this field must have

        • options: LiteralFieldOptions = {}

          Literal field configuration

        Returns FieldDescriptor<T>

        Field descriptor for literal values

        class APIResponse extends Schema {
        version = Literal("1.0");
        success = Literal(true);
        }

        const response = APIResponse.create();
        response.version; // Type: "1.0" (literal)
        response.success; // Type: true (literal)

    LiteralFieldOptions for available options

    Methods

    • Validate and populate a schema instance with data. Throws SchemaError if validation fails.

      Type Parameters

      Parameters

      • instance: T | Proxied<T>

        The schema instance (proxied or raw)

      • data: unknown

        The data to parse

      Returns void

    • Validate data against a schema without modifying the instance.

      Type Parameters

      Parameters

      • instance: T | Proxied<T>

        The schema instance (proxied or raw)

      • data: unknown

        The data to validate

      Returns boolean

      true if valid, false otherwise

    • Generate a JSON Schema string from a schema instance.

      Type Parameters

      Parameters

      • instance: T | Proxied<T>

        The schema instance (proxied or raw)

      • pretty: boolean = true

        Whether to pretty-print (default: true)

      Returns string

      JSON Schema as a string