Skip to content

Commit

Permalink
Merge pull request #1241 from ajv-validator/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
epoberezkin committed Jul 1, 2020
2 parents 140cfa6 + 1105fd5 commit 332b30d
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 97 deletions.
38 changes: 0 additions & 38 deletions lib/compile/util.js
Expand Up @@ -13,8 +13,6 @@ module.exports = {
ucs2length: require('./ucs2length'),
varOccurences: varOccurences,
varReplace: varReplace,
cleanUpCode: cleanUpCode,
finalCleanUpCode: finalCleanUpCode,
schemaHasRules: schemaHasRules,
schemaHasRulesExcept: schemaHasRulesExcept,
schemaUnknownRules: schemaUnknownRules,
Expand Down Expand Up @@ -139,42 +137,6 @@ function varReplace(str, dataVar, expr) {
}


var EMPTY_ELSE = /else\s*{\s*}/g
, EMPTY_IF_NO_ELSE = /if\s*\([^)]+\)\s*\{\s*\}(?!\s*else)/g
, EMPTY_IF_WITH_ELSE = /if\s*\(([^)]+)\)\s*\{\s*\}\s*else(?!\s*if)/g;
function cleanUpCode(out) {
return out.replace(EMPTY_ELSE, '')
.replace(EMPTY_IF_NO_ELSE, '')
.replace(EMPTY_IF_WITH_ELSE, 'if (!($1))');
}


var ERRORS_REGEXP = /[^v.]errors/g
, REMOVE_ERRORS = /var errors = 0;|var vErrors = null;|validate.errors = vErrors;/g
, REMOVE_ERRORS_ASYNC = /var errors = 0;|var vErrors = null;/g
, RETURN_VALID = 'return errors === 0;'
, RETURN_TRUE = 'validate.errors = null; return true;'
, RETURN_ASYNC = /if \(errors === 0\) return data;\s*else throw new ValidationError\(vErrors\);/
, RETURN_DATA_ASYNC = 'return data;'
, ROOTDATA_REGEXP = /[^A-Za-z_$]rootData[^A-Za-z0-9_$]/g
, REMOVE_ROOTDATA = /if \(rootData === undefined\) rootData = data;/;

function finalCleanUpCode(out, async) {
var matches = out.match(ERRORS_REGEXP);
if (matches && matches.length == 2) {
out = async
? out.replace(REMOVE_ERRORS_ASYNC, '')
.replace(RETURN_ASYNC, RETURN_DATA_ASYNC)
: out.replace(REMOVE_ERRORS, '')
.replace(RETURN_VALID, RETURN_TRUE);
}

matches = out.match(ROOTDATA_REGEXP);
if (!matches || matches.length !== 3) return out;
return out.replace(REMOVE_ROOTDATA, '');
}


function schemaHasRules(schema, rules) {
if (typeof schema == 'boolean') return !schema;
for (var key in schema) if (rules[key]) return true;
Expand Down
9 changes: 9 additions & 0 deletions lib/dot/_limit.jst
Expand Up @@ -17,6 +17,15 @@
, $op = $isMax ? '<' : '>'
, $notOp = $isMax ? '>' : '<'
, $errorKeyword = undefined;

if (!($isData || typeof $schema == 'number' || $schema === undefined)) {
throw new Error($keyword + ' must be number');
}
if (!($isDataExcl || $schemaExcl === undefined
|| typeof $schemaExcl == 'number'
|| typeof $schemaExcl == 'boolean')) {
throw new Error($exclusiveKeyword + ' must be number or boolean');
}
}}

