Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Performance related stuff

  • Loading branch information...
commit afdb388856fafef86d764aec1bfc645a34e5bbc3 1 parent e1be4ee
@tdegrunt authored
Showing with 43 additions and 26 deletions.
  1. +20 −7 lib/attribute.js
  2. +23 −19 lib/validator.js
View
27 lib/attribute.js
@@ -4,9 +4,22 @@ var helpers = require('./helpers');
var attribute = {};
-attribute.informativeProperties = ['id', 'default', 'description', 'title'];
-attribute.argumentProperties = ['exclusiveMinimum', 'exclusiveMaximum', 'items', 'additionalItems', 'properties', 'additionalProperties', 'patternProperties', 'extends'];
-attribute.ignoreProperties = [].concat(attribute.informativeProperties, attribute.argumentProperties);
+attribute.ignoreProperties = {
+ // informativeProperties
+ 'id': true,
+ 'default': true,
+ 'description': true,
+ 'title': true,
+ // argumentProperties
+ 'exclusiveMinimum': true,
+ 'exclusiveMaximum': true,
+ 'items': true,
+ 'additionalItems': true,
+ 'properties': true,
+ 'additionalProperties': true,
+ 'patternProperties': true,
+ 'extends': true
+};
/**
* @name validators
@@ -267,8 +280,8 @@ validators.maxItems = function validateMaxItems(instance, schema) {
* @return {boolean}
*/
function testArrays(v, i, a) {
- var j, len;
- for (j = i + 1, len = a.length; j < len; j++) {
+ var j, len = a.length;
+ for (j = i + 1, len; j < len; j++) {
if (helpers.deepCompareStrict(v, a[j])) {
return false;
}
@@ -316,8 +329,8 @@ validators.dependencies = function validateDependencies(instance, schema, option
dep = [dep];
}
if (dep instanceof Array) {
- var i, len;
- for (i = 0, len = dep.length; i < len; i++) {
+ var i, len = dep.length;
+ for (i = 0, len; i < len; i++) {
if (instance[dep[i]] === undefined) {
return " property " + dep[i] + " not found, required by " + propPath;
}
View
42 lib/validator.js
@@ -127,8 +127,10 @@ Validator.prototype.validateProperty = function validateProperty(instance, schem
var errors = [];
var skipAttributes = (options && options.skipAttributes) || [];
var keys = Object.keys(schema);
- keys.forEach(function (key) {
- if (skipAttributes.indexOf(key) < 0 && attribute.ignoreProperties.indexOf(key) < 0) {
+ var key;
+ for (var i = 0, len = keys.length; i < len; i++ ) {
+ key = keys[i];
+ if (!attribute.ignoreProperties[key] && skipAttributes.indexOf(key) < 0) {
var validatorErr;
var validator = attribute.validators[key];
if (validator) {
@@ -141,7 +143,7 @@ Validator.prototype.validateProperty = function validateProperty(instance, schem
errors.push(helpers.createError(schema, key, propertyName, validatorErr));
}
}
- });
+ }
return errors;
};
@@ -174,22 +176,24 @@ Validator.prototype.validateObject = function (instance, schema, options, proper
var property;
var errs;
- var lookedAtPatternProperties = [];
+ var lookedAtPatternProperties = {};
- var patternProperties = schema.patternProperties || {};
- var pattern;
- for (pattern in patternProperties) {
- if (patternProperties.hasOwnProperty(pattern)) {
- var expr = new RegExp(pattern);
- for (property in instance) {
- if (instance.hasOwnProperty(property)) {
- if (!expr.test(property)) {
- continue;
- }
- lookedAtPatternProperties.push(property);
- errs = this.validateSchema(instance[property], schema.patternProperties[pattern], options, propertyName + helpers.makeSuffix(property));
- if (errs && errs.length) {
- errors = errors.concat(errs);
+ if (schema.patternProperties) {
+ var patternProperties = schema.patternProperties || {};
+ var pattern;
+ for (pattern in patternProperties) {
+ if (patternProperties.hasOwnProperty(pattern)) {
+ var expr = new RegExp(pattern);
+ for (property in instance) {
+ if (instance.hasOwnProperty(property)) {
+ if (!expr.test(property)) {
+ continue;
+ }
+ lookedAtPatternProperties[property] = true;
+ errs = this.validateSchema(instance[property], schema.patternProperties[pattern], options, propertyName + helpers.makeSuffix(property));
+ if (errs && errs.length) {
+ errors = errors.concat(errs);
+ }
}
}
}
@@ -200,7 +204,7 @@ Validator.prototype.validateObject = function (instance, schema, options, proper
if (schema.additionalProperties !== undefined && schema.additionalProperties !== true) {
for (property in instance) {
if (instance.hasOwnProperty(property)) {
- if (properties[property] !== undefined || lookedAtPatternProperties.indexOf(property) >= 0) {
+ if (properties[property] !== undefined || lookedAtPatternProperties[property]) {
continue;
}
if (schema.additionalProperties === false) {
Please sign in to comment.
Something went wrong with that request. Please try again.