or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

error-handling.mdindex.mdmessages.mdrules.mdschema.mdvalidation-types.md
tile.json

validation-types.mddocs/

Validation Types

Built-in validation support for all common data types including strings, numbers, arrays, objects, and specialized formats like email and URL.

Capabilities

Rule Types

All supported validation types for the type property in rules.

type RuleType = 
  | "string"    // Text validation with length and pattern support
  | "number"    // Numeric validation with range support  
  | "boolean"   // Boolean type validation
  | "method"    // Function type validation
  | "regexp"    // RegExp object validation
  | "integer"   // Integer number validation
  | "float"     // Float number validation
  | "array"     // Array validation with length support
  | "object"    // Object type validation
  | "enum"      // Enumeration validation
  | "date"      // Date object validation
  | "url"       // URL format validation
  | "hex"       // Hexadecimal format validation
  | "email"     // Email format validation
  | "pattern"   // Pattern matching validation
  | "any";      // Accept any type

String Type Validation

Text validation with comprehensive length, pattern, and whitespace checking.

Configuration Options:

  • min - Minimum string length
  • max - Maximum string length
  • len - Exact string length
  • pattern - Regular expression pattern
  • whitespace - Reject whitespace-only strings

Usage Examples:

const rules = {
  // Basic string validation
  title: { type: "string", required: true },
  
  // Length constraints
  username: { type: "string", min: 3, max: 20 },
  
  // Exact length
  zipCode: { type: "string", len: 5 },
  
  // Pattern matching
  slug: { 
    type: "string", 
    pattern: /^[a-z0-9-]+$/ 
  },
  
  // Whitespace validation
  comment: { 
    type: "string", 
    whitespace: true, // Reject empty or whitespace-only
    min: 1
  }
};

Number Type Validation

Numeric validation with range checking and type coercion.

Configuration Options:

  • min - Minimum numeric value
  • max - Maximum numeric value
  • len - Exact numeric value (equality check)

Usage Examples:

const rules = {
  // Basic number validation
  age: { type: "number", required: true },
  
  // Range constraints
  score: { type: "number", min: 0, max: 100 },
  
  // Exact value
  magicNumber: { type: "number", len: 42 },
  
  // Decimal numbers
  price: { type: "number", min: 0.01 }
};

Integer Type Validation

Validates that numbers are integers (whole numbers).

Usage Examples:

const rules = {
  // Basic integer validation
  count: { type: "integer", min: 0 },
  
  // Integer range
  rating: { type: "integer", min: 1, max: 5 }
};

Float Type Validation

Validates floating-point numbers.

Usage Examples:

const rules = {
  // Float validation
  temperature: { type: "float" },
  
  // Float range
  percentage: { type: "float", min: 0.0, max: 100.0 }
};

Boolean Type Validation

Validates boolean values (true/false).

Usage Examples:

const rules = {
  // Boolean validation
  isActive: { type: "boolean", required: true },
  
  // Optional boolean
  newsletter: { type: "boolean" }
};

Array Type Validation

Array validation with length constraints and element validation.

Configuration Options:

  • min - Minimum array length
  • max - Maximum array length
  • len - Exact array length
  • defaultField - Rule applied to all array elements

Usage Examples:

const rules = {
  // Basic array validation
  tags: { type: "array", required: true },
  
  // Length constraints
  items: { type: "array", min: 1, max: 10 },
  
  // Exact length
  coordinates: { type: "array", len: 2 },
  
  // Element validation
  emails: {
    type: "array",
    defaultField: { type: "email" },
    min: 1
  },
  
  // Complex element validation
  users: {
    type: "array",
    defaultField: {
      type: "object",
      fields: {
        name: { type: "string", required: true },
        age: { type: "number", min: 0 }
      }
    }
  }
};

Object Type Validation

Object validation with nested field validation.

Configuration Options:

  • fields - Specific validation rules for object properties
  • defaultField - Rule applied to all object properties

Usage Examples:

const rules = {
  // Basic object validation
  profile: { type: "object", required: true },
  
  // Nested field validation
  user: {
    type: "object",
    fields: {
      name: { type: "string", required: true },
      email: { type: "email", required: true },
      settings: {
        type: "object",
        fields: {
          theme: { type: "string", enum: ["light", "dark"] },
          notifications: { type: "boolean" }
        }
      }
    }
  },
  
  // Dynamic object properties
  metadata: {
    type: "object",
    defaultField: { type: "string" }
  }
};

