Skip to content

Commit

Permalink
fixed required option
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosRava committed Oct 7, 2014
1 parent f3b1120 commit f154099
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"name": "model-structure",
"version": "1.2.7",
"version": "1.2.8",
"repository": {
"url": "https://github.com/MarcosRava/model-structure/"
},
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/validator-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function getValidation(schema, attr) {
}

var msg;
obj.required = prop.required || false;
switch (prop.type) {
case "enum":
if (prop.values.ref.constructor === Array) {
Expand All @@ -43,7 +44,6 @@ function getValidation(schema, attr) {
obj.pattern = patterns.EMAIL;
break;
case "integer":
obj.required = false;
break;
case "datetime":
obj.type = 'date';
Expand Down
8 changes: 4 additions & 4 deletions test/validations/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Validation', function () {
describe('array', function () {
describe('when array is invalid', function () {
it('should return error when array is not a array', function (done) {
var customer = this.getCustomerFactory({tags: 'not array'});
var customer = this.getCustomerFactory({tags: 'not array', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('tags');
expect(err[0].message).to.be(errorExpectedMessage);
Expand All @@ -27,23 +27,23 @@ describe('Validation', function () {

describe('when array is valid', function () {
it('should not return error when array is an array with string', function (done) {
var customer = this.getCustomerFactory({tags: ['rico', 'com', 'vc']});
var customer = this.getCustomerFactory({tags: ['rico', 'com', 'vc'], email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
});
});

it('should not return error when array is an array with integers', function (done) {
var customer = this.getCustomerFactory({tags: [1, 2, 3]});
var customer = this.getCustomerFactory({tags: [1, 2, 3], email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
});
});

it('should return Model instances', function (done) {
var customer = this.getCustomerFactory({name:"Marcos", hdhd:56756});
var customer = this.getCustomerFactory({name:"Marcos", hdhd:56756, email:'[email protected]'});
customer.create(function (err) {
Customer.get(function(err, customers) {
expect(err).to.be(null);
Expand Down
10 changes: 5 additions & 5 deletions test/validations/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Validation', function () {
describe('date', function () {
describe('when date is invalid', function () {
it('should return error when date is not a date', function (done) {
var customer = this.getCustomerFactory({birthDate: 'not@email'});
var customer = this.getCustomerFactory({birthDate: 'not@email', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('birthDate');
expect(err[0].message).to.contain(Message.errorExpected('birthDate', 'date', customer.birthDate));
Expand All @@ -23,7 +23,7 @@ describe('Validation', function () {
});

it('should return error when date has an additional number', function (done) {
var customer = this.getCustomerFactory({birthDate: '1991-01-021'});
var customer = this.getCustomerFactory({birthDate: '1991-01-021', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('birthDate');
expect(err[0].message).to.contain(Message.errorExpected('birthDate', 'date', customer.birthDate));
Expand All @@ -32,7 +32,7 @@ describe('Validation', function () {
});

it('should return error when date is a date that not exists', function (done) {
var customer = this.getCustomerFactory({birthDate: '1991-06-31'});
var customer = this.getCustomerFactory({birthDate: '1991-06-31', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('birthDate');
expect(err[0].message).to.contain(Message.errorExpected('birthDate', 'date', customer.birthDate));
Expand All @@ -43,15 +43,15 @@ describe('Validation', function () {

describe('when date is valid', function () {
it('should not return error when date is in a string format', function (done) {
var customer = this.getCustomerFactory({birthDate: '1991-01-01'});
var customer = this.getCustomerFactory({birthDate: '1991-01-01', email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
});
});

it('should not return error when date is in a date object', function (done) {
var customer = this.getCustomerFactory({birthDate: new Date()});
var customer = this.getCustomerFactory({birthDate: new Date(), email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
Expand Down
12 changes: 6 additions & 6 deletions test/validations/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Validation', function () {
describe('datetime', function () {
describe('when datetime is invalid', function () {
it('should return error when date is not a date', function (done) {
var customer = this.getCustomerFactory({createdAt: 'not@email'});
var customer = this.getCustomerFactory({createdAt: 'not@email', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('createdAt');
expect(err[0].message).to.contain(Message.errorExpected('createdAt', 'date', customer.createdAt));
Expand All @@ -23,7 +23,7 @@ describe('Validation', function () {
});

it('should return error when date has an additional number', function (done) {
var customer = this.getCustomerFactory({createdAt: '1991-01-021'});
var customer = this.getCustomerFactory({createdAt: '1991-01-021', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('createdAt');
expect(err[0].message).to.contain(Message.errorExpected('createdAt', 'date', customer.createdAt));
Expand All @@ -32,7 +32,7 @@ describe('Validation', function () {
});

it('should return error when date is a date that not exists', function (done) {
var customer = this.getCustomerFactory({createdAt: '1991-06-31'});
var customer = this.getCustomerFactory({createdAt: '1991-06-31', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('createdAt');
expect(err[0].message).to.contain(Message.errorExpected('createdAt', 'date', customer.createdAt));
Expand All @@ -43,23 +43,23 @@ describe('Validation', function () {

describe('when datetime is valid', function () {
it('should not return error when date is in a string format', function (done) {
var customer = this.getCustomerFactory({createdAt: '1991-01-01'});
var customer = this.getCustomerFactory({createdAt: '1991-01-01', email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
});
});

it('should not return error when datetime is in a string format', function (done) {
var customer = this.getCustomerFactory({createdAt: '1991-01-01T02:00:00.000Z'});
var customer = this.getCustomerFactory({createdAt: '1991-01-01T02:00:00.000Z', email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
});
});

it('should not return error when date is in a date object', function (done) {
var customer = this.getCustomerFactory({createdAt: new Date()});
var customer = this.getCustomerFactory({createdAt: new Date(), email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
Expand Down
8 changes: 4 additions & 4 deletions test/validations/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Validation', function () {
describe('enumerable', function () {
describe('when enumerable is invalid', function () {
it('should return error when value is a string and is not in the options accept', function (done) {
var customer = this.getCustomerFactory({gender: 'R'});
var customer = this.getCustomerFactory({gender: 'R', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('gender');
expect(err[0].message).to.contain('gender must be one of M, F');
Expand All @@ -22,7 +22,7 @@ describe('Validation', function () {
});

it('should return error when value is an integer and is not in the options accept', function (done) {
var customer = this.getCustomerFactory({statusId: 4});
var customer = this.getCustomerFactory({statusId: 4, email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('statusId');
expect(err[0].message).to.contain('statusId must be one of 0, 1');
Expand All @@ -33,15 +33,15 @@ describe('Validation', function () {

describe('when enumerable is valid', function () {
it('should not return error when value is an integer and is in the options accept', function (done) {
var customer = this.getCustomerFactory({statusId: 0});
var customer = this.getCustomerFactory({statusId: 0, email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
});
});

it('should not return error when value is a string and is in the options accept', function (done) {
var customer = this.getCustomerFactory({gender: 'F'});
var customer = this.getCustomerFactory({gender: 'F', email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
Expand Down
8 changes: 4 additions & 4 deletions test/validations/float.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ describe('Validation', function () {
describe('float', function () {
describe('when float is invalid', function () {
it('should return error when float is not an float', function (done) {
var customer = this.getCustomerFactory({income: 'not Income'});
var customer = this.getCustomerFactory({income: 'not Income', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('income');
expect(err[0].message).to.contain(errorExpectedMessage);
done();
});
});
it('should return error when float is a float in a string', function (done) {
var customer = this.getCustomerFactory({income: '10.5'});
var customer = this.getCustomerFactory({income: '10.5', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('income');
expect(err[0].message).to.contain(errorExpectedMessage);
Expand All @@ -35,15 +35,15 @@ describe('Validation', function () {

describe('when float is valid', function () {
it('should not return error when float is an float', function (done) {
var customer = this.getCustomerFactory({income: 10.5});
var customer = this.getCustomerFactory({income: 10.5, email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
});
});

it('should not return error when float is an integer', function (done) {
var customer = this.getCustomerFactory({income: 10});
var customer = this.getCustomerFactory({income: 10, email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
Expand Down
4 changes: 2 additions & 2 deletions test/validations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Validation', function () {
}
}
validators.push(validator);
var customer = this.getCustomerFactory({name: 'Sanji'}, {validators:validators});
var customer = this.getCustomerFactory({name: 'Sanji', email:'[email protected]'}, {validators:validators});
customer.create()
.then(function(customer) {
customer.name = 'sanji';
Expand All @@ -64,7 +64,7 @@ describe('Validation', function () {
});

it('should trigger validations parameter form Validator', function (done) {
var customer = this.getCustomerFactory({name: 'sanji'});
var customer = this.getCustomerFactory({name: 'sanji', email:'[email protected]'});
var validators = [];
var validator = new Validator({validate: firstLetterUpperCase});
var error = this.error;
Expand Down
6 changes: 3 additions & 3 deletions test/validations/integer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ describe('Validation', function () {
describe('integer', function () {
describe('when integer is invalid', function () {
it('should return error when integer is not an integer', function (done) {
var customer = this.getCustomerFactory({age: 'not Age'});
var customer = this.getCustomerFactory({age: 'not Age', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('age');
expect(err[0].message).to.contain(errorExpectedMessage);
done();
});
});
it('should return error when integer is an integer in a string', function (done) {
var customer = this.getCustomerFactory({age: '10'});
var customer = this.getCustomerFactory({age: '10', email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('age');
expect(err[0].message).to.contain(errorExpectedMessage);
Expand All @@ -35,7 +35,7 @@ describe('Validation', function () {

describe('when integer is valid', function () {
it('should not return error when integer is an integer', function (done) {
var customer = this.getCustomerFactory({age: 100});
var customer = this.getCustomerFactory({age: 100, email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
Expand Down
8 changes: 4 additions & 4 deletions test/validations/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Validation', function () {
describe('when nested object is invalid', function () {
it('should return error when address is not an object', function (done) {
var address = 'not an object';
var customer = this.getCustomerFactory({address: address});
var customer = this.getCustomerFactory({address: address, email:'[email protected]'});
customer.isValid(function (err) {
expect(err[0].field).to.be('address');
expect(err[0].message).to.contain(errorExpectedMessage);
Expand All @@ -30,15 +30,15 @@ describe('Validation', function () {
});
it('should have only schema properties in nested array', function () {
var address = {addressName: 'Street X', number: 'X100', zipCode: '010400', q:1};
var customer = this.getCustomerFactory({address: address});
var customer = this.getCustomerFactory({address: address, email:'[email protected]'});
expect(customer.address.q).to.not.be(1);
});
});

describe('when nested object is valid', function () {
it('should validate fields in nested object', function (done) {
var address = {addressName: 'Street X', number: 'X100', zipCode: '010400'};
var customer = this.getCustomerFactory({address: address});
var customer = this.getCustomerFactory({address: address, email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.not.be(null);
expect(err[0].field).to.be('address.number');
Expand All @@ -48,7 +48,7 @@ describe('Validation', function () {
it('should validate fields in nested objects in array', function (done) {
var phone1 = {number: 'X100', areaCode: 11};
var phone2 = {number: 995952478, areaCode: 11};
var customer = this.getCustomerFactory({phones:[phone2, phone1]});
var customer = this.getCustomerFactory({phones:[phone2, phone1], email:'[email protected]'});
customer.isValid(function (err) {
expect(err).to.not.be(null);
expect(err[0].field).to.be('phones[1].number');
Expand Down
8 changes: 4 additions & 4 deletions test/validations/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Validation', function () {
describe('when not error', function () {

it('should trigger validations parameter form Validator', function (done) {
var customer = this.getCustomerFactory({name: 'Sanji'});
var customer = this.getCustomerFactory({name: 'Sanji', email:'[email protected]'});
var validators = [];
var validator = new Validator({validate: firstLetterUpperCase});
var error = this.error;
Expand All @@ -43,7 +43,7 @@ describe('Validation', function () {
describe('when has error', function () {

it('should trigger validations parameter from Model instance', function (done) {
var customer = this.getCustomerFactory({name: 'sanji'});
var customer = this.getCustomerFactory({name: 'sanji', email:'[email protected]'});
var validators = [];
var validator = new Validator({validate: firstLetterUpperCase});
var error = this.error;
Expand All @@ -68,7 +68,7 @@ describe('Validation', function () {
});

it('should trigger validations parameter from Validator', function (done) {
var customer = this.getCustomerFactory({name: 'sanji'});
var customer = this.getCustomerFactory({name: 'sanji', email:'[email protected]'});
var validators = [];
var validator = new Validator({validate: firstLetterUpperCase});
var error = this.error;
Expand All @@ -93,7 +93,7 @@ describe('Validation', function () {
});

it('should trigger validations parameter from Validator using async-validator sintaxe', function (done) {
var customer = this.getCustomerFactory({name: 'sanji', age: 2});
var customer = this.getCustomerFactory({name: 'sanji', age: 2, email:'[email protected]'});
var validators = [];

var asyncSchema = {
Expand Down
4 changes: 2 additions & 2 deletions test/validations/required.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Validation', function () {
};
var validator = new Validator({validate: schema});
validators.push(validator);
var customer = this.getCustomerFactory({name:undefined});
var customer = this.getCustomerFactory({name:undefined, email:'[email protected]'});
customer.isValid(validators, function (err) {
expect(err[0].field).to.be('name');
done();
Expand All @@ -39,7 +39,7 @@ describe('Validation', function () {
};
var validator = new Validator({validate: schema});
validators.push(validator);
var customer = this.getCustomerFactory({name:"Marcos"}, {validators:validators});
var customer = this.getCustomerFactory({name:"Marcos", email:'[email protected]'}, {validators:validators});
customer.isValid(function (err) {
expect(err).to.be(null);
done();
Expand Down
Loading

0 comments on commit f154099

Please sign in to comment.