0.0.3 • Published 2 years ago

@nslibs/validator v0.0.3

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
2 years ago

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