wadofgum-json-schema
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -16,25 +16,21 @@ 'use strict'; | ||
validate () { | ||
validate (cb) { | ||
const self = this; | ||
return new Promise((resolve, reject) => { | ||
if (!this.constructor.meta.has('schema')) { | ||
return cb(new Error('No schema has been provided for model ' + this.constructor.name)); | ||
} | ||
if (!this.constructor.meta.has('schema')) { | ||
return reject(new Error('No schema has been provided for model ' + this.constructor.name)); | ||
} | ||
const schema = this.constructor.meta.get('schema'); | ||
const validator = this.constructor.meta.get('validator'); | ||
const valid = validator.validateSchema(schema); | ||
const schema = this.constructor.meta.get('schema'); | ||
const validator = this.constructor.meta.get('validator'); | ||
const valid = validator.validateSchema(schema); | ||
if (!valid) { | ||
return cb(new Error('Schema name ' + this.constructor.name + ' is not valid')); | ||
} | ||
validator.validate(this, schema, (err, result) => { | ||
if (!valid) { | ||
return reject(new Error('Schema name ' + this.constructor.name + ' is not valid')); | ||
if (err) { | ||
return cb(new Error('Validation has failed with message ' + err[0].message)); | ||
} | ||
validator.validate(self, schema, (err, result) => { | ||
if (err) { | ||
return reject(new Error('Validation has failed with message ' + err[0].message)); | ||
} | ||
return resolve(self); | ||
}); | ||
return cb(null, this); | ||
}); | ||
@@ -41,0 +37,0 @@ }; |
{ | ||
"name": "wadofgum-json-schema", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "json schema validation for wadofgum models", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -7,3 +7,3 @@ ## wadofgum-json-schema [](https://travis-ci.org/simon-pr/wadofgum-json-schema) | ||
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 rejects if validation fails. | ||
After extending your model with this mixin, instances of your class will have a `validate` method which accepts a callback as its only parameter. | ||
@@ -34,7 +34,7 @@ Simply provide a json schema for validation and then assign it to the static `schema` property on your class. | ||
let model = new Model({ name: 'test', age: '45', dateOfBirth: '1975-10-01'}); | ||
model.validate().then(function () { | ||
model.name; // 'test' | ||
model.age; // 45 | ||
model.dateOfBirth; // '1975-10-01' | ||
model.validate((err, result) => { | ||
model.name; // 'test' | ||
model.age; // 45 | ||
model.dateOfBirth; // '1975-10-01' | ||
}); | ||
``` |
@@ -30,3 +30,3 @@ 'use strict'; | ||
user.validate().catch((err) => { | ||
user.validate((err, result) => { | ||
@@ -59,9 +59,8 @@ expect(err).to.exist(); | ||
const user = new User(); | ||
user.validate((err, result) => { | ||
user.validate().catch((err) => { | ||
expect(err).to.exist(); | ||
expect(err.message).to.contain('Schema name'); | ||
done(); | ||
}).catch(done); | ||
}); | ||
@@ -94,4 +93,5 @@ }); | ||
user.validate().then(() => { | ||
user.validate((err, result) => { | ||
expect(err).to.not.exist(); | ||
expect(user.name).to.equal('John'); | ||
@@ -101,6 +101,6 @@ expect(user.age).to.equal(40); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); | ||
it('should fail validation data against a json schema', (done) => { | ||
it('should fail validation with invlaid data against a z-schema', (done) => { | ||
@@ -129,3 +129,3 @@ class User extends Wadofgum.mixin(Validation) {}; | ||
user.validate().catch((err) => { | ||
user.validate((err, result) => { | ||
@@ -135,6 +135,5 @@ expect(err).to.exist(); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); | ||
}); |
6882
140