orange-dragonfly-validator
v0.8.0
Published
Library for input parameters validation
Downloads
51
Maintainers
Readme
Orange Dragonfly Validator
One day Orange Dragonfly will become a NodeJS framework. For now I'm starting to publish its components.
This library is created for input parameters' validation.
How it works?
You have input in some object (Input). You have another object with schema of allowed input (Schema).
Schema describes available Input and there are any issues throwing an exception which contains information about the errors in the Input.
Simple example which explains the idea
const validate = require("orange-dragonfly-validator")
const rules = {
"name": {
"type": "string",
"pattern": /^[A-Z]([a-z]+)$/
},
"position": {
"type": "string"
},
"term_ends": {
"type": "integer",
"min": 2025
}
}
function f(input) {
try {
validate(rules, input)
console.log(`${input.name}'s job as ${input.position} ends in ${input.term_ends}`)
} catch (e) {
console.error(e.message, e.info)
}
}
f({
"name": "Donald",
"position": "President of the United States",
"term_ends": 2021
})
// Output: "Validation failed { term_ends: [ 'Minimal value (length) is 2025. 2021 provided' ] }"
f({
"name": "Donald",
"position": "President of the United States",
"term_ends": 2025
})
// Output: "Donald's job as President of the United States ends in 2025"
Configuration
Rule definition
There is no required params in rule's definition.
- type (
string
orarray
): describes allowed types of the parameter. Allowed values:string
,number
,integer
,array
,object
,boolean
,null
. - in (
array
): describes allowed values. - in:public (
boolean
orarray
): iftrue
error message ofin
property will have list of available values. Ifarray
is provided if will be provided in error message ofin
instead ofin
values. For example it may be used if some of available values is deprecated and should not be exposed to users. - min (
integer
): minimal (length of) value (applicable forinteger
,number
,string
andarray
values). - max (
integer
): maximal (length of) value (applicable forinteger
,number
,string
andarray
values). - required (
boolean
): show is value required or not. - pattern (
RegExp
): RegExp object. - default (any type): default value. It will ve added to the Input if it is not provided.
- children (
object
): description of children value (applicable forarray
andobject
).- # (rule object): rule for
object
key validation. Applicable for root of the schema as well. - * (rule object): rule for all values of
object
orarray
. Applicable for root of the schema as well. - %key% (rule object): rule for specific
object
key's value. Applicable for root of the schema as well (it is how you define rules). - @ (
object
): options. Applicable for root of the schema as well.- strict (
boolean
): in strict mode validator does not allow in Input keys not defined in Rules (default istrue
, but it can also be overridden inoptions
argument ofvalidate
function)
- strict (
- # (rule object): rule for
- transform (
function
): transforms value for validation - apply_transformed (
boolean
): iftrue
, replaces original value with value returned by function provided astransform
parameter - per_type (
object
): custom rules for the specific type (makes sense if multiple types are allowed). Acceptable parameters:in
,in:public
,min
,max
,pattern
,special
,transform
,apply_transformed
,children