This repository has been archived on 2020-11-15. You can view files and clone it, but cannot push or open issues or pull requests.
schemajs/test/default.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

2012-05-28 00:35:58 +00:00
describe("default schemas", function()
2011-11-16 17:51:23 +00:00
{
/*jshint expr:true*/
var schemajs = (typeof window === 'undefined') ? require('../schema') : window.schema;
var expect = (typeof window === 'undefined') ? require('chai').expect : window.chai.expect;
2011-11-16 17:51:23 +00:00
2012-05-28 00:35:58 +00:00
it("default values", function()
{
var schema = schemajs.create(
{
sound: {type:'string', 'default':'mooo'}
});
2011-11-16 17:51:23 +00:00
2012-05-28 00:35:58 +00:00
var input1 = schema.validate({sound: 'meow'});
var input2 = schema.validate({});
expect(input1.data.sound).to.equal("meow");
expect(input2.data.sound).to.equal("mooo");
});
it('should execute a function supplied for default', function () {
var schema = schemajs.create({
sound: { type: 'string', 'default': function () { return 'moo moo'; } }
});
var input1 = schema.validate({ sound: 'meow' });
var input2 = schema.validate({});
expect(input1.data.sound).to.equal('meow');
expect(input2.data.sound).to.equal('moo moo');
});
2014-08-24 02:27:53 +00:00
it("respects non string/object/array values with defaults", function () {
var
input1, input2,
schema = schemajs.create({
counter: {
type: 'number',
'default': 30
}
});
input1 = schema.validate({
counter: 9
});
input2 = schema.validate({});
expect(input1.data.counter).to.equal(9);
expect(input2.data.counter).to.equal(30);
});
2011-11-16 17:51:23 +00:00
});