Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/strict-option'
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliaksandr committed Jan 12, 2015
2 parents 6084843 + 3437d96 commit c0a30c9
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 45 deletions.
91 changes: 54 additions & 37 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ Schema.prototype = {
return new this.constructor().like(this);
},

isStrict: false,

strict: function (flag) {
if (flag == null || flag) {
this.isStrict = true;
} else {
delete this.isStrict;
}

return this;
},

/**
* clone schema to this schema object
*
Expand Down Expand Up @@ -240,6 +252,7 @@ Schema.prototype = {
* @returns {Schema.SchemaVerifier}
* */
verifier: function (options) {

return new this.constructor.SchemaVerifier(this, options);
},

Expand Down Expand Up @@ -292,57 +305,61 @@ Schema.prototype = {


var SchemaVerifier = function SchemaVerifier (schema, options) {
this.Schema = schema.constructor;
this.options = _.extend({
options = _.extend({
ignoreExcess: false
}, options);

this.schema = this._compileSchema(schema);
};

SchemaVerifier.prototype = {

_compileSchema: function (schema) {
var options = this.options;
return new this.Schema().like(schema, function (schema) {
var validations = _.cloneDeep(schema.validations || []),
objectRules = [ 'type object' ],
arrayRules = [ 'type array' ];

this.isArray = schema.isArray;
this.schema = new schema.constructor().like(schema, function (schema) {
var thisSchema = this;
var objectRules = [ 'type object' ];
var arrayRules = [ 'type array' ];

thisSchema.isArray = schema.isArray;

if (schema.fields) {
//if (!options.ignoreExcess) {
// objectRules.push({
// available_fields: _.keys(schema.fields)
// });
//}
if (schema.isStrict) {
objectRules.push({
available_fields: _.keys(schema.fields)
});
}

if (schema.fields) {
if (!options.ignoreExcess) {
objectRules.push({ available_fields: _.keys(schema.fields) });
}
arrayRules.push({
each: objectRules
});
}

arrayRules.push({each: objectRules});
}
var validations = _.cloneDeep(schema.validations || []);
if (schema.isArray) {
validations.unshift.apply(validations, arrayRules);
} else if (schema.fields) {
validations.unshift.apply(validations, objectRules);
}

if (schema.isArray) {
validations.unshift.apply(validations, arrayRules);
} else if (schema.fields) {
validations.unshift.apply(validations, objectRules);
}
if (schema.isRequired) {
validations.unshift('required');
} else {
validations = {
any: [ 'type undefined', validations ]
};
}

if (schema.isRequired) {
validations.unshift('required');
} else {
validations = {
any: [ 'type undefined', validations ]
};
}
thisSchema._verifier = new Verifier(validations);
});
};

this.verifierObject = new Verifier(validations);
});
},
SchemaVerifier.prototype = {

_verifySchema: function (schema, value, path, done) {
var that = this;

done = this._wrapDone(done, value, path);

schema.verifierObject.verify(value, function (err) {
schema._verifier.verify(value, function (err) {
if (err) {
done(err);
return;
Expand Down
2 changes: 1 addition & 1 deletion tests/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ exports['validate value-array'] = {
})
};

var s2 = new Schema().array(function (required, optional) {
var s2 = new Schema().strict().array(function (required, optional) {
required('age');
optional('school_names');
});
Expand Down
5 changes: 3 additions & 2 deletions tests/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ exports['Simple Usage'] = {
});
},
'verify - excess field' : function (test) {
var sh1 = new Schema().object(function () {
console.log(123);
var sh1 = new Schema().strict().object(function () {
this.field('first_name');
this.required('last_name');
this.optional('middle_name');
Expand All @@ -94,7 +95,7 @@ exports['Simple Usage'] = {
var _keys = _.keys(value);
value.excess_field = true;

sh1.verifier().verify(value, function (err) {
sh1.verify(value, function (err) {
test.ok(!!err);
test.equal(err.rule, 'available_fields');
test.deepEqual(err.params, _keys);
Expand Down
12 changes: 7 additions & 5 deletions tests/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ exports.schema = {
}
};

var s1 = new Schema().object(function (required, optional) {
var s1 = new Schema().strict().object(function (required, optional) {
required('age');
optional('school_names');
});

var s2 = new Schema().object(function (required, optional) {
required('age');
optional('school_names');
});
Expand Down Expand Up @@ -178,11 +183,8 @@ exports['validate object'] = {
}),

'#12-ignore': tester({
schema: s1,
schema: s2,
expect: true,
options: {
ignoreExcess: true
},
value: {
'age': {
'asdasdasd': 'asdasdasd'
Expand Down
68 changes: 68 additions & 0 deletions tests/strict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"use strict";

var tester = require('./_lib/tester');

var Schema = require('./_lib/schema');

var sh1 = Schema.create().object(function (r, o) {
r('some_1');
r('some_2');
o('some_3');
});

exports['valid object'] = tester({
schema: sh1,
expect: true,
value: {
some_1: 1,
some_2: 2,
some_3: 3,
some_4: 3
}
});

var sh2 = sh1.clone().strict();
exports['invalid object'] = tester({
schema: sh2,
vErr: {
rule: 'available_fields',
params: ['some_1', 'some_2', 'some_3'],
path: []
},
value: {
some_1: 1,
some_2: 2,
some_3: 3,
some_4: 3
}
});


var sh3 = sh1.clone().array();
exports['valid array-object'] = tester({
schema: sh3,
expect: true,
value: [{
some_1: 1,
some_2: 2,
some_3: 3,
some_4: 3
}]
});


var sh4 = sh3.clone().strict();
exports['invalid array-object'] = tester({
schema: sh4,
vErr: {
rule: 'available_fields',
params: ['some_1', 'some_2', 'some_3'],
path: ['0']
},
value: [{
some_1: 1,
some_2: 2,
some_3: 3,
some_4: 3
}]
});

0 comments on commit c0a30c9

Please sign in to comment.