Skip to content

Commit

Permalink
Various optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
bugventure committed Apr 29, 2015
1 parent 6582cdb commit 3ce9015
Showing 1 changed file with 69 additions and 23 deletions.
92 changes: 69 additions & 23 deletions lib/jsen.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ types.integer = function (path) {
};

types.array = function (path) {
return 'Array.isArray(' + path + ')';
return path + ' !== undefined && Array.isArray(' + path + ')';
};

types.object = function (path) {
return 'typeof ' + path + ' === "object" && !!' + path + ' && !Array.isArray(' + path + ')';
return path + ' !== undefined && typeof ' + path + ' === "object" && !!' + path + ' && !Array.isArray(' + path + ')';
};

keywords.type = function (context) {
Expand Down Expand Up @@ -276,9 +276,40 @@ keywords.required = function (context) {
}
};

keywords.properties =
keywords.patternProperties =
keywords.additionalProperties = function (context) {
keywords.properties = function (context) {
if (context.validatedProperties) {
// prevent multiple generations of property validation
return;
}

var props = context.schema.properties,
propKeys = type(props) === 'object' ? Object.keys(props) : [],
patProps = context.schema.patternProperties,
patterns = type(patProps) === 'object' ? Object.keys(patProps) : [],
addProps = context.schema.additionalProperties,
addPropsCheck = addProps === false || type(addProps) === 'object',
prop, i;

// do not use this generator if we have patternProperties or additionalProperties
// instead, the generator below will be used for all three keywords
if (!propKeys.length || patterns.length || addPropsCheck) {
return;
}

for (i = 0; i < propKeys.length; i++) {
prop = propKeys[i];

context.code('if (' + context.path + '.' + prop + ' !== undefined) {');

context.validate(context.path + '.' + prop, props[prop]);

context.code('}');
}

context.validatedProperties = true;
};

keywords.patternProperties = keywords.additionalProperties = function (context) {
if (context.validatedProperties) {
// prevent multiple generations of this function
return;
Expand All @@ -290,20 +321,29 @@ keywords.additionalProperties = function (context) {
patterns = type(patProps) === 'object' ? Object.keys(patProps) : [],
addProps = context.schema.additionalProperties,
addPropsCheck = addProps === false || type(addProps) === 'object',
key, found,
keys, key, n, found,
propKey, pattern, i;

if (!propKeys.length && !patterns.length && !addPropsCheck) {
return;
}

keys = context.declare('[]');
key = context.declare('""');
n = context.declare(0);

if (addPropsCheck) {
found = context.declare(false);
}

context.code('for (' + key + ' in ' + context.path + ') {');
context.code(keys + ' = Object.keys(' + context.path + ')');

context.code('for (' + n + '; ' + n + ' < ' + keys + '.length; ' + n + '++) {')
(key + ' = ' + keys + '[' + n + ']')

('if (' + context.path + '[' + key + '] === undefined) {')
('continue')
('}');

if (addPropsCheck) {
context.code(found + ' = false');
Expand All @@ -319,7 +359,7 @@ keywords.additionalProperties = function (context) {
context.code(found + ' = true');
}

context.validate(context.path + '[' + key + ']', props[propKey]);
context.validate(context.path + '.' + propKey, props[propKey]);

context.code('}');
}
Expand Down Expand Up @@ -370,7 +410,7 @@ keywords.dependencies = function (context) {
for (key in context.schema.dependencies) {
dep = context.schema.dependencies[key];

context.code('if (' + context.path + '["' + key + '"] !== undefined) {');
context.code('if (' + context.path + '.' + key + ' !== undefined) {');

if (type(dep) === 'object') {
//schema dependency
Expand All @@ -379,7 +419,7 @@ keywords.dependencies = function (context) {
else {
// property dependency
for (i; i < dep.length; i++) {
context.code('if (' + context.path + '["' + dep[i] + '"] === undefined) {');
context.code('if (' + context.path + '.' + dep[i] + ' === undefined) {');
context.error('dependencies', dep[i]);
context.code('}');
}
Expand Down Expand Up @@ -409,16 +449,16 @@ keywords.anyOf = function (context) {
found = context.declare(false),
i = 0;

context.code(initialCount + ' = errors.length');
context.code(initialCount + ' = err');

for (; i < context.schema.anyOf.length; i++) {
context.code('if (!' + found + ') {');

context.code(errCount + ' = errors.length');
context.code(errCount + ' = err');

context.validate(context.path, context.schema.anyOf[i]);

context.code(found + ' = errors.length === ' + errCount)
context.code(found + ' = err === ' + errCount)
('}');
}

Expand All @@ -427,7 +467,7 @@ keywords.anyOf = function (context) {
context.error('anyOf');

context.code('} else {')
('errors.length = ' + initialCount)
('err = ' + initialCount)
('}');
};

Expand All @@ -441,14 +481,14 @@ keywords.oneOf = function (context) {
errCount = context.declare(0),
i = 0;

context.code(initialCount + ' = errors.length');
context.code(initialCount + ' = err');

for (; i < context.schema.oneOf.length; i++) {
context.code(errCount + ' = errors.length');
context.code(errCount + ' = err');

context.validate(context.path, context.schema.oneOf[i]);

context.code('if (errors.length === ' + errCount + ') {')
context.code('if (err === ' + errCount + ') {')
(matching + '++')
('}');
}
Expand All @@ -458,7 +498,7 @@ keywords.oneOf = function (context) {
context.error('oneOf');

context.code('} else {')
('errors.length = ' + initialCount)
('err = ' + initialCount)
('}');
};

Expand All @@ -469,16 +509,16 @@ keywords.not = function (context) {

var errCount = context.declare(0);

context.code(errCount + ' = errors.length');
context.code(errCount + ' = err');

context.validate(context.path, context.schema.not);

context.code('if (errors.length === ' + errCount + ') {');
context.code('if (err === ' + errCount + ') {');

context.error('not');

context.code('} else {')
('errors.length = ' + errCount)
('err = ' + errCount)
('}');
};

Expand Down Expand Up @@ -575,7 +615,10 @@ function jsen(schema) {

function validate(path, schema) {
function error(keyword) {
code('errors.push({ path: "' + path + '", keyword: "' + keyword + '" })');
// code('errors.push({ path: "' + path + '", keyword: "' + keyword + '" })');
code('err++');
// if (path !== 'data') code('console.log("' + path + '")');
// code('return false');
}

if (schema.$ref !== undefined) {
Expand Down Expand Up @@ -621,11 +664,14 @@ function jsen(schema) {
}

var code = func('data')
('var err = 0')
('errors.length = 0');

validate('data', schema);

code('return !errors.length');
code('return err === 0');

// console.log(code.toSource());

return code.compile(scope);
}
Expand Down

0 comments on commit 3ce9015

Please sign in to comment.