Skip to content

Commit

Permalink
fix: enforce validations as array
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Jul 25, 2016
1 parent dc5ccd0 commit dd11edf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
17 changes: 11 additions & 6 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,21 @@ Model.prototype.children = function (children) {
};

Model.prototype.validations = function (validations) {
var msg = 'validations must be a regex, function, or array of functions';
if (_.isArray(validations)) {
validations.forEach(item => assert.ok(typeof item === 'function', msg));
} else if (validations instanceof RegExp) {
assert.ok(_.isArray(validations), 'validations must be an array');
return validations.reduce(function (model, validation) {
return model.validation(validation);
}, this);
};

Model.prototype.validation = function (validation) {
if (validation instanceof RegExp) {
assert.equal(this.spec.type, 'string', 'model must be of type string');
} else {
assert.ok(typeof validations === 'function', msg);
assert.ok(typeof validation === 'function', 'validation must be a regex or function');
}

return this._with({validations: validations});
var validations = this.spec.validations || [];
return this._with({validations: validations.concat([validation])})
};

Model.prototype._getApplicableOptions = function (value, root, path) {
Expand Down
28 changes: 14 additions & 14 deletions test/Model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,33 +439,33 @@ defineTest('Model.js', function (Model) {
describe('#validations', function () {
it('should set spec.validations when regex', function () {
var regex = /^something|else$/;
var model = new Model().type('string').validations(regex);
model.spec.should.have.property('validations', regex);
var model = new Model().type('string').validations([regex]);
model.spec.should.have.property('validations').eql([regex]);
});

it('should set spec.validations when function', function () {
var validate = function () { };
var model = new Model().type('number').validations(validate);
model.spec.should.have.property('validations', validate);
var model = new Model().type('number').validations([validate]);
model.spec.should.have.property('validations').eql([validate]);
});

it('should set spec.validations when array of functions', function () {
var validateA = function () { };
var validateB = function () { };
var validate = [validateA, validateB];
var model = new Model().validations(validate);
model.spec.should.have.property('validations', validate);
model.spec.should.have.property('validations').eql(validate);
});

it('should fail when given a non-function', function () {
it('should fail when given a non-array', function () {
(function () {
new Model().validations('random');
}).should.throw();
});

it('should fail when given a regexp and type is not string', function () {
(function () {
new Model().validations(/foo/);
new Model().validations([/foo/]);
}).should.throw();
});

Expand Down Expand Up @@ -569,7 +569,7 @@ defineTest('Model.js', function (Model) {
var model = new Model({
type: 'string',
default: 'hello world',
validations: function () { assert.ok(false, 'oops'); },
validations: [function () { assert.ok(false, 'oops'); }],
});

model.validate(undefined).asObject().should.eql({
Expand All @@ -583,7 +583,7 @@ defineTest('Model.js', function (Model) {
var model = new Model({
type: 'string',
default: null,
validations: function () { assert.ok(false, 'oops'); },
validations: [function () { assert.ok(false, 'oops'); }],
});

model.validate(undefined).asObject().should.eql({
Expand Down Expand Up @@ -620,7 +620,7 @@ defineTest('Model.js', function (Model) {
var model = new Model({
type: 'string',
parse: parser,
validations: validations,
validations: [validations],
});

model.validate('something').asObject().should.eql({
Expand Down Expand Up @@ -667,7 +667,7 @@ defineTest('Model.js', function (Model) {
var model = new Model({
type: 'number',
transform: transform,
validations: validate,
validations: [validate],
});

model.validate(123).asObject().should.eql({
Expand All @@ -680,7 +680,7 @@ defineTest('Model.js', function (Model) {

context('when validations is a RegExp', function () {
it('should pass a valid match', function () {
var model = new Model({type: 'string', validations: /^foo.*bar$/});
var model = new Model({type: 'string', validations: [/^foo.*bar$/]});
model.validate('fooANYTHINGbar').asObject().should.eql({
value: 'fooANYTHINGbar',
conforms: true,
Expand All @@ -689,7 +689,7 @@ defineTest('Model.js', function (Model) {
});

it('should fail an invalid match', function () {
var model = new Model({type: 'string', validations: /^foo.*bar$/});
var model = new Model({type: 'string', validations: [/^foo.*bar$/]});
model.validate('somethingElsebar').asObject().should.eql({
value: 'somethingElsebar',
conforms: false,
Expand All @@ -698,7 +698,7 @@ defineTest('Model.js', function (Model) {
});

it('should fail a non-string', function () {
var model = new Model({type: 'string', validations: /\d+/});
var model = new Model({type: 'string', validations: [/\d+/]});
model.validate(12312).asObject().should.eql({
value: 12312,
conforms: false,
Expand Down
2 changes: 1 addition & 1 deletion test/transformations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defineTest('transformations.js', function (transformations) {
beforeEach(function () {
children = {
id: new Model({type: 'number'}),
name: new Model({type: 'string', validations: /^ABC/}),
name: new Model({type: 'string', validations: [/^ABC/]}),
isAdmin: new Model({type: 'boolean'}),
};

Expand Down

0 comments on commit dd11edf

Please sign in to comment.