0.1.5 • Published 3 years ago
ajv-simple-wrapper v0.1.5
ajv-simple-wrapper
An opinionated class wrapper for ajv
that throws errors and is typed.
Usage
Install ajv
and ajv-simple-wrapper
.
npm install ajv ajv-simple-wrapper
Import ajv-simple-wrapper
, initialize, and validate.
import { AjvSimpleWrapper as Ajv } from 'ajv-simple-wrapper';
const schema = {
type: 'object'
properties: {
foo: {
type: 'string'
},
bar: {
type: 'number'
}
}
};
const validator = new Ajv(schema);
try {
const result = validator.validate({
foo: 'a string',
bar: 'not a number!',
});
} catch (err) {
console.error('Validation failed!', err);
}
Error Handling
Alternatively, you can initialize your ajv wrapper with a configuration of
throwOnValidationError: false
to not throw when an error occurs.
import { AjvSimpleWrapper as Ajv } from 'ajv-simple-wrapper';
const schema = {
type: 'object'
properties: {
foo: {
type: 'string'
},
bar: {
type: 'number'
}
}
};
const validator = new Ajv(schema, { throwOnValidationError: false });
const result = validator.validate({
foo: 'a string',
bar: 'not a number!',
});
if (!result) {
// Errors from the last validation will be available on the validator.errors
console.error('Validation failed!', validator.errors);
}
0.2.0-alpha.2
3 years ago
0.2.0-alpha.1
3 years ago
0.2.0-alpha.3
3 years ago
0.1.4
3 years ago
0.1.5
3 years ago
0.1.3
4 years ago
0.1.2
4 years ago
0.1.1
4 years ago
0.1.0
4 years ago