convert tests to mocha, add travis ci
This commit is contained in:
parent
12534d448d
commit
836fed1062
|
@ -0,0 +1,3 @@
|
|||
language: node_js
|
||||
node_js:
|
||||
- 0.6
|
|
@ -1,5 +1,5 @@
|
|||
var _ = require('underscore');
|
||||
var rules = require('./rules.js');
|
||||
var _ = require('underscore');
|
||||
var rules = require('./rules.js');
|
||||
|
||||
var Schema = function(schema)
|
||||
{
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
"url": "http://github.com/eleith/schemajs.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tap": ">= 0.0.13"
|
||||
"mocha": ">= 1.0.3"
|
||||
},
|
||||
"dependencies":
|
||||
{
|
||||
|
@ -19,6 +19,6 @@
|
|||
"engine": ["node >= 0.4.8"],
|
||||
"main": "schema",
|
||||
"scripts": {
|
||||
"test" : "tap test/*.js"
|
||||
"test" : "mocha -R spec test/*.js"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,57 +1,56 @@
|
|||
var tap = require("tap");
|
||||
var schemajs = require('../schema');
|
||||
|
||||
tap.test("testing complex array type", function(t)
|
||||
describe("complex schemas", function()
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
var schemajs = require('../schema');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
it("arrays", function()
|
||||
{
|
||||
input:
|
||||
{
|
||||
type:'array',
|
||||
schema:
|
||||
{
|
||||
type: 'number'
|
||||
}
|
||||
}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input:
|
||||
{
|
||||
type:'array',
|
||||
schema:
|
||||
{
|
||||
type: 'number'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'username'});
|
||||
var input2 = schema.validate({input: [112390123]});
|
||||
var input3 = schema.validate({input: [112390123, 'username']});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(!input1.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'username'});
|
||||
var input2 = schema.validate({input: [112390123]});
|
||||
var input3 = schema.validate({input: [112390123, 'username']});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(!input1.valid, 'input1 is not valid (not an array)');
|
||||
t.ok(input2.valid, 'input2 is valid');
|
||||
t.ok(input2.valid, 'input3 is not valid (2nd element is not a number)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing complex object type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("testing complex object type", function()
|
||||
{
|
||||
input:
|
||||
{
|
||||
type:'object',
|
||||
schema:
|
||||
{
|
||||
name: { type: "string", properties: { max: 255 }, required: true},
|
||||
email: { type: "email", error: "email is not a valid email address"}
|
||||
}
|
||||
}
|
||||
});
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input:
|
||||
{
|
||||
type:'object',
|
||||
schema:
|
||||
{
|
||||
name: { type: "string", properties: { max: 255 }, required: true},
|
||||
email: { type: "email", error: "email is not a valid email address"}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'username'});
|
||||
var input2 = schema.validate({input: {name:"x", email:"x@xyz.com"}});
|
||||
var input3 = schema.validate({input: {name:123, email:"x@xyz.com"}});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
var input1 = schema.validate({input: 'username'});
|
||||
var input2 = schema.validate({input: {name:"x", email:"x@xyz.com"}});
|
||||
var input3 = schema.validate({input: {name:123, email:"x@xyz.com"}});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(!input1.valid, 'input1 is not valid (not an object)');
|
||||
t.ok(input2.valid, 'input2 is valid');
|
||||
t.ok(input2.valid, 'input3 is not valid (input.name is not a string)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
expect(!input1.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
var tap = require("tap");
|
||||
var schemajs = require('../schema');
|
||||
|
||||
tap.test("testing default", function(t)
|
||||
describe("default schemas", function()
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
var schemajs = require('../schema');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
it("default values", function()
|
||||
{
|
||||
sound: {type:'string', 'default':'mooo'},
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
sound: {type:'string', 'default':'mooo'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({sound: 'meow'});
|
||||
var input2 = schema.validate({});
|
||||
|
||||
expect(input1.data.sound).to.equal("meow");
|
||||
expect(input2.data.sound).to.equal("mooo");
|
||||
});
|
||||
var input1 = schema.validate({sound: 'meow'});
|
||||
var input2 = schema.validate({});
|
||||
|
||||
t.ok(input1.data.sound == 'meow', 'input1 is valid');
|
||||
t.ok(input2.data.sound == 'mooo', 'input2 is valid');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
|
|
@ -1,36 +1,41 @@
|
|||
var tap = require("tap");
|
||||
var schemajs = require('../schema');
|
||||
|
||||
tap.test("testing basic error", function(t)
|
||||
describe("schema errors", function()
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
var schemajs = require('../schema');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
it("basic error", function()
|
||||
{
|
||||
sound: {type:'string', error:'silence!', required:true},
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
sound: {type:'string', error:'silence!', required:true}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({sound: 'meow'});
|
||||
var input2 = schema.validate({sound: 0});
|
||||
var input3 = schema.validate({});
|
||||
|
||||
expect(input1.data.sound).to.equal('meow');
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(input2.errors.sound).to.equal('silence!');
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input3.errors.sound).to.equal('silence!');
|
||||
});
|
||||
var input1 = schema.validate({sound: 'meow'});
|
||||
var input2 = schema.validate({sound: 0});
|
||||
var input3 = schema.validate({});
|
||||
|
||||
t.ok(input1.data.sound == 'meow', 'input1 is valid');
|
||||
t.ok(!input2.valid && input2.errors.sound == 'silence!', 'input2 is not valid (proper error raised)');
|
||||
t.ok(!input3.valid && input3.errors.sound == 'silence!', 'input3 is not valid (proper error raised)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing detailed errors", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
|
||||
it("detailed errors", function()
|
||||
{
|
||||
sound: {type:'string', error:{type:'sound must be speakable', required:'sound is missing'}, required:true},
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
sound: {type:'string', error:{type:'sound must be speakable', required:'sound is missing'}, required:true}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({sound: 'meow'});
|
||||
var input2 = schema.validate({sound: 0});
|
||||
var input3 = schema.validate({});
|
||||
|
||||
expect(input1.data.sound).to.equal('meow');
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(input2.errors.sound).to.equal('sound must be speakable');
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input3.errors.sound).to.equal('sound is missing');
|
||||
});
|
||||
var input1 = schema.validate({sound: 'meow'});
|
||||
var input2 = schema.validate({sound: 0});
|
||||
var input3 = schema.validate({});
|
||||
|
||||
t.ok(input1.data.sound == 'meow', 'input1 is valid');
|
||||
t.ok(!input2.valid && input2.errors.sound == 'sound must be speakable', 'input2 is not valid (proper error raised)');
|
||||
t.ok(!input3.valid && input3.errors.sound == 'sound is missing', 'input3 is not valid (proper error raised)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
|
300
test/filters.js
300
test/filters.js
|
@ -1,180 +1,170 @@
|
|||
var tap = require("tap");
|
||||
var schemajs = require('../schema');
|
||||
|
||||
tap.test("testing toInt filter", function(t)
|
||||
describe("filter schemas", function()
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
var schemajs = require('../schema');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
it("toInt filter", function()
|
||||
{
|
||||
input: {type:"int", filters:["toInt"]}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:"int", filters:["toInt"]}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: '100'});
|
||||
var input2 = schema.validate({input: 101});
|
||||
var input3 = schema.validate({input: '102a'});
|
||||
var input4 = schema.validate({input: 'oneohthree'});
|
||||
var input5 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(input3.valid).to.be.ok;
|
||||
expect(!input4.valid).to.be.ok;
|
||||
expect(input5.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: '100'});
|
||||
var input2 = schema.validate({input: 101});
|
||||
var input3 = schema.validate({input: '102a'});
|
||||
var input4 = schema.validate({input: 'oneohthree'});
|
||||
var input5 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(input2.valid, 'input2 is valid');
|
||||
t.ok(input3.valid, 'input3 is valid');
|
||||
t.ok(!input4.valid, 'input4 is not valid');
|
||||
t.ok(input5.valid, 'input5 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing toFloat filter", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("toFloat filter", function()
|
||||
{
|
||||
input: {type:"number", filters:["toFloat"]}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:"number", filters:["toFloat"]}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: '100.123'});
|
||||
var input2 = schema.validate({input: 101.123});
|
||||
var input3 = schema.validate({input: 'oneohthree'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: '100.123'});
|
||||
var input2 = schema.validate({input: 101.123});
|
||||
var input3 = schema.validate({input: 'oneohthree'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(input2.valid, 'input2 is valid');
|
||||
t.ok(!input3.valid, 'input3 is not valid');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing toString filter", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("toString filter", function()
|
||||
{
|
||||
input: {type:"string", filters:["toString"]}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:"string", filters:["toString"]}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 1123});
|
||||
var input2 = schema.validate({input: ['a', 'b']});
|
||||
var input3 = schema.validate({input: 'oneohthree'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 1123});
|
||||
var input2 = schema.validate({input: ['a', 'b']});
|
||||
var input3 = schema.validate({input: 'oneohthree'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(input2.valid, 'input2 is valid');
|
||||
t.ok(input3.valid, 'input3 is valid');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing toDate filter", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("toDate filter", function()
|
||||
{
|
||||
input: {type:"date", filters:["toDate"]}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:"date", filters:["toDate"]}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: '11-24-2011'});
|
||||
var input2 = schema.validate({input: '11/24/11'});
|
||||
var input3 = schema.validate({input: 'November 24, 2011'});
|
||||
var input4 = schema.validate({input: 'yesterday'});
|
||||
var input5 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(input3.valid).to.be.ok;
|
||||
expect(!input4.valid).to.be.ok;
|
||||
expect(input5.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: '11-24-2011'});
|
||||
var input2 = schema.validate({input: '11/24/11'});
|
||||
var input3 = schema.validate({input: 'November 24, 2011'});
|
||||
var input4 = schema.validate({input: 'yesterday'});
|
||||
var input5 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(input2.valid, 'input2 is valid');
|
||||
t.ok(input3.valid, 'input3 is valid');
|
||||
t.ok(!input4.valid, 'input4 is not valid (not a Date)');
|
||||
t.ok(input5.valid, 'input5 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing toBoolean filter", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("toBoolean filter", function()
|
||||
{
|
||||
input: {type:"boolean", filters:["toBoolean"]}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:"boolean", filters:["toBoolean"]}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 0});
|
||||
var input2 = schema.validate({input: 1});
|
||||
var input3 = schema.validate({input: 2});
|
||||
|
||||
var input4 = schema.validate({input: 'yes'});
|
||||
var input5 = schema.validate({input: 'no'});
|
||||
var input6 = schema.validate({input: 'maybe'});
|
||||
|
||||
var input7 = schema.validate({input: 'true'});
|
||||
var input8 = schema.validate({input: 'false'});
|
||||
var input9 = schema.validate({input: 'neither'});
|
||||
|
||||
var input10 = schema.validate({input: true});
|
||||
var input11 = schema.validate({input: false});
|
||||
var input12 = schema.validate({input: null});
|
||||
|
||||
var input13 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
|
||||
expect(input4.valid).to.be.ok;
|
||||
expect(input5.valid).to.be.ok;
|
||||
expect(!input6.valid).to.be.ok;
|
||||
|
||||
expect(input7.valid).to.be.ok;
|
||||
expect(input8.valid).to.be.ok;
|
||||
expect(!input9.valid).to.be.ok;
|
||||
|
||||
expect(input10.valid).to.be.ok;
|
||||
expect(input11.valid).to.be.ok;
|
||||
expect(!input12.valid).to.be.ok;
|
||||
|
||||
expect(input13.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 0});
|
||||
var input2 = schema.validate({input: 1});
|
||||
var input3 = schema.validate({input: 2});
|
||||
|
||||
var input4 = schema.validate({input: 'yes'});
|
||||
var input5 = schema.validate({input: 'no'});
|
||||
var input6 = schema.validate({input: 'maybe'});
|
||||
|
||||
var input7 = schema.validate({input: 'true'});
|
||||
var input8 = schema.validate({input: 'false'});
|
||||
var input9 = schema.validate({input: 'neither'});
|
||||
|
||||
var input10 = schema.validate({input: true});
|
||||
var input11 = schema.validate({input: false});
|
||||
var input12 = schema.validate({input: null});
|
||||
|
||||
var input13 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(input2.valid, 'input2 is valid');
|
||||
t.ok(!input3.valid, 'input3 is valid (not a boolean)');
|
||||
|
||||
t.ok(input4.valid, 'input4 is valid');
|
||||
t.ok(input5.valid, 'input5 is valid');
|
||||
t.ok(!input6.valid, 'input6 is valid (not a boolean)');
|
||||
|
||||
t.ok(input7.valid, 'input7 is valid');
|
||||
t.ok(input8.valid, 'input8 is valid');
|
||||
t.ok(!input9.valid, 'input9 is valid (not a boolean)');
|
||||
|
||||
t.ok(input10.valid, 'input10 is valid');
|
||||
t.ok(input11.valid, 'input11 is valid');
|
||||
t.ok(!input12.valid, 'input12 is valid (not a boolean)');
|
||||
|
||||
t.ok(input13.valid, 'input13 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing trim filter", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("testing trim filter", function()
|
||||
{
|
||||
input: {type:"string", filters:["trim"]}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:"string", filters:["trim"]}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: ' nachos '});
|
||||
var input2 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input1.data.input).to.equal('nachos');
|
||||
expect(input2.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: ' nachos '});
|
||||
var input2 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid && input1.data.input == 'nachos', 'input1 is valid');
|
||||
t.ok(input2.valid, 'input2 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing lowercase filter", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("lowercase filter", function()
|
||||
{
|
||||
input: {type:"string", filters:["lowercase"]}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:"string", filters:["lowercase"]}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'BURRITOS'});
|
||||
var input2 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input1.data.input).to.equal('burritos');
|
||||
expect(input2.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'BURRITOS'});
|
||||
var input2 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid && input1.data.input == 'burritos', 'input1 is valid');
|
||||
t.ok(input2.valid, 'input2 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing uppercase filter", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("uppercase filter", function()
|
||||
{
|
||||
input: {type:"string", filters:["uppercase"]}
|
||||
});
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:"string", filters:["uppercase"]}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'donuts'});
|
||||
var input2 = schema.validate({});
|
||||
|
||||
var input1 = schema.validate({input: 'donuts'});
|
||||
var input2 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid && input1.data.input == 'DONUTS', 'input1 is valid');
|
||||
t.ok(input2.valid, 'input2 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input1.data.input).to.equal('DONUTS');
|
||||
expect(input2.valid).to.be.ok;
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,98 +1,92 @@
|
|||
var tap = require("tap");
|
||||
var schemajs = require('../schema');
|
||||
|
||||
tap.test("testing minimum string length property", function(t)
|
||||
describe("filter schemas", function()
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
var schemajs = require('../schema');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
it("minimum string length property", function()
|
||||
{
|
||||
username: {properties: {min:2}}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
username: {properties: {min:2}}
|
||||
});
|
||||
|
||||
var user1 = schema.validate({username: 'username'});
|
||||
var user2 = schema.validate({username: 'a'});
|
||||
var user3 = schema.validate({username: 'ab'});
|
||||
var user4 = schema.validate({});
|
||||
|
||||
expect(user1.valid).to.be.ok;
|
||||
expect(!user2.valid).to.be.ok;
|
||||
expect(user3.valid).to.be.ok;
|
||||
expect(user4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var user1 = schema.validate({username: 'username'});
|
||||
var user2 = schema.validate({username: 'a'});
|
||||
var user3 = schema.validate({username: 'ab'});
|
||||
var user4 = schema.validate({});
|
||||
|
||||
t.ok(user1.valid, 'user1 has valid username length');
|
||||
t.ok(!user2.valid, 'user2 has invalid username length (too short)');
|
||||
t.ok(user3.valid, 'user3 has valid username length (same as min)');
|
||||
t.ok(user4.valid, 'user4 has no username (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing maximum string length property", function(t)
|
||||
{
|
||||
var schema = schemajs.create({
|
||||
username: {properties: {max: 16}}
|
||||
});
|
||||
|
||||
var user1 = schema.validate({username: 'username'});
|
||||
var user2 = schema.validate({username: '01234567890abcdefg'});
|
||||
var user3 = schema.validate({username: '0123456789abcdef'});
|
||||
var user4 = schema.validate({});
|
||||
|
||||
t.ok(user1.valid, 'user1 has valid username length');
|
||||
t.ok(!user2.valid, 'user2 has invalid username length (too long)');
|
||||
t.ok(user3.valid, 'user3 has valid username length (same as max)');
|
||||
t.ok(user4.valid, 'user4 has no username (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing regex property", function(t)
|
||||
{
|
||||
var schema = schemajs.create({
|
||||
mobile: {properties:{regex:/\d{3}-\d{3}-\d{4}/}}
|
||||
});
|
||||
|
||||
var mobile1 = schema.validate({mobile: '123-456-7890'});
|
||||
var mobile2 = schema.validate({mobile: '23-456-7890'});
|
||||
var mobile3 = schema.validate({});
|
||||
|
||||
t.ok(mobile1.valid, 'mobile1 is valid (matches regex)');
|
||||
t.ok(!mobile2.valid, 'mobile2 is invalid (does not match regex)');
|
||||
t.ok(mobile3.valid, 'mobile3 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing 'in' property", function(t)
|
||||
{
|
||||
var schema = schemajs.create({
|
||||
fruit: {properties:{'in': ['apple', 'pear', 'tomato']}}
|
||||
});
|
||||
|
||||
var fruit1 = schema.validate({fruit: 'apple'});
|
||||
var fruit2 = schema.validate({fruit: 'peas'});
|
||||
var fruit3 = schema.validate({});
|
||||
|
||||
t.ok(fruit1.valid, 'fruit1 is valid (is in array)');
|
||||
t.ok(!fruit2.valid, 'fruit2 is invalid (is not in array)');
|
||||
t.ok(fruit3.valid, 'fruit3 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing custom property", function(t)
|
||||
{
|
||||
schemajs.properties.notIn = function(value, list)
|
||||
it("maximum string length property", function()
|
||||
{
|
||||
return list.indexOf(value) == -1;
|
||||
};
|
||||
|
||||
var schema = schemajs.create({
|
||||
planet: {properties:{notIn:['pluto', 'moon', 'sun']}}
|
||||
var schema = schemajs.create({
|
||||
username: {properties: {max: 16}}
|
||||
});
|
||||
|
||||
var user1 = schema.validate({username: 'username'});
|
||||
var user2 = schema.validate({username: '01234567890abcdefg'});
|
||||
var user3 = schema.validate({username: '0123456789abcdef'});
|
||||
var user4 = schema.validate({});
|
||||
|
||||
expect(user1.valid).to.be.ok;
|
||||
expect(!user2.valid).to.be.ok;
|
||||
expect(user3.valid).to.be.ok;
|
||||
expect(user4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
it("regex property", function()
|
||||
{
|
||||
var schema = schemajs.create({
|
||||
mobile: {properties:{regex:/\d{3}-\d{3}-\d{4}/}}
|
||||
});
|
||||
|
||||
var mobile1 = schema.validate({mobile: '123-456-7890'});
|
||||
var mobile2 = schema.validate({mobile: '23-456-7890'});
|
||||
var mobile3 = schema.validate({});
|
||||
|
||||
expect(mobile1.valid).to.be.ok;
|
||||
expect(!mobile2.valid).to.be.ok;
|
||||
expect(mobile3.valid).to.be.ok;
|
||||
});
|
||||
|
||||
it("testing 'in' property", function()
|
||||
{
|
||||
var schema = schemajs.create({
|
||||
fruit: {properties:{'in': ['apple', 'pear', 'tomato']}}
|
||||
});
|
||||
|
||||
var fruit1 = schema.validate({fruit: 'apple'});
|
||||
var fruit2 = schema.validate({fruit: 'peas'});
|
||||
var fruit3 = schema.validate({});
|
||||
|
||||
expect(fruit1.valid).to.be.ok;
|
||||
expect(!fruit2.valid).to.be.ok;
|
||||
expect(fruit3.valid).to.be.ok;
|
||||
});
|
||||
|
||||
it("testing custom property", function()
|
||||
{
|
||||
schemajs.properties.notIn = function(value, list)
|
||||
{
|
||||
return list.indexOf(value) == -1;
|
||||
};
|
||||
|
||||
var schema = schemajs.create({
|
||||
planet: {properties:{notIn:['pluto', 'moon', 'sun']}}
|
||||
});
|
||||
|
||||
var planet1 = schema.validate({planet: 'earth'});
|
||||
var planet2 = schema.validate({planet: 'pluto'});
|
||||
var planet3 = schema.validate({});
|
||||
|
||||
expect(planet1.valid).to.be.ok;
|
||||
expect(!planet2.valid).to.be.ok;
|
||||
expect(planet3.valid).to.be.ok;
|
||||
|
||||
delete schemajs.properties.notIn;
|
||||
});
|
||||
|
||||
var planet1 = schema.validate({planet: 'earth'});
|
||||
var planet2 = schema.validate({planet: 'pluto'});
|
||||
var planet3 = schema.validate({});
|
||||
|
||||
t.ok(planet1.valid, 'planet1 is valid (is in array)');
|
||||
t.ok(!planet2.valid, 'planet2 is invalid (is not in array)');
|
||||
t.ok(planet3.valid, 'planet3 is valid (not required to exist)');
|
||||
|
||||
delete schemajs.properties.notIn;
|
||||
t.end();
|
||||
});
|
||||
|
|
|
@ -1,18 +1,19 @@
|
|||
var tap = require("tap");
|
||||
var schemajs = require('../schema');
|
||||
|
||||
tap.test("testing required", function(t)
|
||||
describe("required schemas", function()
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
var schemajs = require('../schema');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
it("required", function()
|
||||
{
|
||||
input: {type:'string', required:true},
|
||||
output: {type:'string'},
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'string', required:true},
|
||||
output: {type:'string'},
|
||||
});
|
||||
var input1 = schema.validate({input: 'username'});
|
||||
var input2 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
});
|
||||
var input1 = schema.validate({input: 'username'});
|
||||
var input2 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is valid (it is required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
|
421
test/types.js
421
test/types.js
|
@ -1,262 +1,239 @@
|
|||
var tap = require("tap");
|
||||
var schemajs = require('../schema');
|
||||
|
||||
tap.test("testing string type", function(t)
|
||||
describe("schema types", function()
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
var schemajs = require('../schema');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
it("string", function()
|
||||
{
|
||||
input: {type:'string'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'string'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'username'});
|
||||
var input2 = schema.validate({input: 112390123});
|
||||
var input3 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(input3.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'username'});
|
||||
var input2 = schema.validate({input: 112390123});
|
||||
var input3 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is invalid (not a string)');
|
||||
t.ok(input3.valid, 'input3 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing string+ type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("string+", function()
|
||||
{
|
||||
input: {type:'string+'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'string+'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'username'});
|
||||
var input2 = schema.validate({input: 112390123});
|
||||
var input3 = schema.validate({input: ''});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'username'});
|
||||
var input2 = schema.validate({input: 112390123});
|
||||
var input3 = schema.validate({input: ''});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is invalid (not a string)');
|
||||
t.ok(!input3.valid, 'input3 is invalid (not a non-empty string)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing number type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("number", function()
|
||||
{
|
||||
input: {type:'number'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'number'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 123123123});
|
||||
var input2 = schema.validate({input: '123123'});
|
||||
var input3 = schema.validate({input: 1231.3123123});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 123123123});
|
||||
var input2 = schema.validate({input: '123123'});
|
||||
var input3 = schema.validate({input: 1231.3123123});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is invalid (not a number)');
|
||||
t.ok(input3.valid, 'input3 is valid');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing alpha type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("alpha", function()
|
||||
{
|
||||
input: {type:'alpha'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'alpha'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'abcdefg'});
|
||||
var input2 = schema.validate({input: 'abcd123'});
|
||||
var input3 = schema.validate({input: 'what about spaces'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'abcdefg'});
|
||||
var input2 = schema.validate({input: 'abcd123'});
|
||||
var input3 = schema.validate({input: 'what about spaces'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is invalid (not an alpha)');
|
||||
t.ok(!input3.valid, 'input3 is invalid (not an alpha)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing alphanum type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("alphanum", function()
|
||||
{
|
||||
input: {type:'alphanum'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'alphanum'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'abcdefg'});
|
||||
var input2 = schema.validate({input: 'abcd123'});
|
||||
var input3 = schema.validate({input: 'what about spaces'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'abcdefg'});
|
||||
var input2 = schema.validate({input: 'abcd123'});
|
||||
var input3 = schema.validate({input: 'what about spaces'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(input2.valid, 'input2 is valid');
|
||||
t.ok(!input3.valid, 'input3 is invalid (not an alphanum)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing email type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("email", function()
|
||||
{
|
||||
input: {type:'email'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'email'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'zelda@hyrule.com'});
|
||||
var input2 = schema.validate({input: 'master sword'});
|
||||
var input3 = schema.validate({input: 'zelda@hyrule'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 'zelda@hyrule.com'});
|
||||
var input2 = schema.validate({input: 'master sword'});
|
||||
var input3 = schema.validate({input: 'zelda@hyrule'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is invalid (not an email)');
|
||||
t.ok(!input3.valid, 'input3 is invalid (not an email)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing object type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("object", function()
|
||||
{
|
||||
input: {type:'object'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'object'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: {patents:'needs reform'}});
|
||||
var input2 = schema.validate({input: 'microsoft'});
|
||||
var input3 = schema.validate({input: ['apple', 'oracle']});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: {patents:'needs reform'}});
|
||||
var input2 = schema.validate({input: 'microsoft'});
|
||||
var input3 = schema.validate({input: ['apple', 'oracle']});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is invalid (not an object)');
|
||||
t.ok(!input3.valid, 'input3 is invalid (not an object)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing array type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("array", function()
|
||||
{
|
||||
input: {type:'array'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'array'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: ['spiderman', 'magneto']});
|
||||
var input2 = schema.validate({input: 'superman'});
|
||||
var input3 = schema.validate({input: {xmen:'cyclops'}});
|
||||
var input4 = schema.validate({input: []});
|
||||
var input5 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
expect(input5.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: ['spiderman', 'magneto']});
|
||||
var input2 = schema.validate({input: 'superman'});
|
||||
var input3 = schema.validate({input: {xmen:'cyclops'}});
|
||||
var input4 = schema.validate({input: []});
|
||||
var input5 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is invalid (not an array)');
|
||||
t.ok(!input3.valid, 'input3 is invalid (not an array)');
|
||||
t.ok(input4.valid, 'input4 is valid');
|
||||
t.ok(input5.valid, 'input5 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing date type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("date", function()
|
||||
{
|
||||
input: {type:'date'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'date'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: new Date()});
|
||||
var input2 = schema.validate({input: '11-03-1980'});
|
||||
var input3 = schema.validate({input: 'yesterday'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: new Date()});
|
||||
var input2 = schema.validate({input: '11-03-1980'});
|
||||
var input3 = schema.validate({input: 'yesterday'});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is invalid (not a date)');
|
||||
t.ok(!input3.valid, 'input3 is invalid (not a date)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing number type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("int", function()
|
||||
{
|
||||
input: {type:'int'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'int'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 123123123});
|
||||
var input2 = schema.validate({input: '123123'});
|
||||
var input3 = schema.validate({input: 1231.3123123});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: 123123123});
|
||||
var input2 = schema.validate({input: '123123'});
|
||||
var input3 = schema.validate({input: 1231.3123123});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is invalid (not an int)');
|
||||
t.ok(!input3.valid, 'input3 is invalid (not an int)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing boolean type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("boolean", function()
|
||||
{
|
||||
input: {type:'boolean'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'boolean'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: true});
|
||||
var input2 = schema.validate({input: false});
|
||||
var input3 = schema.validate({input: 1});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(input2.valid).to.be.ok;
|
||||
expect(!input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: true});
|
||||
var input2 = schema.validate({input: false});
|
||||
var input3 = schema.validate({input: 1});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(input2.valid, 'input2 is valid');
|
||||
t.ok(!input3.valid, 'input3 is invalid (not a boolean)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing url type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("url", function()
|
||||
{
|
||||
link: {type:'url'}
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
link: {type:'url'}
|
||||
});
|
||||
|
||||
var link1 = schema.validate({link:'www.eleith.com'});
|
||||
var link2 = schema.validate({link:'https://eleith.com'});
|
||||
var link3 = schema.validate({link:'google'});
|
||||
var link4 = schema.validate({});
|
||||
|
||||
expect(link1.valid).to.be.ok;
|
||||
expect(link2.valid).to.be.ok;
|
||||
expect(!link3.valid).to.be.ok;
|
||||
expect(link4.valid).to.be.ok;
|
||||
});
|
||||
|
||||
var link1 = schema.validate({link: 'www.eleith.com'});
|
||||
var link2 = schema.validate({link: 'https://eleith.com'});
|
||||
var link3 = schema.validate({link: 'google'});
|
||||
var link4 = schema.validate({});
|
||||
|
||||
t.ok(link1.valid, 'link1 is valid');
|
||||
t.ok(link2.valid, 'link2 is valid');
|
||||
t.ok(!link3.valid, 'link3 is invalid (not an url)');
|
||||
t.ok(link4.valid, 'link4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
tap.test("testing zipcode type", function(t)
|
||||
{
|
||||
var schema = schemajs.create(
|
||||
it("zipcode", function()
|
||||
{
|
||||
input: {type:'zipcode'}
|
||||
});
|
||||
var schema = schemajs.create(
|
||||
{
|
||||
input: {type:'zipcode'}
|
||||
});
|
||||
|
||||
var input1 = schema.validate({input: '99025'});
|
||||
var input2 = schema.validate({input: '7745'});
|
||||
var input3 = schema.validate({input: 94302});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
var input1 = schema.validate({input: '99025'});
|
||||
var input2 = schema.validate({input: '7745'});
|
||||
var input3 = schema.validate({input: 94302});
|
||||
var input4 = schema.validate({});
|
||||
|
||||
t.ok(input1.valid, 'input1 is valid');
|
||||
t.ok(!input2.valid, 'input2 is invalid (not a zipcode)');
|
||||
t.ok(input3.valid, 'input3 is invalid (not a zipcode)');
|
||||
t.ok(input4.valid, 'input4 is valid (not required to exist)');
|
||||
|
||||
t.end();
|
||||
expect(input1.valid).to.be.ok;
|
||||
expect(!input2.valid).to.be.ok;
|
||||
expect(input3.valid).to.be.ok;
|
||||
expect(input4.valid).to.be.ok;
|
||||
});
|
||||
});
|
||||
|
|
Reference in New Issue