Overview
JSON Forms uses AJV (Another JSON Schema Validator) to validate form data against JSON Schema. Validation happens automatically and error messages are displayed next to the corresponding form controls.Default Validation Setup
JSON Forms creates an AJV instance with the following default configuration:Default Options
allErrors: true- Check all validation rules and collect all errorsverbose: true- Include the schema and data in error messagesstrict: false- Don’t fail on unknown keywordsaddUsedSchema: false- Don’t add schemas to the instance automatically- Includes
ajv-formatsfor format validation (email, date, uri, etc.)
Validate Function
The core validation function:Error Object Structure
AJV returns error objects with the following structure:JSON Schema Validation Keywords
Type Validation
Required Fields
String Validation
Number Validation
Array Validation
Format Validation
ajv-formats):
date- Full date (RFC 3339 section 5.6)time- Time (RFC 3339 section 5.6)date-time- Date and time (RFC 3339)uri- Universal Resource Identifieremail- Email addresshostname- Host nameipv4- IPv4 addressipv6- IPv6 addressregex- Regular expressionuuid- UUIDjson-pointer- JSON Pointer
Enum Validation
Const Validation
Custom AJV Instance
You can provide a custom AJV instance to JSON Forms:Validation Modes
JSON Forms supports different validation modes:ValidateAndShow (Default)
Validates data and shows all errors:ValidateAndHide
Validates data but doesn’t show errors in the UI:NoValidation
Skips validation entirely:Accessing Validation Errors
Validation errors are returned in theonChange callback:
Custom Validators
Adding Custom Keywords
Adding Custom Formats
Async Validation
AJV supports async validation for cases like checking uniqueness:Error Message Customization
Customize error messages using the i18n system. See the Internationalization page for details. Default error message handling:Common Validation Patterns
Password Strength
Conditional Validation (Dependencies)
Cross-Field Validation
Best Practices
- Use JSON Schema validation: Leverage JSON Schema’s built-in validation when possible
- Provide clear error messages: Use i18n to customize error messages for better UX
- Validate early: Set validation mode to show errors as users type
- Keep validation in the schema: Don’t duplicate validation logic in your UI code
- Use formats: Leverage
ajv-formatsfor common patterns like email and date - Test edge cases: Ensure your validation handles null, undefined, and edge values
- Consider performance: Complex validation on large forms can impact performance
- Document custom validators: Clearly document any custom validation keywords or formats