Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error in validating date-time #84

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 49 additions & 38 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var uri = require('url');

var ValidationError = exports.ValidationError = function ValidationError (message, instance, schema, propertyPath) {
var ValidationError = exports.ValidationError = function ValidationError(message, instance, schema, propertyPath) {
if (propertyPath) {
this.property = propertyPath;
}
Expand Down Expand Up @@ -48,60 +48,70 @@ ValidatorResult.prototype.importErrors = function importErrors(res) {
this.addError(res);
} else if (res && res.errors) {
var errs = this.errors;
res.errors.forEach(function (v) {
res.errors.forEach(function(v) {
errs.push(v)
});
}
};

ValidatorResult.prototype.toString = function toString(res) {
return this.errors.map(function(v,i){ return i+': '+v.toString()+'\n'; }).join('');
return this.errors.map(function(v, i) {
return i + ': ' + v.toString() + '\n';
}).join('');
};

Object.defineProperty(ValidatorResult.prototype, "valid", { get: function() {
return !this.errors.length;
} });
Object.defineProperty(ValidatorResult.prototype, "valid", {
get: function() {
return !this.errors.length;
}
});

/**
* Describes a problem with a Schema which prevents validation of an instance
* @name SchemaError
* @constructor
*/
var SchemaError = exports.SchemaError = function SchemaError (msg, schema) {
var SchemaError = exports.SchemaError = function SchemaError(msg, schema) {
this.message = msg;
this.schema = schema;
Error.call(this, msg);
Error.captureStackTrace(this, SchemaError);
};
SchemaError.prototype = Object.create(Error.prototype,
{ constructor: {value: SchemaError, enumerable: false}
, name: {value: 'SchemaError', enumerable: false}
});
SchemaError.prototype = Object.create(Error.prototype, {
constructor: {
value: SchemaError,
enumerable: false
},
name: {
value: 'SchemaError',
enumerable: false
}
});

var SchemaContext = exports.SchemaContext = function SchemaContext (schema, options, propertyPath, base, schemas) {
var SchemaContext = exports.SchemaContext = function SchemaContext(schema, options, propertyPath, base, schemas) {
this.schema = schema;
this.options = options;
this.propertyPath = propertyPath;
this.base = base;
this.schemas = schemas;
};

SchemaContext.prototype.resolve = function resolve (target) {
SchemaContext.prototype.resolve = function resolve(target) {
return uri.resolve(this.base, target);
};

SchemaContext.prototype.makeChild = function makeChild(schema, propertyName){
var propertyPath = (propertyName===undefined) ? this.propertyPath : this.propertyPath+makeSuffix(propertyName);
var base = uri.resolve(this.base, schema.id||'');
SchemaContext.prototype.makeChild = function makeChild(schema, propertyName) {
var propertyPath = (propertyName === undefined) ? this.propertyPath : this.propertyPath + makeSuffix(propertyName);
var base = uri.resolve(this.base, schema.id || '');
var ctx = new SchemaContext(schema, this.options, propertyPath, base, Object.create(this.schemas));
if(schema.id && !ctx.schemas[base]){
if (schema.id && !ctx.schemas[base]) {
ctx.schemas[base] = schema;
}
return ctx;
}

var FORMAT_REGEXPS = exports.FORMAT_REGEXPS = {
'date-time': /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}[tT ]\d{2}:\d{2}:\d{2}(\.\d+)?([zZ]|[+-]\d{2}:\d{2})$/,
'date-time': /^\d{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1}[Tt]{0,1}[ ]{0,1}\d{2}:\d{2}:\d{2}((\.*\d{0,7}[Zz]{0,1}$)|(\.*\d{0,7}[\+\-]\d{0,2}:\d{0,2}$)|(\.*\d{0,7}$)|$|[Zz]{0,1}$)/,
'date': /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}$/,
'time': /^\d{2}:\d{2}:\d{2}$/,

Expand All @@ -116,10 +126,10 @@ var FORMAT_REGEXPS = exports.FORMAT_REGEXPS = {

'alpha': /^[a-zA-Z]+$/,
'alphanumeric': /^[a-zA-Z0-9]+$/,
'utc-millisec': function (input) {
'utc-millisec': function(input) {
return (typeof input === 'string') && parseFloat(input) === parseInt(input, 10) && !isNaN(input);
},
'regex': function (input) {
'regex': function(input) {
var result = true;
try {
new RegExp(input);
Expand All @@ -136,7 +146,7 @@ FORMAT_REGEXPS.regexp = FORMAT_REGEXPS.regex;
FORMAT_REGEXPS.pattern = FORMAT_REGEXPS.regex;
FORMAT_REGEXPS.ipv4 = FORMAT_REGEXPS['ip-address'];

exports.isFormat = function isFormat (input, format) {
exports.isFormat = function isFormat(input, format) {
if (FORMAT_REGEXPS[format] !== undefined) {
if (FORMAT_REGEXPS[format] instanceof RegExp) {
return FORMAT_REGEXPS[format].test(input);
Expand All @@ -148,7 +158,7 @@ exports.isFormat = function isFormat (input, format) {
return false;
};

var makeSuffix = exports.makeSuffix = function makeSuffix (key) {
var makeSuffix = exports.makeSuffix = function makeSuffix(key) {
key = key.toString();
// This function could be capable of outputting valid a ECMAScript string, but the
// resulting code for testing which form to use would be tens of thousands of characters long
Expand All @@ -162,7 +172,7 @@ var makeSuffix = exports.makeSuffix = function makeSuffix (key) {
return '[' + JSON.stringify(key) + ']';
};

exports.deepCompareStrict = function deepCompareStrict (a, b) {
exports.deepCompareStrict = function deepCompareStrict(a, b) {
if (typeof a !== typeof b) {
return false;
}
Expand All @@ -173,7 +183,7 @@ exports.deepCompareStrict = function deepCompareStrict (a, b) {
if (a.length !== b.length) {
return false;
}
return a.every(function (v, i) {
return a.every(function(v, i) {
return deepCompareStrict(a[i], b[i]);
});
}
Expand All @@ -186,21 +196,21 @@ exports.deepCompareStrict = function deepCompareStrict (a, b) {
if (aKeys.length !== bKeys.length) {
return false;
}
return aKeys.every(function (v) {
return aKeys.every(function(v) {
return deepCompareStrict(a[v], b[v]);
});
}
return a === b;
};

module.exports.deepMerge = function deepMerge (target, src) {
module.exports.deepMerge = function deepMerge(target, src) {
var array = Array.isArray(src);
var dst = array && [] || {};

if (array) {
target = target || [];
dst = dst.concat(target);
src.forEach(function (e, i) {
src.forEach(function(e, i) {
if (typeof e === 'object') {
dst[i] = deepMerge(target[i], e)
} else {
Expand All @@ -211,15 +221,14 @@ module.exports.deepMerge = function deepMerge (target, src) {
});
} else {
if (target && typeof target === 'object') {
Object.keys(target).forEach(function (key) {
Object.keys(target).forEach(function(key) {
dst[key] = target[key];
});
}
Object.keys(src).forEach(function (key) {
Object.keys(src).forEach(function(key) {
if (typeof src[key] !== 'object' || !src[key]) {
dst[key] = src[key];
}
else {
} else {
if (!target[key]) {
dst[key] = src[key];
} else {
Expand All @@ -242,8 +251,8 @@ module.exports.deepMerge = function deepMerge (target, src) {
exports.objectGetPath = function objectGetPath(o, s) {
var parts = s.split('/').slice(1);
var k;
while (typeof (k=parts.shift()) == 'string') {
var n = decodeURIComponent(k.replace(/~0/,'~').replace(/~1/g,'/'));
while (typeof(k = parts.shift()) == 'string') {
var n = decodeURIComponent(k.replace(/~0/, '~').replace(/~1/g, '/'));
if (!(n in o)) return;
o = o[n];
}
Expand All @@ -255,8 +264,10 @@ exports.objectGetPath = function objectGetPath(o, s) {
* @param Array a
* @return {String}
*/
exports.encodePath = function encodePointer(a){
// ~ must be encoded explicitly because hacks
// the slash is encoded by encodeURIComponent
return a.map(function(v){ return '/'+encodeURIComponent(v).replace(/~/g,'%7E'); }).join('');
}
exports.encodePath = function encodePointer(a) {
// ~ must be encoded explicitly because hacks
// the slash is encoded by encodeURIComponent
return a.map(function(v) {
return '/' + encodeURIComponent(v).replace(/~/g, '%7E');
}).join('');
}
Loading