added tests for all properties/filters/types, fixes in code to ensure all tests pass, bump version

This commit is contained in:
eleith 2011-11-14 22:43:55 -08:00
parent d426db9dc0
commit 31954d48b3
4 changed files with 100 additions and 57 deletions

View File

@ -1,4 +1,4 @@
#v0.1.2
#v0.1.4
### schemajs
@ -120,6 +120,7 @@ schema with embedded schemas for object and array types
[string+, string (empty string allowed), alpha, alphanum, email, object, array, date, number, int, boolean, url, zipcode]
to extend, add a function onto schema.types that accepts a value and returns a boolean if the type matches
test is run AFTER schema.filters are run
schema.types.awesome = function(value)

View File

@ -212,17 +212,17 @@ var is =
'alphanum': function(value)
{
return (/^[a-z0-9]+$/i).test(value);
return (/^[a-zA-Z0-9]+$/i).test(value);
},
'alpha': function(value)
{
return (/^[a-z]+$/i).test(value);
return (/^[a-zA-Z]+$/i).test(value);
},
'object': function(value)
{
return typeof(value) == 'object';
return typeof(value) == 'object' && !Array.isArray(value);
},
'array': function(value)
@ -232,7 +232,8 @@ var is =
'date': function(value)
{
return _.isDate(value);
// getTime() allows us to check if date is valid
return _.isDate(value) && !isNaN(value.getTime());
},
'number': function(value)

View File

@ -1,9 +1,9 @@
{
"name": "schemajs",
"description": "validate objects (including http request params) agains a schema. includes express middleware",
"version": "0.1.3",
"version": "0.1.4",
"author": "eleith",
"contributors": [],
"contributors": ["andychilton"],
"repository":
{
"type": "git",

View File

@ -1,57 +1,98 @@
var tap = require("tap"),
test = tap.test,
plan = tap.plan;
var tap = require("tap");
var schemajs = require('../schema');
var schemajs = require('../schema');
tap.test("testing minimum string length property", function(t)
{
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({});
test("test minimum string length", function (t) {
var schema = schemajs.create({
username : { type : 'string', properties : { min : 2 } },
});
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)');
var user1 = schema.validate({
username : 'username',
});
t.ok(user1.valid, 'user1a has valid username length')
var user2 = schema.validate({
username : 'a',
});
t.ok(!user2.valid, 'user2 has invalid username length (too short)')
var user3 = schema.validate({
username : 'ab',
});
t.ok(user3.valid, 'user3 has valid username length (same as min)')
var user4 = schema.validate({});
t.ok(user4.valid, 'user4 has no username (so checking does not happen and therefore it is valid)')
t.end();
t.end();
});
test("test maximum string length", function (t) {
var schema = schemajs.create({
username : { type : 'string', properties : { max : 16 } },
});
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({});
var user1 = schema.validate({
username : 'username',
});
t.ok(user1.valid, 'user1a has valid username length')
var user2 = schema.validate({
username : '01234567890abcdefg',
});
t.ok(!user2.valid, 'user2 has invalid username length (too long)')
var user3 = schema.validate({
username : '0123456789abcdef',
});
t.ok(user3.valid, 'user3 has valid username length (same as max)')
var user4 = schema.validate({});
t.ok(user4.valid, 'user4 has no username (so checking does not happen and therefore it is valid)')
t.end();
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)
{
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({});
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();
});