Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #5 from colinf/formats

Conflicts:
	lib/revalidator.js
  • Loading branch information...
commit fd581bda1663ed5ef0b35be4e3ed9e531f157e5d 2 parents 7806f9e + 0b9419c
@pksunkara pksunkara authored
Showing with 33 additions and 26 deletions.
  1. +24 −25 lib/revalidator.js
  2. +9 −1 test/validator-test.js
View
49 lib/revalidator.js
@@ -1,7 +1,7 @@
(function (exports) {
exports.validate = validate;
exports.mixin = mixin;
-
+
//
// ### function validate (object, schema, options)
// #### {Object} object the object to validate.
@@ -35,13 +35,13 @@
// TODO: self-described validation
// if (! options.selfDescribing) { ... }
//
-
+
return {
valid: !(errors.length),
errors: errors
};
};
-
+
/**
* Default validation options. Defaults can be overridden by
* passing an 'options' hash to {@link #validate}. They can
@@ -96,11 +96,11 @@
/**
*
*/
- validate.formats = {
+ validate.formats = {
'email': /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,
- 'ip-address': /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i,
- 'ipv6': /^([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}$/,
- 'date-time': /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/,
+ 'ip-address': /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i,
+ 'ipv6': /^([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}$/,
+ 'date-time': /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/,
'date': /^\d{4}-\d{2}-\d{2}$/,
'time': /^\d{2}:\d{2}:\d{2}$/,
'color': /^#[a-z0-9]{6}|#[a-z0-9]{3}|(?:rgb\(\s*(?:[+-]?\d+%?)\s*,\s*(?:[+-]?\d+%?)\s*,\s*(?:[+-]?\d+%?)\s*\))aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|and yellow$/i,
@@ -108,17 +108,17 @@
//'phone': (not supported)
//'uri': (not supported)
//'host-name': (not supported)
- 'utc-millisec': {
- test: function (value) {
- return typeof(value) === 'number' && value >= 0;
+ 'utc-millisec': {
+ test: function (value) {
+ return typeof(value) === 'number' && value >= 0;
}
- },
- 'regex': {
- test: function (value) {
- try { new RegExp(value) }
- catch (e) { return false }
-
- return true;
+ },
+ 'regex': {
+ test: function (value) {
+ try { new RegExp(value) }
+ catch (e) { return false }
+
+ return true;
}
}
};
@@ -126,27 +126,27 @@
/**
*
*/
- validate.formatExtensions = {
+ validate.formatExtensions = {
'url': /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
};
-
+
function mixin(obj) {
var sources = Array.prototype.slice.call(arguments, 1);
while (sources.length) {
var source = sources.shift();
if (!source) { continue }
-
+
if (typeof(source) !== 'object') {
throw new TypeError('mixin non-object');
}
-
+
for (var p in source) {
if (source.hasOwnProperty(p)) {
obj[p] = source[p];
}
}
}
-
+
return obj;
};
@@ -206,12 +206,11 @@
format = schema.format;
if (options.validateFormatExtensions) { spec = validate.formatExtensions[format] }
+ if (!spec) { spec = validate.formats[format] }
if (!spec) {
- spec = validate.formats[format];
if (options.validateFormatsStrict) {
return error('format', property, value, schema, errors);
}
-
}
else {
if (!spec.test(value)) {
@@ -223,7 +222,7 @@
if (schema.enum && schema.enum.indexOf(value) === -1) {
error('enum', property, value, schema, errors);
}
-
+
// Dependencies (see 5.8)
if (typeof schema.dependencies === 'string' &&
object[schema.dependencies] === undefined) {
View
10 test/validator-test.js
@@ -197,7 +197,7 @@ vows.describe('revalidator', {
"and an object": {
topic: {
title: 'Gimme some Gurus',
- date: new(Date)().toUTCString(),
+ date: '2012-02-04',
body: "And I will pwn your codex.",
tags: ['energy drinks', 'code'],
author: 'cloudhead',
@@ -265,6 +265,14 @@ vows.describe('revalidator', {
},
"return an object with `valid` set to false": assertInvalid
},
+ "and if it has a incorrect format (date)": {
+ topic: function (object, schema) {
+ object = clone(object);
+ object.date = 'bad date';
+ return revalidator.validate(object, schema);
+ },
+ "return an object with `valid` set to false": assertInvalid
+ },
"and if it didn't validate a pattern": {
topic: function (object, schema) {
object = clone(object);
Please sign in to comment.
Something went wrong with that request. Please try again.