configuration-validator-sand
v1.0.0
Published
configuration validator
Downloads
3
Readme
configurationValidator
Example
//require package
var validate = require('configurationValidator')
//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() //ถ้า 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)