simple-json-validate v1.0.5
SIMPLE-JSON-VALIDATE
tool for check json input and set json data.
NOTE: This still beta version. Feel free to report bug thank!Getting Started
Installing
npm install simple-json-validate --saveUsage
const validate = require('simple-json-validate');
const pattern = {
"test": {
"id": "required|number|max_length[13]|min_length[13]",
"email": "required|email",
"period": "default[30]",
"value": "required|limit[100]"
}
};
let input = {
"id": "1111111111111",
"email": "freeweed.m@gmail.com",
"value": "10000"
}
let validated = new validate(pattern);
let output = validated.check('test', input);
console.log(output);//{"id":"1111111111111","email":"freeweed.m@gmail.com","value":"100","period":"30","isValid":true}How To use
1. Require SIMPLE-JSON-VALIDATE
const validate = require('simple-json-validate');2. Setup Your Json Pattern
var pattern = {
"first pattern name": {
"json key": "setup format"
},
"second pattern name": {
"json key": "setup format"
}
}
new validate(pattern);3. Call Check Function
let output = validated.check("pattern name", input) NOTE: input can be only JSON Object4. Check Your Input Is Valid Or Not
if(output.isValid){
//awesome code here
}The Format
You can use multiple format with | between format such as
"id": required|number|max_length[13]|min_length[13]Avaliable Format
requireddata is required
numberaccept only number
emailaccept only email
default[...]set default data (This option will use only when input data is invalid or null. replace ... with default data such as default30)
avaliable[...]set data that can be accepted (replace ... with accepted data such as avaliablebanana, weed)
limit[...]what ever input data will set to limit data (replace ... with limit data such as limit100)
max_length[...]maximum length of data can be accepted (replace ... with interger such ass max_length13)
min_length[...]minimum length of data can be accepted (replace ... with interger such as min_length13)
max_value[...]maximum value of data can be accepted (replace ... with interger such as man_value10)
min_value[...]minimum value of data can be accepted (replace ... with interger such as min_value1)
Authors
freeweed
License
This project is licensed under the WTFPL License - see the LICENSE.md file for details
Changelog
1.0.3
Fixed
- some stupid bug
1.0.2
Fixed
validate number bug
TODO
add nested child validate
- add unit test
- improve performance
- add more type