{{? $isDataExcl }}
Expand Down
2 changes: 2 additions & 0 deletions lib/dot/_limitItems.jst
Expand Up @@ -3,6 +3,8 @@
{{# def.setupKeyword }}
{{# def.$data }}

{{# def.numberKeyword }}

{{ var $op = $keyword == 'maxItems' ? '>' : '<'; }}
if ({{# def.$dataNotType:'number' }} {{=$data}}.length {{=$op}} {{=$schemaValue}}) {
{{ var $errorKeyword = $keyword; }}
Expand Down
2 changes: 2 additions & 0 deletions lib/dot/_limitLength.jst
Expand Up @@ -3,6 +3,8 @@
{{# def.setupKeyword }}
{{# def.$data }}

{{# def.numberKeyword }}

{{ var $op = $keyword == 'maxLength' ? '>' : '<'; }}
if ({{# def.$dataNotType:'number' }} {{# def.strLength }} {{=$op}} {{=$schemaValue}}) {
{{ var $errorKeyword = $keyword; }}
Expand Down
2 changes: 2 additions & 0 deletions lib/dot/_limitProperties.jst
Expand Up @@ -3,6 +3,8 @@
{{# def.setupKeyword }}
{{# def.$data }}

{{# def.numberKeyword }}

{{ var $op = $keyword == 'maxProperties' ? '>' : '<'; }}
if ({{# def.$dataNotType:'number' }} Object.keys({{=$data}}).length {{=$op}} {{=$schemaValue}}) {
{{ var $errorKeyword = $keyword; }}
Expand Down
2 changes: 0 additions & 2 deletions lib/dot/allOf.jst
Expand Up @@ -30,5 +30,3 @@
{{= $closingBraces.slice(0,-1) }}
{{?}}
{{?}}

{{# def.cleanUp }}
2 changes: 0 additions & 2 deletions lib/dot/anyOf.jst
Expand Up @@ -39,8 +39,6 @@
} else {
{{# def.resetErrors }}
{{? it.opts.allErrors }} } {{?}}

{{# def.cleanUp }}
{{??}}
{{? $breakOnError }}
if (true) {
Expand Down
2 changes: 0 additions & 2 deletions lib/dot/contains.jst
Expand Up @@ -53,5 +53,3 @@ var {{=$valid}};
{{# def.resetErrors }}
{{?}}
{{? it.opts.allErrors }} } {{?}}

{{# def.cleanUp }}
13 changes: 7 additions & 6 deletions lib/dot/definitions.def
Expand Up @@ -112,12 +112,6 @@
#}}


{{## def.cleanUp: {{ out = it.util.cleanUpCode(out); }} #}}


{{## def.finalCleanUp: {{ out = it.util.finalCleanUpCode(out, $async); }} #}}


{{## def.$data:
{{
var $isData = it.opts.$data && $schema && $schema.$data
Expand All @@ -144,6 +138,13 @@
#}}


{{## def.numberKeyword:
{{? !($isData || typeof $schema == 'number') }}
{{ throw new Error($keyword + ' must be number'); }}
{{?}}
#}}


{{## def.beginDefOut:
{{
var $$outStack = $$outStack || [];
Expand Down
2 changes: 0 additions & 2 deletions lib/dot/dependencies.jst
Expand Up @@ -76,5 +76,3 @@ var missing{{=$lvl}};
{{= $closingBraces }}
if ({{=$errs}} == errors) {
{{?}}

{{# def.cleanUp }}
2 changes: 0 additions & 2 deletions lib/dot/if.jst
Expand Up @@ -65,8 +65,6 @@
{{# def.extraError:'if' }}
}
{{? $breakOnError }} else { {{?}}

{{# def.cleanUp }}
{{??}}
{{? $breakOnError }}
if (true) {
Expand Down
2 changes: 0 additions & 2 deletions lib/dot/items.jst
Expand Up @@ -96,5 +96,3 @@ var {{=$valid}};
{{= $closingBraces }}
if ({{=$errs}} == errors) {
{{?}}

{{# def.cleanUp }}
11 changes: 6 additions & 5 deletions lib/dot/properties.jst
Expand Up @@ -28,9 +28,9 @@
, $nextData = 'data' + $dataNxt
, $dataProperties = 'dataProperties' + $lvl;

var $schemaKeys = Object.keys($schema || {})
var $schemaKeys = Object.keys($schema || {}).filter(notProto)
, $pProperties = it.schema.patternProperties || {}
, $pPropertyKeys = Object.keys($pProperties)
, $pPropertyKeys = Object.keys($pProperties).filter(notProto)
, $aProperties = it.schema.additionalProperties
, $someProperties = $schemaKeys.length || $pPropertyKeys.length
, $noAdditional = $aProperties === false
Expand All @@ -42,8 +42,11 @@
, $currentBaseId = it.baseId;

var $required = it.schema.required;
if ($required && !(it.opts.$data && $required.$data) && $required.length < it.opts.loopRequired)
if ($required && !(it.opts.$data && $required.$data) && $required.length < it.opts.loopRequired) {
var $requiredHash = it.util.toHash($required);
}

function notProto(p) { return p !== '__proto__'; }
}}


Expand Down Expand Up @@ -240,5 +243,3 @@ var {{=$nextValid}} = true;
{{= $closingBraces }}
if ({{=$errs}} == errors) {
{{?}}

{{# def.cleanUp }}
2 changes: 0 additions & 2 deletions lib/dot/propertyNames.jst
Expand Up @@ -50,5 +50,3 @@ var {{=$errs}} = errors;
{{= $closingBraces }}
if ({{=$errs}} == errors) {
{{?}}

{{# def.cleanUp }}
6 changes: 0 additions & 6 deletions lib/dot/validate.jst
Expand Up @@ -254,12 +254,6 @@
var {{=$valid}} = errors === errs_{{=$lvl}};
{{?}}

{{# def.cleanUp }}

{{? $top }}
{{# def.finalCleanUp }}
{{?}}

{{
function $shouldUseGroup($rulesGroup) {
var rules = $rulesGroup.rules;
Expand Down
44 changes: 44 additions & 0 deletions spec/ajv.spec.js
Expand Up @@ -512,5 +512,49 @@ describe('Ajv', function () {
});
});
});

describe('sub-schema validation outside of definitions during compilation', function() {
it('maximum', function() {
passValidationThrowCompile({
$ref: '#/foo',
foo: {maximum: 'bar'}
});
});

it('exclusiveMaximum', function() {
passValidationThrowCompile({
$ref: '#/foo',
foo: {exclusiveMaximum: 'bar'}
});
});

it('maxItems', function() {
passValidationThrowCompile({
$ref: '#/foo',
foo: {maxItems: 'bar'}
});
});

it('maxLength', function() {
passValidationThrowCompile({
$ref: '#/foo',
foo: {maxLength: 'bar'}
});
});

it('maxProperties', function() {
passValidationThrowCompile({
$ref: '#/foo',
foo: {maxProperties: 'bar'}
});
});

function passValidationThrowCompile(schema) {
ajv.validateSchema(schema) .should.equal(true);
should.throw(function() {
ajv.compile(schema);
});
}
});
});
});
28 changes: 0 additions & 28 deletions spec/issues/388_code_clean-up.spec.js

This file was deleted.

0 comments on commit 332b30d

Please sign in to comment.