Simple and lightweight javascript object schema validation library.
Description
- Lightweight: when bundled with rollup and terser, the output weight less than 2kB (vs ~167kB for @hapi/joi, 80 times ratio!)
- Modular: easily create new checkers (see here)
- Typescript support
Installation
Garant can be installed using yarn or npm.
npm install garant
# or
yarn add garant
Usage
import {Validator} from 'garant';
// or
const {Validator} = require('garant');
const schema = {
username: {
type: 'string',
required: true
},
email: {
type: 'string',
},
info: {
type: 'object',
children:{
age: {
type: 'number',
},
height: {
type: 'number',
}
}
}
};
const validator = new Validator(schema);
const object = {
username: 'Jane',
email: 'jane@example.com',
info: {
age: 22,
height: 165
}
};
const results = validator.check(object);
// {
// hasError: false,
// messages: [],
// data: {
// username: 'Jane',
// email: 'jane@example.com',
// info: {
// age: 22,
// height: 165
// }
// }
// }
Road map
Required checkerType checkerChildren checkerDefault checker (set default value if undefined)- Improve documentation
- Regex checker
- Length (min, max) checker
- Array content checker
Want to add your checker? Simply create yours in the src/checkers directory and register it in the Validator class. Submit your pull request!
Contribute
Pull requests are welcome ! Feel free to contribute.
Credits
Coded with ❤️ by Corentin Thomasset.
License
This project is under the MIT license.