imagic-validator
is a simple yet powerful JavaScript validation library. It provides an easy-to-use API for validating various data types, including strings, numbers, arrays, and objects.
You can install imagic-validator
via npm:
npm install imagic-validator
import IMV from 'imagic-validator'
const schema = IMV.string().required().min(5).max(10).pattern(/^[a-zA-Z]+$/)
console.log(schema.validate('Hello')) // true
console.log(schema.errors) // []
console.log(schema.validate('Hi')) // false
console.log(schema.errors) // [ { field: null, code: 'STRING_MIN_LENGTH', message: 'Value must have at least 5 characters, 2 given.' } ]
console.log(schema.validate('HelloWorld!')) // false
console.log(schema.errors) // [ { field: null, code: 'STRING_MAX_LENGTH', message: 'Value must have at most 10 characters, 11 given.' } ]
console.log(schema.validate('Hello123')) // false
console.log(schema.errors) // [ { field: null, code: 'STRING_PATTERN_MISMATCH', message: 'Value fails to match the specified pattern "/^[a-zA-Z]+$/".' } ]
const schema = IMV.number().required().min(10).max(100)
console.log(schema.validate(50)) // true
console.log(schema.errors) // []
console.log(schema.validate(5)) // false
console.log(schema.errors) // [ { field: null, code: 'NUMBER_MIN', message: 'Number must be at least 10.' } ]
console.log(schema.validate(150)) // false
console.log(schema.errors) // [ { field: null, code: 'NUMBER_MAX', message: 'Number must be at most 100.' } ]
const schema = IMV.array().min(2).max(5).unique()
console.log(schema.validate([1, 2, 3])) // true
console.log(schema.errors) // []
console.log(schema.validate([1])) // false
console.log(schema.errors) // [ { field: null, code: 'ARRAY_MIN_LENGTH', message: 'Array must have at least 2 elements, 1 given.' } ]
console.log(schema.validate([1, 2, 2])) // false
console.log(schema.errors) // [ { field: null, code: 'ARRAY_UNIQUE', message: 'Array elements must be unique.' } ]
const schema = IMV.object({
name: IMV.string().required(),
age: IMV.number().min(18)
}).length(2)
console.log(schema.validate({ name: 'John', age: 30 })) // true
console.log(schema.errors) // []
console.log(schema.validate({ name: 'John' })) // false
console.log(schema.errors) // [ { field: null, code: 'OBJECT_KEYS_LENGTH', message: 'Object must have exactly 2 keys, 1 given.' } ]
The library uses Mocha and Chai for testing. To run the tests, use the following command:
npm test
Tests cover the core validation features for strings, numbers, arrays, and objects.
Full documentation and usage examples are available on GitHub.
This project is licensed under the MIT License. See LICENSE for details.
If you have ideas for improvements, please open a pull request or create an issue in the GitHub repository.