Skip to main content

Type definition

interface SimpleValidationSchema {
  [fieldName: string]: {
    type: 'string' | 'number' | 'boolean' | 'date';
    required?: boolean;
    min?: number;
    max?: number;
    length?: number;
    email?: boolean;
    url?: boolean;
    uuid?: boolean;
    cuid?: boolean;
    datetime?: boolean;
    ip?: 'v4' | 'v6';
    regex?: string;
    startsWith?: string;
    endsWith?: string;
    numeric?: boolean;
    equals?: string | boolean;
    notEquals?: string;
    customValidators?: Array<{
      validator: (value: any, data: Record<string, any>) => string | undefined;
      messageKey?: string;
    }>;
  };
}
Defines the validation rules for each field in your data object. Each key in the schema corresponds to a field name, and the value describes the validation rules for that field.
Currently implemented features: The type definition above shows all defined properties. However, the current validation engine implements only a subset:
  • String: min, max, length, email, regex
  • Number: min, max
  • Boolean: equals
  • Date: type checking only
  • Field comparison: equals, notEquals
  • Custom validators: fully supported
Properties like url, uuid, cuid, datetime, ip, startsWith, endsWith, and numeric are defined in the TypeScript interface but not yet active in the validation engine.

Field properties

Basic properties

type
'string' | 'number' | 'boolean' | 'date'
required
The expected data type for this field.
required
boolean
default:false
Whether the field is required. If true, the field must be present and not empty.

String validations

min
number
For strings: minimum length. For numbers: minimum value. For dates: minimum date.
max
number
For strings: maximum length. For numbers: maximum value. For dates: maximum date.
length
number
Exact string length required.
email
boolean
If true, validates that the string is a valid email address format.
url
boolean
Not yet implemented. Defined in types but not active in the validation engine. Use regex for URL validation.
uuid
boolean
Not yet implemented. Defined in types but not active in the validation engine. Use regex for UUID validation.
cuid
boolean
Not yet implemented. Defined in types but not active in the validation engine.
datetime
boolean
Not yet implemented. Defined in types but not active in the validation engine.
ip
'v4' | 'v6'
Not yet implemented. Defined in types but not active in the validation engine.
regex
string
A regex pattern as a string. The field value must match this pattern. Fully implemented.
startsWith
string
Not yet implemented. Defined in types but not active in the validation engine.
endsWith
string
Not yet implemented. Defined in types but not active in the validation engine.
numeric
boolean
Not yet implemented. Defined in types but not active in the validation engine. Use regex: '^[0-9]+$' for numeric validation.

Field comparison

equals
string | boolean
For strings/numbers: the name of another field that this field must equal.For booleans: the exact boolean value required (true or false).
notEquals
string
The name of another field that this field must not equal.

Custom validators

customValidators
Array<object>
An array of custom validation functions for advanced validation logic.

Examples

Basic schema

const schema: SimpleValidationSchema = {
  username: {
    type: 'string',
    required: true,
    min: 3,
    max: 20
  },
  email: {
    type: 'string',
    required: true,
    email: true
  },
  age: {
    type: 'number',
    required: true,
    min: 18
  }
};

String format validations

const schema: SimpleValidationSchema = {
  website: {
    type: 'string',
    url: true
  },
  userId: {
    type: 'string',
    uuid: true
  },
  phoneNumber: {
    type: 'string',
    regex: '^\\+?[1-9]\\d{1,14}$'
  },
  zipCode: {
    type: 'string',
    numeric: true,
    length: 5
  }
};

Field comparison

const schema: SimpleValidationSchema = {
  password: {
    type: 'string',
    required: true,
    min: 8
  },
  confirmPassword: {
    type: 'string',
    required: true,
    equals: 'password'
  },
  acceptTerms: {
    type: 'boolean',
    equals: true
  }
};

Custom validators

const schema: SimpleValidationSchema = {
  password: {
    type: 'string',
    required: true,
    customValidators: [
      {
        validator: (value) => {
          if (!/[A-Z]/.test(value)) {
            return 'Must contain at least one uppercase letter';
          }
          return undefined;
        },
        messageKey: 'passwordUppercase'
      },
      {
        validator: (value) => {
          if (!/[0-9]/.test(value)) {
            return 'Must contain at least one number';
          }
          return undefined;
        },
        messageKey: 'passwordNumber'
      }
    ]
  },
  birthYear: {
    type: 'number',
    required: true,
    customValidators: [
      {
        validator: (value) => {
          const currentYear = new Date().getFullYear();
          if (value > currentYear) {
            return 'Birth year cannot be in the future';
          }
          return undefined;
        }
      }
    ]
  }
};
Custom validators receive both the field value and the entire data object, allowing for complex cross-field validations.

Advanced cross-field validation

const schema: SimpleValidationSchema = {
  startDate: {
    type: 'date',
    required: true
  },
  endDate: {
    type: 'date',
    required: true,
    customValidators: [
      {
        validator: (value, data) => {
          if (data.startDate && value < data.startDate) {
            return 'End date must be after start date';
          }
          return undefined;
        },
        messageKey: 'dateRange'
      }
    ]
  }
};
When using regex, provide the pattern as a string, not as a RegExp object. The pattern will be converted to a RegExp internally.

Build docs developers (and LLMs) love