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.
Whether the field is required. If true, the field must be present and not empty.
String validations
For strings: minimum length. For numbers: minimum value. For dates: minimum date.
For strings: maximum length. For numbers: maximum value. For dates: maximum date.
Exact string length required.
If true, validates that the string is a valid email address format.
Not yet implemented. Defined in types but not active in the validation engine. Use regex for URL validation.
Not yet implemented. Defined in types but not active in the validation engine. Use regex for UUID validation.
Not yet implemented. Defined in types but not active in the validation engine.
Not yet implemented. Defined in types but not active in the validation engine.
Not yet implemented. Defined in types but not active in the validation engine.
A regex pattern as a string. The field value must match this pattern. Fully implemented.
Not yet implemented. Defined in types but not active in the validation engine.
Not yet implemented. Defined in types but not active in the validation engine.
Not yet implemented. Defined in types but not active in the validation engine. Use regex: '^[0-9]+$' for numeric validation.
Field comparison
For strings/numbers: the name of another field that this field must equal. For booleans: the exact boolean value required (true or false).
The name of another field that this field must not equal.
Custom validators
An array of custom validation functions for advanced validation logic. Show Array element structure
validator
(value: any, data: Record<string, any>) => string | undefined
required
A function that receives the field value and the entire data object. Return an error message string if validation fails, or undefined if validation succeeds.
Optional key to reference this validator in the customMessages object. Allows you to provide custom error messages for this validator.
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
}
};
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.