PolyVal supports four basic field types: string, number, boolean, and date. Each type has its own set of validation rules that you can apply.
String fields
String fields support the most comprehensive set of validation rules, including length constraints, format validation, and pattern matching.
Basic string validation
const schema = {
username: {
type: 'string' ,
required: true ,
min: 3 ,
max: 20
}
};
Length validation
You can validate string length using min, max, and length rules:
Minimum length
Maximum length
Exact length
const schema = {
password: {
type: 'string' ,
required: true ,
min: 8 // At least 8 characters
}
};
PolyVal provides built-in email format validation:
const schema = {
email: {
type: 'string' ,
required: true ,
email: true // Validates email format
}
};
The email validator uses the pattern /^[^\s@]+@[^\s@]+\.[^\s@]+$/ to validate email addresses.
Pattern matching
Use regular expressions to validate custom patterns:
const schema = {
username: {
type: 'string' ,
required: true ,
min: 3 ,
max: 20 ,
regex: '^[a-zA-Z0-9_]+$' // Only alphanumeric and underscore
},
password: {
type: 'string' ,
required: true ,
min: 8 ,
// Strong password pattern
regex: '^(?=.*[a-z])(?=.*[A-Z])(?=.* \\ d)(?=.*[@$!%*?&])[A-Za-z \\ d@$!%*?&]+$'
}
};
The regex rule accepts a string pattern, not a RegExp object. Remember to escape special characters.
For advanced patterns like URLs, UUIDs, phone numbers, or other formats, use the regex rule with an appropriate pattern.
Number fields
Number fields support minimum and maximum value validation:
const schema = {
age: {
type: 'number' ,
required: true ,
min: 18 , // Minimum value
max: 120 // Maximum value
},
price: {
type: 'number' ,
min: 0 // No negative values
}
};
For number fields, min and max validate the numeric value, not the length.
Boolean fields
Boolean fields can be required and optionally validated for specific values:
const schema = {
acceptTerms: {
type: 'boolean' ,
required: true ,
equals: true // Must be true (checked)
},
newsletter: {
type: 'boolean' ,
required: false // Optional checkbox
}
};
Use equals: true for checkboxes that must be checked, like terms and conditions.
Date fields
Date fields support type validation to ensure the value is a Date object:
const schema = {
birthDate: {
type: 'date' ,
required: true
}
};
const data = {
birthDate: new Date ( '1990-01-01' )
};
const errors = validate ( schema , data , { lang: 'en' });
Date values must be JavaScript Date objects, not strings or timestamps. Currently, min/max date validation is not implemented in the active validation engine.
Complete example
Here’s a complete example from the PolyVal source code demonstrating all field types:
import { validate , SimpleValidationSchema } from 'polyval' ;
const userRegistrationSchema : SimpleValidationSchema = {
username: {
type: 'string' ,
required: true ,
min: 3 ,
max: 20 ,
regex: '^[a-zA-Z0-9_]+$'
},
email: {
type: 'string' ,
required: true ,
email: true
},
password: {
type: 'string' ,
required: true ,
min: 8 ,
regex: '^(?=.*[a-z])(?=.*[A-Z])(?=.* \\ d)(?=.*[@$!%*?&])[A-Za-z \\ d@$!%*?&]+$'
},
age: {
type: 'number' ,
min: 18
},
acceptTerms: {
type: 'boolean' ,
required: true ,
equals: true
}
};
const userData = {
username: 'johndoe' ,
email: '[email protected] ' ,
password: 'Secure1@Password' ,
age: 25 ,
acceptTerms: true
};
const errors = validate ( userRegistrationSchema , userData , { lang: 'en' });
if ( errors . length === 0 ) {
console . log ( 'Validation successful!' );
}
Type validation
All fields automatically validate their type. If you provide a value that doesn’t match the expected type, you’ll receive an invalid_type error:
const schema = {
age: {
type: 'number' ,
min: 18
}
};
// This will fail with "Invalid type" error
const errors = validate ( schema , { age: '25' }, { lang: 'en' });
// Username: Invalid type
Type validation occurs before any other validation rules are checked.