npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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