npm.io
2.0.0 • Published 10 years ago

wadofgum-validation

Licence
MIT
Version
2.0.0
Deps
1
Vulns
2
Weekly
0

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.

Keywords