Module @andi/powers/models/validation-service/contracts

Index

Classes

Interfaces

Type aliases

Variables

Type aliases

InferSchema

InferSchema<T>: T extends BaseSchema<infer U> ? U : never

Infers the TypeScript type from a given schema.

This utility type extracts the type that the schema validates, making it easier to work with strongly typed data.

example
const schema = val.string();
type InferredType = InferSchema<typeof schema>; // InferredType is `string`

const numberSchema = val.number();
type NumberType = InferSchema<typeof numberSchema>; // NumberType is `number`

const objectSchema = val.object({
  name: val.string(),
  age: val.number(),
});
type ObjectType = InferSchema<typeof objectSchema>; // ObjectType is { name: string; age: number }

Type parameters

  • T: BaseSchema<any>

    A schema extending BaseSchema.

SchemaMap

SchemaMap: Record<string, BaseSchema<any>>

A map of property names to their respective schemas.

This type is used to define the structure of an object schema, where each key corresponds to a property name and the value is a schema that validates the property's value.

example
const schemaMap: SchemaMap = {
  name: val.string(),
  age: val.number(),
  isActive: val.boolean(),
};

const objectSchema = val.object(schemaMap);
objectSchema.validate({ name: "John", age: 30, isActive: true }); // Passes validation

Val

Val: { array: <T>(elementSchema: BaseSchema<T>) => ArraySchema<T>; boolean: () => BooleanSchema; coerce: { boolean: () => BaseSchema<boolean>; date: () => BaseSchema<Date>; number: () => BaseSchema<number>; string: () => BaseSchema<string> }; date: () => DateSchema; email: () => StringSchema; enum: <T>(...values: T[]) => EnumSchema<T>; isoDate: () => StringSchema; number: () => NumberSchema; object: <T>(shape: T) => ObjectSchema<T>; string: () => StringSchema; url: () => StringSchema; uuid: () => StringSchema }

The Val type defines the contract for the val API, which provides methods for creating and validating schemas. This API is designed to simplify schema creation and validation for various data types.

