0.8.0 • Published 3 years ago
orange-dragonfly-validator v0.8.0
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 (
stringorarray): describes allowed types of the parameter. Allowed values:string,number,integer,array,object,boolean,null. - in (
array): describes allowed values. - in:public (
booleanorarray): iftrueerror message ofinproperty will have list of available values. Ifarrayis provided if will be provided in error message ofininstead ofinvalues. 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,stringandarrayvalues). - max (
integer): maximal (length of) value (applicable forinteger,number,stringandarrayvalues). - 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 forarrayandobject).- # (rule object): rule for
objectkey validation. Applicable for root of the schema as well. - * (rule object): rule for all values of
objectorarray. Applicable for root of the schema as well. - %key% (rule object): rule for specific
objectkey'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 inoptionsargument ofvalidatefunction)
- strict (
- # (rule object): rule for
- transform (
function): transforms value for validation - apply_transformed (
boolean): iftrue, replaces original value with value returned by function provided astransformparameter - 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