wadofgum-validation

2.0.0 • Public • Published

wadofgum-validation Build Status

A validation mixin for wadofgum based on joi.

Usage

After extending your model with this mixin, instances of your class will have a validate method. This method returns a promise that resolves when validation is complete, or throws if validation fails.

To provide a schema for validation, assign it to the schema property on your class. Most Joi features are covered, including type coercion, required keys, renaming keys, and more.

const Wadofgum = require('wadofgum');
const Validation = require('wadofgum-validation');
const Joi = require('joi');
 
class Model extends Wadofgum.mixin(Validation) {};
Model.schema = Joi.object({
  name: Joi.string().required(),
  age: Joi.number().integer()
});
 
let model = new Model({ name: 'test', age: '45' });
model.validate().then(function () {
  model.name; // 'test'
  model.age; // 45
});

If the wadofgum-events mixin is also loaded, this module will emit preValidate and postValidate events.

Readme

Keywords

Package Sidebar

Install

npm i wadofgum-validation

Weekly Downloads

2

Version

2.0.0

License

MIT

Last publish

Collaborators

  • fritzy
  • nlf