@nslibs/validator

opts.onError(error): Function to handle an error - error: object representing the ajv error. Default throws a validation error with ajv error properties on error. - error.orig_message: The original ajv error message - error.message: A modified error message that includes the name of the data. It is taken from the title property on a schema. - error.*: See the ajv docs for the rest of the properties - opts.isType(name, data): Function to check types

Usage no npm install needed!

<script type="module">
  import nslibsValidator from 'https://cdn.skypack.dev/@nslibs/validator';
</script>

README

Validator

new Validator(opts)

  • opts.onError(error): Function to handle an error
    • error: object representing the ajv error. Default throws a validation error with ajv error properties on error.
      • error.orig_message: The original ajv error message
      • error.message: A modified error message that includes the name of the data. It is taken from the title property on a schema.
      • error.*: See the ajv docs for the rest of the properties
  • opts.isType(name, data): Function to check types

val(data, schema)

  • data: anything to be validated
  • schema: json schema
    • schema.optional:

Custom Schema Keywords

nstype

An enhanced version of type that supports undefined, function, etc. Custom types can also be specified with isType in constructor.

Schema

{ type: ['array', 'string']}

Example

let val = new Validator({
    isType(name, data){
        switch(name){
            case: 'Moment':
                return data instanceof Moment;
        }
    }
})
val(moment_date, { nstype: 'Moment'})
val(moment_date, { nstype: ['Moment']})

Examples

let { Validator } = require('@nslibs/validator');

let v = new Validator();
let val = v.val;

val('hi', {type: 'number'}) // => ValError: Something should be number
val('hi', {type: 'number', title: 'Count'}) // ValError: Count should be a number