wadofgum-json-schema
Advanced tools
Comparing version 0.1.1 to 0.2.0
'use strict'; | ||
const Validate = require('./validate.js'); | ||
const ZSchema = require('z-schema'); | ||
@@ -8,6 +9,7 @@ | ||
class Model extends baseClass { | ||
static set schema (jsonSchema) { | ||
static set schema (rawSchema) { | ||
const schema = jsonSchema; | ||
this.meta.set('schema', schema); | ||
Validate.assert('Schema', rawSchema); | ||
this.meta.set('metaSchema', rawSchema.metaSchema); | ||
this.meta.set('schema', rawSchema.schema); | ||
this.meta.set('validator', new ZSchema()); | ||
@@ -22,3 +24,3 @@ | ||
} | ||
const metaSchema = this.constructor.meta.get('metaSchema'); | ||
const schema = this.constructor.meta.get('schema'); | ||
@@ -29,3 +31,3 @@ const validator = this.constructor.meta.get('validator'); | ||
if (!valid) { | ||
return cb(new Error('Schema name ' + this.constructor.name + ' is not valid')); | ||
return cb(new Error('Schema name ' + metaSchema.name + ' has failed validation with z-schema')); | ||
} | ||
@@ -32,0 +34,0 @@ validator.validate(this, schema, (err, result) => { |
{ | ||
"name": "wadofgum-json-schema", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "json schema validation for wadofgum models", | ||
@@ -30,4 +30,6 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"hoek": "^3.0.1", | ||
"joi": "^7.0.0", | ||
"z-schema": "^3.15.4" | ||
} | ||
} |
@@ -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 which accepts a callback as its only parameter. | ||
After extending your model with this mixin, instances of your class will have a `validate` method which accepts a callback as its only parameter. | ||
@@ -18,15 +18,26 @@ Simply provide a json schema for validation and then assign it to the static `schema` property on your class. | ||
Model.schema = { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string' | ||
}, | ||
age: { | ||
type: 'integer' | ||
}, | ||
dateOfBirth: { | ||
type: 'string', | ||
format: 'date' | ||
metaSchema: { | ||
description: 'Person record schema', | ||
type: 'record', | ||
base: 'entity', | ||
jsonSchema: 'v4', | ||
name: 'person', | ||
version: 1 | ||
}, | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string' | ||
}, | ||
age: { | ||
type: 'integer' | ||
}, | ||
dateOfBirth: { | ||
type: 'string', | ||
format: 'date' | ||
} | ||
} | ||
} | ||
}; | ||
@@ -33,0 +44,0 @@ |
'use strict'; | ||
const Code = require('code'); | ||
const Hoek = require('hoek'); | ||
const Lab = require('lab'); | ||
@@ -8,2 +9,5 @@ const Wadofgum = require('wadofgum'); | ||
// Fixtures | ||
const PersonSchema = require('./fixtures/person.js'); | ||
const PersonData = require('./fixtures/personData.js'); | ||
@@ -20,14 +24,26 @@ // Set-up lab | ||
class User extends Wadofgum.mixin(Validation) {}; | ||
const user = new User(); | ||
expect(user.validate).to.exist(); | ||
class Person extends Wadofgum.mixin(Validation) {}; | ||
const person = new Person(); | ||
expect(person.validate).to.exist(); | ||
done(); | ||
}); | ||
it('should throw if invalid rawSchema used is invalid', (done) => { | ||
class Person extends Wadofgum.mixin(Validation) {}; | ||
const InvalidSchema = Hoek.clone(PersonSchema); | ||
delete InvalidSchema.metaSchema; | ||
expect(() => { | ||
Person.schema = InvalidSchema; | ||
}).to.throw(Error); | ||
done(); | ||
}); | ||
it('errors when attempting to validate a model with no schema', (done) => { | ||
class User extends Wadofgum.mixin(Validation) {}; | ||
const user = new User(); | ||
class Person extends Wadofgum.mixin(Validation) {}; | ||
const person = new Person(); | ||
user.validate((err, result) => { | ||
person.validate((err, result) => { | ||
@@ -42,21 +58,8 @@ expect(err).to.exist(); | ||
class User extends Wadofgum.mixin(Validation) {}; | ||
User.schema = { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
format: 'unknown' | ||
}, | ||
age: { | ||
type: 'integer' | ||
}, | ||
dateOfBirth: { | ||
type: 'string', | ||
format: 'date' | ||
} | ||
} | ||
}; | ||
const user = new User(); | ||
user.validate((err, result) => { | ||
class Person extends Wadofgum.mixin(Validation) {}; | ||
const InvalidSchema = Hoek.clone(PersonSchema); | ||
InvalidSchema.schema.properties.person.properties.salutation.format = 'unknown'; | ||
Person.schema = InvalidSchema; | ||
const person = new Person(); | ||
person.validate((err, result) => { | ||
@@ -72,30 +75,11 @@ expect(err).to.exist(); | ||
class User extends Wadofgum.mixin(Validation) {}; | ||
User.schema = { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string' | ||
}, | ||
age: { | ||
type: 'integer' | ||
}, | ||
dateOfBirth: { | ||
type: 'string', | ||
format: 'date' | ||
} | ||
} | ||
}; | ||
const user = new User({ | ||
name: 'John', | ||
age: 40, | ||
dateOfBirth: '1975-10-01' | ||
}); | ||
class Person extends Wadofgum.mixin(Validation) {}; | ||
Person.schema = PersonSchema; | ||
const person = new Person(PersonData); | ||
user.validate((err, result) => { | ||
person.validate((err, result) => { | ||
expect(err).to.not.exist(); | ||
expect(user.name).to.equal('John'); | ||
expect(user.age).to.equal(40); | ||
expect(user.dateOfBirth).to.equal('1975-10-01'); | ||
expect(person.person.givenName).to.equal('John'); | ||
expect(person.person.dateOfBirth).to.equal('1975-10-01'); | ||
done(); | ||
@@ -107,25 +91,8 @@ }); | ||
class User extends Wadofgum.mixin(Validation) {}; | ||
User.schema = { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string' | ||
}, | ||
age: { | ||
type: 'integer' | ||
}, | ||
dateOfBirth: { | ||
type: 'string', | ||
format: 'date' | ||
} | ||
} | ||
}; | ||
const user = new User({ | ||
name: 'John', | ||
age: 40, | ||
dateOfBirth: '01/10/1975' | ||
}); | ||
class Person extends Wadofgum.mixin(Validation) {}; | ||
Person.schema = PersonSchema; | ||
PersonData.person.dateOfBirth = '01-10-1975'; | ||
const person = new Person(PersonData); | ||
user.validate((err, result) => { | ||
person.validate((err, result) => { | ||
@@ -132,0 +99,0 @@ expect(err).to.exist(); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20485
9
475
50
3
1
+ Addedhoek@^3.0.1
+ Addedjoi@^7.0.0
+ Addedhoek@3.0.44.3.1(transitive)
+ Addedisemail@2.2.1(transitive)
+ Addedjoi@7.3.0(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedtopo@2.1.1(transitive)