XXX.js is an error rich schema validation library intended for use in debugging and reverse engineering. It supports a similar syntax supported by Mongoose and Ajv JSON schema validator, and custom options for types listed below. The difference is that for each error, you get an object describing the object path and the context alongside with the error message. This library also supports custom user defined callbacks to validate objects and class instances flexibly.
const validator = new Validator({
type:"object",
required:{
"id":{type:"number"},
"name":{type:"string",match:/^[a-zA-Z]+\ [a-zA-Z]+$/}
},
optional:{
"age":{type:"number", custom:n => n>10},
"buffer":{type:Uint8Array, custom:buff => buff.length<1000},
"houses":{
type:"array",
repeat:true,
pattern:[
{type:"string"},
[{type:"number"},{type:"bigint"}],
"+"
]
}
},
others:{type:"string"}
});
console.log(validator.validate({
id: 53,
name: "Barack Obama",
age: 61,
houses: ["New York",5,3,1,2,"Los Angeles",2,14513451234123n,6,3],
buffer: new Uint8Array(500)
}));
// Result:
// true
console.log(validator.validate({
id: 54,
name: "Noah Jenkins",
age: 6
}));
// Result:
// ValidatorError {
// msg: 'Custom validation failed',
// path: '.age',
// target: 6,
// context: 'n => n>10'
// }
console.log(validator.validate({
id: 55,
name: "Alan Bartlett Shepard Jr."
}));
// Result:
// ValidatorError {
// msg: 'String does not match the pattern',
// path: '.name',
// target: 'Alan Bartlett Shepard Jr.',
// context: /^[a-zA-Z]+\ [a-zA-Z]+$/
// }
console.log(validator.validate({
id: 56,
name: "Ken Wins",
houses: ["Albuquerque",3,2,1,true]
}));
// Result:
// ValidatorError {
// msg: 'Expected a string, but got a boolean instead',
// path: '.houses[4]',
// target: true,
// context: 'string'
// }const validator = new Validator({
type:<type name in string>,
... //other options depending on the types
});Documentation is in WIP, and may not match the implementation
Validator is a constructor for the Validator class. The argument is options | Array<options>
Options contain the option
The validator will check if the input is of type Object regardless of options
options.inclusive: Boolean
Default: false
If set to true, the validator will accept objects with properties that don't appear in options.props_mandatory and options.props_optional, and check it against rules defined in options.props_any
options.props_mandatory: PropertyMap
Default: {}
Look at PropertyMap for the interface definition.
The validator will reject the input object if any of the properties defined in this field are absent. It will then check the object against the rules defined inside this map.
options.props_optional: PropertyMap
Default: {}
Look at PropertyMap for the interface definition.
The validator will check the input object against the rules defined inside this map. If the property in question is absent, the object will be accepted by default.
options.props_any: Rule
Default: []
The validator will check the input object property against this rule if the property does not appear in either options.props_mandatory or options.props_optional.
If this option is available, options.inclusive will automatically be set to true
If left empty the validator will accept all properties that checks against this rule.
Alias for options.props_mandatory
Alias for options.props_optional
Alias for options.props_optional
Alias for options.props_any
The validator will check if the input is of type Array regardless of options
options.maxLength: Number
Default: Infinity
The input array may not exceed this length
options.minLength: Number
Default: -Infinity
The input array may not be shorter than this length
Default: []
Array of Rules. The validator will try to match the input array from the pattern head, and repeat the matches using string suffixes if present with the same rule as regex
options.repeat: Boolean
Default: false
If the pattern is exhausted, the rest of the input array will be matched using repeating the pattern
options.align_end: Boolean
Default: true
The validator will reject if the pattern end does not match the input array end.
options.all: Rule
Default: N/A This will override all the other options. The validator will accept if all of the input array elements match this rule.
The validator will check if the input is of type String regardless of options
Default: /.*/
The validator will reject inputs that don't match this option using String.prototype.match().
options.class: Function
Default: mandatory The validator will reject if the input object is not an instance of the class specified in this field.
options.typename: String
Default: mandatory The validator will reject if the input does not match the type specified in this field.
options.validator: Function
Default: mandatory
The validator will call the function specified in this field, with the object in question as the argument. The function should either return true to accept the object, or throw an error to reject the object.
PropertyMap: Object
An object that contains key value pair that corresponds to the input.
The values should either be an instance of Validator or a valid argument of Validator constructor.
definition
interface PropertyMap {
[key: string]: Rule
}A valid option argument for the Validator constructor.
validator_accept_any: Validator
An instance of Validator that accepts any value