Skip to content

Commit

Permalink
Fix subclass schema validation. Fixes #48 (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyruppel authored and talyssonoc committed Feb 21, 2018
1 parent 039712c commit c7169c3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/attributes/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ function attributesDecorator(schema, schemaOptions = {}) {
}
});

schema = Schema.normalize(schema, schemaOptions);

if(WrapperClass[SCHEMA]) {
schema = Object.assign({}, WrapperClass[SCHEMA], schema);
}

schema = Schema.normalize(schema, schemaOptions);

define(WrapperClass, SCHEMA, {
value: schema
});
Expand Down
56 changes: 56 additions & 0 deletions test/unit/validation/structureSubclass.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { attributes } = require('../../../src');
const { assertValid, assertInvalid } = require('../../support/validationMatchers');

describe('validation', () => {
describe('structure subclass', () => {
var Admin;
var User;

beforeEach(() => {
User = attributes({
name: {
type: String,
required: true
}
})(class User {});

Admin = attributes({
level: {
type: Number,
required: true
}
})(class Admin extends User {});
});

context('with invalid superclass schema', () => {
it('is invalid', () => {
const admin = new Admin({
level: 3
});

assertInvalid(admin, 'name');
});
});

context('with invalid subclass schema', () => {
it('is invalid', () => {
const admin = new Admin({
name: 'The admin'
});

assertInvalid(admin, 'level');
});
});

context('with valid superclass and subclass schema', () => {
it('is valid', () => {
const admin = new Admin({
name: 'The admin',
level: 3
});

assertValid(admin);
});
});
});
});

0 comments on commit c7169c3

Please sign in to comment.