Enum Type Validation

Validates that values match one of the specified allowed values.

Configuration Options:

  • enum - Array of allowed values

Usage Examples:

const rules = {
  // String enumeration
  status: { 
    type: "enum", 
    enum: ["active", "inactive", "pending"] 
  },
  
  // Number enumeration
  priority: { 
    type: "enum", 
    enum: [1, 2, 3, 4, 5] 
  },
  
  // Mixed type enumeration
  value: { 
    type: "enum", 
    enum: ["yes", "no", true, false, null] 
  }
};

Date Type Validation

Validates Date objects and date strings.

Usage Examples:

const rules = {
  // Date object validation
  createdAt: { type: "date", required: true },
  
  // Date string validation (will be parsed)
  birthday: { type: "date" },
  
  // Date with custom validation
  eventDate: {
    type: "date",
    validator: (rule, value, callback) => {
      const date = new Date(value);
      const now = new Date();
      if (date < now) {
        callback("Event date must be in the future");
      } else {
        callback();
      }
    }
  }
};

Email Type Validation

Validates email address format using built-in email pattern.

Usage Examples:

const rules = {
  // Basic email validation
  email: { type: "email", required: true },
  
  // Email with additional constraints
  workEmail: {
    type: "email",
    pattern: /@company\.com$/,
    message: "Must be a company email address"
  }
};

URL Type Validation

Validates URL format.

Usage Examples:

const rules = {
  // Basic URL validation
  website: { type: "url" },
  
  // Required URL
  homepage: { type: "url", required: true },
  
  // URL with pattern constraint
  apiEndpoint: {
    type: "url",
    pattern: /^https:\/\//,
    message: "Must be a secure HTTPS URL"
  }
};

Hex Type Validation

Validates hexadecimal format (e.g., color codes).

Usage Examples:

const rules = {
  // Hex color validation
  color: { type: "hex" },
  
  // Required hex value
  backgroundColor: { type: "hex", required: true },
  
  // Hex with length constraint
  shortHex: {
    type: "hex",
    len: 3 // #RGB format
  }
};

RegExp Type Validation

Validates that values are RegExp objects.

Usage Examples:

const rules = {
  // RegExp validation
  pattern: { type: "regexp", required: true },
  
  // Validate user-provided regex
  searchPattern: { type: "regexp" }
};

Method (Function) Type Validation

Validates that values are functions.

Usage Examples:

const rules = {
  // Function validation
  callback: { type: "method", required: true },
  
  // Optional function
  transformer: { type: "method" }
};

Pattern Type Validation

Special pattern-based validation that automatically sets type to "pattern" when a RegExp pattern is provided without an explicit type.

Usage Examples:

const rules = {
  // Pattern validation (type automatically set to "pattern")
  code: { pattern: /^[A-Z]{2}\d{4}$/ },
  
  // Explicit pattern type
  identifier: { 
    type: "pattern", 
    pattern: /^[a-f0-9-]{36}$/ // UUID format
  }
};

Any Type Validation

Accepts any value type without type checking.

Usage Examples:

const rules = {
  // Accept any type
  dynamicValue: { type: "any" },
  
  // Any type with other constraints
  flexibleField: {
    type: "any",
    required: true,
    validator: (rule, value, callback) => {
      // Custom validation logic
      if (value === undefined || value === null) {
        callback("Value cannot be null or undefined");
      } else {
        callback();
      }
    }
  }
};

Type Combinations

Multiple Type Validation

Use arrays of rules to apply different type validations.

Usage Example:

const rules = {
  // Accept string or number
  mixedField: [
    { type: "string", min: 1 },
    { type: "number", min: 0 }
  ],
  
  // Complex multi-type validation
  flexibleId: [
    { type: "string", pattern: /^[a-z0-9-]+$/ },
    { type: "number", min: 1 }
  ]
};

Custom Type Registration

Register custom types for reuse across schemas.

Usage Example:

// Register custom phone type
Schema.register("phone", (rule, value, callback) => {
  const phoneRegex = /^\+?[\d\s\-\(\)]{10,}$/;
  if (!phoneRegex.test(value)) {
    callback("Invalid phone number format");
  } else {
    callback();
  }
});

// Use custom type
const rules = {
  phone: { type: "phone", required: true }
};