Merge pull request #18 from oliverbrooks/master
include null values in output if allownull is true
This commit is contained in:
commit
ddead1d6ea
|
@ -55,7 +55,9 @@ Schema.prototype.validate = function(data, opts) {
|
|||
_.keys(schema.schema).length &&
|
||||
!_.isUndefined(value)) {
|
||||
|
||||
if(!(schema.allownull && _.isNull(value))) {
|
||||
if(schema.allownull && _.isNull(value)) {
|
||||
values[params[i]] = null;
|
||||
} else {
|
||||
if (schema.type == "object") {
|
||||
var results = (new Schema(schema.schema)).validate(value);
|
||||
if(!results.valid) {
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
/*jshint expr:true*/
|
||||
var schemajs = (typeof window === 'undefined') ? require('../schema') : window.schema;
|
||||
var expect = (typeof window === 'undefined') ? require('chai').expect : window.chai.expect;
|
||||
|
||||
describe("allownull schemas", function() {
|
||||
|
||||
var schema = schemajs.create({
|
||||
input: {type: 'string', allownull: true}
|
||||
});
|
||||
|
||||
it("should allow an attribute to be set to it's type", function() {
|
||||
var form = schema.validate({input: 'username'});
|
||||
|
||||
expect(form.valid).to.be.ok;
|
||||
expect(form.data.input).to.equal('username');
|
||||
});
|
||||
|
||||
it("should allow an attribute to be set to null", function() {
|
||||
var form = schema.validate({input: null});
|
||||
|
||||
expect(form.valid).to.be.ok;
|
||||
expect(form.data.input).to.equal(null);
|
||||
});
|
||||
|
||||
it("should be valid but not include an attribute if it is not included", function() {
|
||||
var form = schema.validate({});
|
||||
|
||||
expect(form.valid).to.be.ok;
|
||||
expect(form.data.input).to.eql(undefined);
|
||||
});
|
||||
|
||||
it("should not validate if the attribute is a number", function() {
|
||||
var form = schema.validate({input: 6});
|
||||
expect(form.valid).to.not.be.ok;
|
||||
});
|
||||
|
||||
|
||||
});
|
|
@ -38,12 +38,15 @@ describe("complex schemas", function()
|
|||
var input5 = schema.validate({input: null});
|
||||
var input6 = schema2.validate({input: null});
|
||||
|
||||
expect(!input1.valid).to.be.ok;
|
||||
expect(input1.valid).to.not.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input2.data.input).to.eql([112390123]);
|
||||
expect(input3.valid).to.not.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
expect(!input5.valid).to.be.ok;
|
||||
expect(input5.valid).to.not.be.ok;
|
||||
expect(input6.valid).to.be.ok;
|
||||
expect(input6.data.input).to.equal(null);
|
||||
|
||||
});
|
||||
|
||||
it("arrays and objects", function()
|
||||
|
@ -115,11 +118,11 @@ describe("complex schemas", function()
|
|||
var input5 = schema.validate({input:null});
|
||||
var input6 = schema2.validate({input:null});
|
||||
|
||||
expect(!input1.valid).to.be.ok;
|
||||
expect(input1.valid).to.not.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
expect(!input5.valid).to.be.ok;
|
||||
expect(input5.valid).to.not.be.ok;
|
||||
expect(input6.valid).to.be.ok;
|
||||
});
|
||||
});
|
||||
|
|
|
@ -31,7 +31,9 @@ describe("required schemas", function()
|
|||
var input3 = schema.validate({input:null, output:"hi there"});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input1.data.input).to.equal('username');
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(input3.valid).to.be.ok;
|
||||
expect(input3.data.input).to.equal(null);
|
||||
});
|
||||
});
|
||||
|
|
Reference in New Issue