Validation

Declarative Auth Forms uses Zod for powerful, type-safe validation with automatic error messages.

Built-in Validation

Each field type comes with sensible default validation:

  • Email - Validates proper email format
  • Password - Enforces minimum length requirements
  • Phone - Validates phone number format
  • Required - Ensures field is not empty

Required Fields

Mark fields as required using the required property:

{
  "name": "email",
  "type": "email",
  "label": "Email Address",
  "required": true
}

String Length Validation

Control minimum and maximum length for text inputs:

{
  "name": "password",
  "type": "password",
  "label": "Password",
  "required": true,
  "minLength": 8,
  "maxLength": 128
}

Pattern Matching

Use regex patterns for custom validation:

{
  "name": "username",
  "type": "text",
  "label": "Username",
  "pattern": "^[a-zA-Z0-9_]+$",
  "patternMessage": "Username can only contain letters, numbers, and underscores"
}

Custom Error Messages

Override default error messages with custom text:

{
  "name": "email",
  "type": "email",
  "label": "Email Address",
  "required": true,
  "errorMessages": {
    "required": "Please enter your email address",
    "invalid": "Please enter a valid email address"
  }
}

Password Confirmation

Validate that two password fields match:

{
  "fields": [
    {
      "name": "password",
      "type": "password",
      "label": "Password",
      "required": true,
      "minLength": 8
    },
    {
      "name": "confirmPassword",
      "type": "password",
      "label": "Confirm Password",
      "required": true,
      "matchField": "password",
      "matchMessage": "Passwords do not match"
    }
  ]
}

Real-time Validation

Validation runs automatically as users type, providing immediate feedback. Error messages appear below fields when validation fails.

Server-side Validation

While client-side validation provides instant feedback, always validate on the server as well. Better Auth handles server-side validation automatically when you submit the form.