@ennis/valid
Simple object validation. Leverages the javascript global type constructors for easier composition. Expects JSON-like values (doesn't mess with functions, dates, etc).
Install
npm i @ennis/valid
Usage
const Val = require('@ennis/valid');
let schema = {
shouldBeArray: Array,
hopeItsANumber: Number,
nestedObject: {
inside: String,
},
arraysToo: [
{eachOne: String}
],
stringOrNum: Val.or(String, Number),
intsAreAnnoying: Number.isInteger,
upToYou: Val.optional(String)
}
Val.validate(schema, {
shouldBeArray: ['hi'],
hopeItsANumber: 13.5,
nestedObject: {
inside: 'good so far'
},
arraysToo: [
{eachOne: 'foo'},
{eachOne: 'bar'}
],
stringOrNum: 'string it is'
intsAreAnnoying: 12
});
// returns: {valid: true, errors: []}
Val.validate({
name: String,
age: val => val > 20
}, {
name: null,
age: 14
});
/* returns: {
valid: false,
errors: [
".name: expected string, got null",
".age: integer did not pass validator function"
]
}*/
Types
All valid types and definitions are shown here:
type | valid definitions |
---|---|
object |
'object' , Object
|
array |
'array' , Array
|
string |
'string' , String
|
boolean |
'boolean' , Boolean
|
number |
'number' , Number
|
integer |
'integer' , Number.isInteger
|
null |
'null' , null
|
exists (not null/undefined) | 'exists' |
validator function | value => true/false |
API
.validate(schema, inquestion)
Recursively travels through an object or array to ensure all values are present and of the proper type. Accepts type definitions or validator functions. Schemas can include a single definition as the first item of an array. See usage example
Returns an object:
{
valid: true || false,
errors: ['human readable invalid features']
}
.isInvalid(typeDefinition, value)
Atomic function of .validate(). Just checks the value against the type definition. Returns false
or a string explaining the fault.
For example:
const {isInvalid } = require('@ennis/valid');
isInvalid(String, 13);
// returns 'expected string, got: integer'
isInvalid(Number, 13);
// returns false
Type operators
.optional(type)
Modify a type definition to make it optional. Present values of another type will be invalid.
For example:
const {optional, isInvalid } = require('@ennis/valid');
isInvalid(optional(String), null)
// false;
isInvalid(optional(String), 13)
// 'expected optional string, got: integer'
.notEmpty(arrayDefinition)
By default an empty array, even if it has a defined nested structure in the schema, is valid when its empty. This ensures there is atleast one item in the array. Has nonEmpty()
alias.
For example:
const {validate, notEmpty} = Val;
let schema = {
users: notEmpty([
{id: Number, name: String}
])
}
validate(schema, {users: []})
// {valid: false, errors: ['.users: expected non empty array, got: array']}
validate(schema, {users: [
{id: 1, name: 'John'},
{id: 2, name: 'Jane'}
]})
// {valid: true, errors: []}
.or(...types)
Accepts a list of type definitions (or a single aray). A present value of any of the types will be valid. Nested objects/arrays won't work.
For example:
const {or, isInvalid } = require('@ennis/valid');
isInvalid(or(String, Object), 15)
// 'expect string or object, got: integer';
isInvalid(or(String, Number), 13)
// false