conf-validator
v1.1.3
Published
configuration validator
Downloads
308
Readme
configurationValidator
Example
//require package
var validate = require('conf-validator')
//create schema that you want to validate
let schema = [{
name: "name", // ชื่อที่จะแสดงใน log กรณี validate ไม่ผ่าน
value: object, // object ที่ต้องการ validate
schema: [{
config_path: "" //path ที่เป็น array ระบุ/ไม่ระบุ index ก็ได้ (ถ้าไม่ระบุต้องวนทุก array)ต้องมีพารามิเตอร์ตัวนี้ (M)
min: int //type string or numberic or alphanumberic == length, int or double==value มีหรือไม่มีพารามิเตอร์ตัวนี้ (O)
max: int //type string or numberic or alphanumberic == length, int or double==value มีหรือไม่มีพารามิเตอร์ตัวนี้ (O)
eq: int //type string or numberic or alphanumberic == length, int or double==value มีหรือไม่มีพารามิเตอร์ตัวนี้ (O)
array: $value //true/false, //default == false
required_attr: $value //true/false, true == ต้องมี attribute //default == true
required_val: $value //true/false, true == ต้องมี value มา ห้ามว่าง //default == true
type: //numberic, alphanumberic, integer, string, boolean , regexp , condition , double , object
regexp: "[1-9]", //ถ้า type เป็น regexp จะต้องมีพารามิเตอร์นี้
condition: fn( object, key, value) //ถ้า type เป็น condition จะต้องมีพารามิเตอร์นี้
}]
}]
//call function validator
let isCorrect = validate.validator(schema) // return boolean
ตัวอย่าง 1
//object ที่ต้องการ validate
let conf1 = {
"apps": [{
"script": "index.js",
"name": "cauldron",
"env": {
"node_name": "cauldron",
"app_host": "0.0.0.0",
"app_port": "3000",
"use_https": false
}
}]
}
//schema สำหรับ validate object
const schema = [{
"name": "pm2.json",
"value": conf1,
schema: [{
"config_path": "apps[0].script",
"array": false,
"required_attr": true,
"required_val": true,
"type": "string"
}, {
"config_path": "apps[0].name",
"array": false,
"required_attr": true,
"required_val": true,
"type": "string"
}, {
"config_path": "apps[0].env",
"array": false,
"required_attr": true,
"required_val": true,
"type": "object"
} , {
"config_path": "apps[0].env.node_name",
"array": false,
"required_attr": true,
"required_val": true,
"type": "string"
}, {
"config_path": "apps[0].env.app_host",
"array": false,
"required_attr": true,
"required_val": true,
"type": "string"
}, {
"config_path": "apps[0].env.app_port",
"array": false,
"required_attr": true,
"required_val": true,
"type": "string",
"eq" : 4
}, {
"config_path": "apps[0].env.use_https",
"array": false,
"required_attr": true,
"required_val": true,
"type": "boolean"
}]
}]
var validate = require('configurationValidator')
let isCorrect = validate.validator(schema) // return boolean
ตัวอย่าง 2
let conf2 = {
"apps": [{
"script": "index.js",
"name": "cauldron",
"env": {
"node_name": "cauldron",
"app_host": "0.0.0.0",
"app_port": 3000,
"use_https": false
}
}]
}
const schema = [{
"name": "pm2.json",
"value": conf2,
schema: [{
"config_path": "apps[0].script",
}, {
"config_path": "apps[0].name",
}, {
"config_path": "apps[0].env",
"type": "object"
}, {
"config_path": "apps[0].env.node_name",
}, {
"config_path": "apps[0].env.app_host",
}, {
"config_path": "apps[0].env.app_port",
"type": "integer"
}, {
"config_path": "apps[0].env.use_https",
"type": "boolean"
}]
}]
var validate = require('configurationValidator')
let isCorrect = validate.validator(schema, function (txt) { // callback return text log
console.log(txt)
})
ตัวอย่าง 3
let conf3 = {
"apps": [{
"script": "index.js",
"name": "cauldron",
"env": {
"node_name": "cauldron",
"app_host": "0.0.0.0",
"app_port": "3000",
"use_https": false
}
},
{
"script": "index.js",
"name": "validate",
"env": {
"node_name": "validate",
"app_host": "0.0.0.0",
"app_port": "4000",
"use_https": true
}
}]
}
const schema = [{
"name": "pm2.json",
"value": conf3,
schema: [{
"config_path": "apps[].script",
"array": false,
"required_attr": true,
"required_val": true,
"type": "string"
}, {
"config_path": "apps[].name",
"array": false,
"required_attr": true,
"required_val": true,
"type": "string"
}, {
"config_path": "apps[].env",
"array": false,
"required_attr": true,
"required_val": true,
"type": "object"
} , {
"config_path": "apps[].env.node_name",
"array": false,
"required_attr": true,
"required_val": true,
"type": "string"
}, {
"config_path": "apps[].env.app_host",
"array": false,
"required_attr": true,
"required_val": true,
"type": "string"
}, {
"config_path": "apps[].env.app_port",
"array": false,
"required_attr": true,
"required_val": true,
"type": "regexp",
"regexp" : "\\d{4}"
}, {
"config_path": "apps[].env.use_https",
"array": false,
"required_attr": true,
"required_val": true,
"type": "boolean"
}]
}]
var validate = require('configurationValidator')
let isCorrect = validate.validator(schema)
history 1.0.0 init version 1.0.1 add function buildPM2ENVObj() 1.0.2 fixed function buildPM2ENVObj() 1.0.3 change readme 1.0.4 fixed buildPM2ENVObj() build log => commonLog 1.0.8 fixed isRequireAttr() 1.0.9,1.0.10 validate ip (domain & ip & wildcard) 1.0.11 fixed bug display error 1.1.1 add redis value to conn_type (type.json) 1.1.1 add dependencies (ajv) 1.1.3 add conn_type smtp