@elpandev/validator
v1.0.9
Published
validator
Readme
@elpandev/validator
A powerful TypeScript validation library for complex payloads with nested objects, arrays, and multiple validation rules. Perfect for API request validation, form data validation, and complex data structure validation.
Features
- ✅ Nested Object Validation: Validate deeply nested objects with ease
- ✅ Array Validation: Support for validating arrays and array elements
- ✅ Multiple Rules: Apply multiple validation rules to single fields
- ✅ Custom Messages: Customizable error messages for each rule
- ✅ TypeScript First: Full TypeScript support with detailed type definitions
- ✅ Flexible Rules: Support for simple values, objects, and arrays in the same schema
- ✅ Bail Strategy: Optional early exit on first validation failure
Key Benefits
- Enterprise Ready: Built for complex validation scenarios with type safety
- Developer Friendly: Intuitive API with excellent TypeScript support
- Highly Extensible: Easy to add custom validation rules
- Performance Optimized: Efficient validation with minimal overhead
- Comprehensive: Handles all common validation scenarios and edge cases
Installation
npm i @elpandev/validatorQuick Start
import { validate_payload, required, email, min } from '@elpandev/validator'
const payload = {
name: 'John',
email: '[email protected]',
age: 25
}
const rules = {
name: { required },
email: { required, email },
age: { required, min: min(18) }
}
const errors = validate_payload({ payload, rules })
// errors will be an empty object if validation passes
// or contain error messages for failed validationsUsage
import { array, integer, IPMessages, IPRules, max, min, required, some, validate_payload } from '@elpandev/validator'
const payload = {
id: 987,
status: 'active',
info: {
level: 3,
tags: ['new', 'urgent']
},
preferences: null,
meta: ['1', 1],
labels: []
}
const rules = {
id: { required, integer, min: min(1), max: max(100) },
status: { required, some: some(['active', 'inactive']) },
info: {
level: { required: integer, max: max(10) },
tags: [
{ required, array, min: min(2) },
{ required, some: some(['new', 'old']) }
]
},
preferences: { required },
meta: [ {}, { required, integer } ],
labels: [ { required, array, min: min(1) } ],
}
const messages = {
id: { max: 'id max' },
preferences: { required: 'preferences required' },
info: { tags: [ {}, { some: 'info.tags some' } ] },
meta: [ {}, { integer: 'meta integer' } ],
}
const errors = validate_payload({ payload, rules, messages })
// {
// id: { max: 'id max' },
// info: { tags: { '1': { some: 'info.tags some' } } },
// preferences: { required: 'preferences required' },
// meta: { '0': { integer: 'meta integer' } },
// labels: { min: 'min' }
// }Available Validation Rules
required- Field must be present and not emptyemail- Valid email formatinteger- Integer number validationnumeric- Numeric value validationmin/max- Minimum and maximum value constraintsmin_length/max_length- String length constraintsarray- Array type validationsome- Value must be one of the allowed valuesstring- String type validationboolean- Boolean type validation- And many more...
TypeScript Support
Full TypeScript support with comprehensive type definitions for:
- Validation rules structure
- Error messages format
- Nested object validation
- Array validation patterns
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - feel free to use in commercial projects.
Keywords: validation, validator, typescript, payload, form-validation, api-validation, data-validation, schema-validation, nested-validation, array-validation
