verif
Secure data validation in JS
Install
npm i verif
const Validator Range =
Usage
Create a validator
const myValidator = 'type': 'object' 'props': 'name': 'type': 'string' 'length': Range3 23 'age': 'type': 'number' 'value': Range5 4000
Test with a validator
myValidator // This will not throw; data is acceptable.
myValidator // This will throw an error:// Error: Number value 4 out of bounds. Expected in [5, 4000]
Validate with a validator
const res = myValidator // This will return information about the validation:/*{ passed: false, message: 'Number value 4 out of bounds. Expected in [5, 4000]', path: '/age/'}*/
Create a range
// new Range(min: number, max: number, inclusive: boolean) 4 7 false// Exclusive range (4, 7) 6 10 true 6 10// Inclusive range [6, 10]
Properties of types
For all validations, a type
must be specified.
number
-
Range
valuePermitted number value range
string
-
Range
lengthPermitted string length range
-
RegEx
testMandatory string regular expression test
array
-
Range
lengthPermitted array length range
-
schema
itemsSchema for item testing
object
-
Object[schema]
propsSchemas for testing individual properties
-
boolean
allowExtraPropsPermit the inclusion of additional properties not defined in
props
Default:
false