Type declaration

  • array: <T>(elementSchema: BaseSchema<T>) => ArraySchema<T>

    Creates an array schema.

    param

    A schema defining the type of elements in the array.

    returns

    A schema that validates arrays of the specified element type.

    example
    const schema = val.array(val.string());
    schema.validate(["apple", "banana"]); // Passes
    schema.validate(["apple", 123]); // Throws ValidationError
    // Example error message:
    // "Array Failed Validation for: ✖ Expected a string, but received a number → at index 1"
  • boolean: () => BooleanSchema

    Creates a boolean schema.

    returns

    A schema that validates boolean values.

    example
    const schema = val.boolean();
    schema.validate(true); // Passes
    schema.validate("not a boolean"); // Throws ValidationError
    // Example error message:
    // "Boolean Failed Validation for: ✖ Expected a boolean value, but received a string."
  • coerce: { boolean: () => BaseSchema<boolean>; date: () => BaseSchema<Date>; number: () => BaseSchema<number>; string: () => BaseSchema<string> }

    Provides coercion-based schemas for transforming input data into the desired type.

    • boolean: () => BaseSchema<boolean>

      Creates a boolean schema that coerces input into a boolean before validation.

      returns

      A schema that coerces and validates boolean values.

      example
      const schema = val.coerce.boolean();
      schema.validate("true"); // Coerces "true" into true and validates it
    • date: () => BaseSchema<Date>

      Creates a date schema that coerces input into a date before validation.

      returns

      A schema that coerces and validates date values.

      example
      const schema = val.coerce.date();
      schema.validate("2025-04-18T00:00:00Z"); // Coerces and validates the date
    • number: () => BaseSchema<number>

      Creates a number schema that coerces input into a number before validation.

      returns

      A schema that coerces and validates number values.

      example
      const schema = val.coerce.number();
      schema.validate("42"); // Coerces "42" into 42 and validates it
    • string: () => BaseSchema<string>

      Creates a string schema that coerces input into a string before validation.

      returns

      A schema that coerces and validates string values.

      example
      const schema = val.coerce.string();
      schema.validate(123); // Coerces 123 into "123" and validates it
  • date: () => DateSchema

    Creates a date schema.

    returns

    A schema that validates date values.

    example
    const schema = val.date();
    schema.validate(new Date()); // Passes
    schema.validate("not a date"); // Throws ValidationError
    // Example error message:
    // "Date Failed Validation for: ✖ Expected a valid date, but received a string."
  • email: () => StringSchema

    Creates a schema that validates email addresses.

    returns

    A schema that validates email strings.

    example
    const schema = val.email();
    schema.validate("test@example.com"); // Passes
    schema.validate("invalid-email"); // Throws ValidationError
    // Example error message:
    // "String Failed Validation for: ✖ Expected a valid email address, but received 'invalid-email'."
  • enum: <T>(...values: T[]) => EnumSchema<T>

    Creates an enum schema.

    param

    The allowed values for the enum.

    returns

    A schema that validates enum values.

    example
    const schema = val.enum("red", "green", "blue");
    schema.validate("red"); // Passes
    schema.validate("yellow"); // Throws ValidationError
    // Example error message:
    // "Enum Failed Validation for: ✖ Expected one of ['red', 'green', 'blue'], but received 'yellow'."
      • Type parameters

        • T: string | number

        Parameters

        • Rest ...values: T[]

        Returns EnumSchema<T>

  • isoDate: () => StringSchema

    Creates a schema that validates ISO date strings.

    returns

    A schema that validates ISO date strings.

    example
    const schema = val.isoDate();
    schema.validate("2025-04-18T00:00:00Z"); // Passes
    schema.validate("invalid-date"); // Throws ValidationError
    // Example error message:
    // "String Failed Validation for: ✖ Expected a valid ISO 8601 date, but received 'invalid-date'."
  • number: () => NumberSchema

    Creates a number schema.

    returns

    A schema that validates number values.

    example
    const schema = val.number();
    schema.validate(42); // Passes
    schema.validate("not a number"); // Throws ValidationError
    // Example error message:
    // "Number Failed Validation for: ✖ Expected a number, but received a string."
  • object: <T>(shape: T) => ObjectSchema<T>

    Creates an object schema.

    param

    An object defining the shape of the schema.

    returns

    A schema that validates objects matching the provided shape.

    example
    const schema = val.object({
      id: val.number(),
      name: val.string(),
      isActive: val.boolean(),
    });
    
    const data = { id: 1, name: "John", isActive: true };
    schema.validate(data); // Passes
    
    const invalidData = { id: "1", name: "Jo", isActive: "yes" };
    schema.validate(invalidData); // Throws ValidationError
    // Example error message:
    // "Validation failed for property 'id': ✖ Expected a number, but received a string → at id"
    // "Validation failed for property 'name': ✖ String is too short. Minimum length is 3 → at name"
    // "Validation failed for property 'isActive': ✖ Expected a boolean, but received a string → at isActive"
  • string: () => StringSchema

    Creates a string schema.

    returns

    A schema that validates string values.

    example
    const schema = val.string();
    schema.validate("hello"); // Passes
    schema.validate(123); // Throws ValidationError
    // Example error message:
    // "String Failed Validation for: ✖ Expected a string, but received a number."
  • url: () => StringSchema

    Creates a schema that validates URLs.

    returns

    A schema that validates URL strings.

    example
    const schema = val.url();
    schema.validate("https://example.com"); // Passes
    schema.validate("invalid-url"); // Throws ValidationError
    // Example error message:
    // "String Failed Validation for: ✖ Expected a valid URL, but received 'invalid-url'."
  • uuid: () => StringSchema

    Creates a schema that validates UUIDs.

    returns

    A schema that validates UUID strings.

    example
    const schema = val.uuid();
    schema.validate("550e8400-e29b-41d4-a716-446655440000"); // Passes
    schema.validate("invalid-uuid"); // Throws ValidationError
    // Example error message:
    // "String Failed Validation for: ✖ Expected a valid UUID, but received 'invalid-uuid'."

Variables

Const regexes

regexes: { html5Email: RegExp; rfc5322Email: RegExp; unicodeEmail: RegExp }

Type declaration

  • html5Email: RegExp
  • rfc5322Email: RegExp
  • unicodeEmail: RegExp

Generated using TypeDoc