Skip to content

Commit

Permalink
refactor: move max*, min* keywords to the new definition format witho…
Browse files Browse the repository at this point in the history
…ut doT templates
  • Loading branch information
epoberezkin committed Aug 9, 2020
1 parent 971d9f9 commit 43d80f6
Show file tree
Hide file tree
Showing 30 changed files with 106 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
@@ -1,6 +1,7 @@
extends: eslint:recommended
parserOptions:
ecmaVersion: 6
sourceType: module
env:
node: true
browser: true
Expand All @@ -24,7 +25,6 @@ rules:
no-trailing-spaces: 2
no-use-before-define: [2, nofunc]
semi: 0
strict: [2, global]
valid-jsdoc: [2, requireReturn: false]
no-control-regex: 0
no-useless-escape: 2
10 changes: 4 additions & 6 deletions lib/ajv.js
@@ -1,5 +1,3 @@
"use strict"

var compileSchema = require("./compile"),
resolve = require("./compile/resolve"),
Cache = require("./cache"),
Expand Down Expand Up @@ -27,10 +25,10 @@ Ajv.prototype._compile = _compile

Ajv.prototype.compileAsync = require("./compile/async")
var customKeyword = require("./keyword")
Ajv.prototype.addKeyword = customKeyword.add
Ajv.prototype.getKeyword = customKeyword.get
Ajv.prototype.removeKeyword = customKeyword.remove
Ajv.prototype.validateKeyword = customKeyword.validate
Ajv.prototype.addKeyword = customKeyword.addKeyword
Ajv.prototype.getKeyword = customKeyword.getKeyword
Ajv.prototype.removeKeyword = customKeyword.removeKeyword
Ajv.prototype.validateKeyword = customKeyword.validateKeyword

var errorClasses = require("./compile/error_classes")
Ajv.ValidationError = errorClasses.Validation
Expand Down
2 changes: 0 additions & 2 deletions lib/cache.js
@@ -1,5 +1,3 @@
"use strict"

module.exports = Cache

function Cache() {
Expand Down
2 changes: 0 additions & 2 deletions lib/compile/async.js
@@ -1,5 +1,3 @@
"use strict"

var MissingRefError = require("./error_classes").MissingRef

module.exports = compileAsync
Expand Down
2 changes: 0 additions & 2 deletions lib/compile/equal.js
@@ -1,5 +1,3 @@
"use strict"

// do NOT remove this file - it would break pre-compiled schemas
// https://github.com/ajv-validator/ajv/issues/889
module.exports = require("fast-deep-equal")
2 changes: 0 additions & 2 deletions lib/compile/error_classes.js
@@ -1,5 +1,3 @@
"use strict"

var resolve = require("./resolve")

module.exports = {
Expand Down
2 changes: 0 additions & 2 deletions lib/compile/index.js
@@ -1,5 +1,3 @@
"use strict"

var resolve = require("./resolve"),
util = require("./util"),
errorClasses = require("./error_classes"),
Expand Down
2 changes: 0 additions & 2 deletions lib/compile/resolve.js
@@ -1,5 +1,3 @@
"use strict"

var URI = require("uri-js"),
equal = require("fast-deep-equal"),
util = require("./util"),
Expand Down
8 changes: 2 additions & 6 deletions lib/compile/rules.js
@@ -1,5 +1,3 @@
"use strict"

var ruleModules = require("../dotjs"),
toHash = require("./util").toHash

Expand All @@ -9,16 +7,14 @@ module.exports = function rules() {
type: "number",
rules: ["multipleOf", "format"],
},
{type: "string", rules: ["maxLength", "minLength", "pattern", "format"]},
{type: "string", rules: ["pattern", "format"]},
{
type: "array",
rules: ["maxItems", "minItems", "items", "contains", "uniqueItems"],
rules: ["items", "contains", "uniqueItems"],
},
{
type: "object",
rules: [
"maxProperties",
"minProperties",
"required",
"dependencies",
"propertyNames",
Expand Down
2 changes: 0 additions & 2 deletions lib/compile/schema_obj.js
@@ -1,5 +1,3 @@
"use strict"

var util = require("./util")

module.exports = SchemaObject
Expand Down
2 changes: 0 additions & 2 deletions lib/compile/ucs2length.js
@@ -1,5 +1,3 @@
"use strict"

// https://mathiasbynens.be/notes/javascript-encoding
// https://github.com/bestiejs/punycode.js - punycode.ucs2.decode
module.exports = function ucs2length(str) {
Expand Down
4 changes: 2 additions & 2 deletions lib/compile/util.js
@@ -1,5 +1,3 @@
"use strict"

module.exports = {
copy: copy,
checkDataType: checkDataType,
Expand All @@ -25,6 +23,8 @@ module.exports = {
escapeJsonPointer: escapeJsonPointer,
appendSchema: ($data, schemaCode) =>
$data ? `" + ${schemaCode}` : `${schemaCode}"`,
concatSchema: ($data, schemaCode) =>
$data ? `" + ${schemaCode} + "` : schemaCode,
dataNotType: ($data, schemaCode, schemaType) =>
$data
? `(${schemaCode}!==undefined && typeof ${schemaCode}!=="${schemaType}") || `
Expand Down
2 changes: 0 additions & 2 deletions lib/data.js
@@ -1,5 +1,3 @@
"use strict"

// TODO use $data in keyword definitions
var KEYWORDS = [
"multipleOf",
Expand Down
2 changes: 0 additions & 2 deletions lib/definition_schema.js
@@ -1,5 +1,3 @@
"use strict"

var metaSchema = require("./refs/json-schema-draft-07.json")

module.exports = {
Expand Down
12 changes: 0 additions & 12 deletions lib/dot/_limitItems.jst

This file was deleted.

12 changes: 0 additions & 12 deletions lib/dot/_limitLength.jst

This file was deleted.

12 changes: 0 additions & 12 deletions lib/dot/_limitProperties.jst

This file was deleted.

10 changes: 0 additions & 10 deletions lib/dot/errors.def
Expand Up @@ -101,10 +101,6 @@
'enum': "'should be equal to one of the allowed values'",
format: "'should match format \"{{#def.concatSchemaEQ}}\"'",
'if': "'should match \"' + {{=$ifClause}} + '\" schema'",
_exclusiveLimit: "'should be {{=$op}} {{#def.appendSchema}}",
_limitItems: "'should NOT have {{?$keyword=='maxItems'}}more{{??}}fewer{{?}} than {{#def.concatSchema}} items'",
_limitLength: "'should NOT be {{?$keyword=='maxLength'}}longer{{??}}shorter{{?}} than {{#def.concatSchema}} characters'",
_limitProperties:"'should NOT have {{?$keyword=='maxProperties'}}more{{??}}fewer{{?}} than {{#def.concatSchema}} properties'",
multipleOf: "'should be multiple of {{#def.appendSchema}}",
not: "'should NOT be valid'",
oneOf: "'should match exactly one schema in oneOf'",
Expand Down Expand Up @@ -135,9 +131,6 @@
'enum': "validate.schema{{=$schemaPath}}",
format: "{{#def.schemaRefOrQS}}",
'if': "validate.schema{{=$schemaPath}}",
_limitItems: "{{#def.schemaRefOrVal}}",
_limitLength: "{{#def.schemaRefOrVal}}",
_limitProperties:"{{#def.schemaRefOrVal}}",
multipleOf: "{{#def.schemaRefOrVal}}",
not: "validate.schema{{=$schemaPath}}",
oneOf: "validate.schema{{=$schemaPath}}",
Expand Down Expand Up @@ -167,9 +160,6 @@
'enum': "{ allowedValues: schema{{=$lvl}} }",
format: "{ format: {{#def.schemaValueQS}} }",
'if': "{ failingKeyword: {{=$ifClause}} }",
_limitItems: "{ limit: {{=$schemaValue}} }",
_limitLength: "{ limit: {{=$schemaValue}} }",
_limitProperties:"{ limit: {{=$schemaValue}} }",
multipleOf: "{ multipleOf: {{=$schemaValue}} }",
not: "{}",
oneOf: "{ passingSchemas: {{=$passingSchemas}} }",
Expand Down
6 changes: 0 additions & 6 deletions lib/dotjs/index.js
Expand Up @@ -12,12 +12,6 @@ module.exports = {
format: require("./format"),
if: require("./if"),
items: require("./items"),
maxItems: require("./_limitItems"),
minItems: require("./_limitItems"),
maxLength: require("./_limitLength"),
minLength: require("./_limitLength"),
maxProperties: require("./_limitProperties"),
minProperties: require("./_limitProperties"),
multipleOf: require("./multipleOf"),
not: require("./not"),
oneOf: require("./oneOf"),
Expand Down
17 changes: 5 additions & 12 deletions lib/keyword.js
Expand Up @@ -3,13 +3,6 @@ var customRuleCode = require("./dotjs/custom")
var definitionSchema = require("./definition_schema")
var util = require("./compile/util")

module.exports = {
add: addKeyword,
get: getKeyword,
remove: removeKeyword,
validate: validateKeyword,
}

/**
* Define custom keyword
* @this Ajv
Expand All @@ -18,7 +11,7 @@ module.exports = {
* @param {Boolean} _skipValidation of keyword definition
* @return {Ajv} this for method chaining
*/
function addKeyword(keyword, definition, _skipValidation) {
export function addKeyword(keyword, definition, _skipValidation) {
/* jshint validthis: true */
/* eslint no-shadow: 0 */
var RULES = this.RULES
Expand Down Expand Up @@ -116,7 +109,7 @@ function ruleCode(it, keyword /*, ruleType */) {
schemaCode = schemaRefOrVal()
}
const data = "data" + (it.dataLevel || "")
const cxt = {fail, keyword, data, $data, schemaCode, schemaType}
const cxt = {fail, keyword, data, $data, schemaCode, opts: it.opts}
// TODO check that code called "fail" or another valid way to return code
code(cxt)
return out
Expand Down Expand Up @@ -173,7 +166,7 @@ function ruleCode(it, keyword /*, ruleType */) {
* @param {String} keyword pre-defined or custom keyword.
* @return {Object|Boolean} custom keyword definition, `true` if it is a predefined keyword, `false` otherwise.
*/
function getKeyword(keyword) {
export function getKeyword(keyword) {
/* jshint validthis: true */
var rule = this.RULES.custom[keyword]
return rule ? rule.definition : this.RULES.keywords[keyword] || false
Expand All @@ -185,7 +178,7 @@ function getKeyword(keyword) {
* @param {String} keyword pre-defined or custom keyword.
* @return {Ajv} this for method chaining
*/
function removeKeyword(keyword) {
export function removeKeyword(keyword) {
/* jshint validthis: true */
var RULES = this.RULES
delete RULES.keywords[keyword]
Expand All @@ -210,7 +203,7 @@ function removeKeyword(keyword) {
* @param {Boolean} throwError true to throw exception if definition is invalid
* @return {boolean} validation result
*/
function validateKeyword(definition, throwError) {
export function validateKeyword(definition, throwError) {
validateKeyword.errors = null
var v = (this._validateKeyword =
this._validateKeyword || this.compile(definitionSchema, true))
Expand Down
2 changes: 1 addition & 1 deletion lib/keywords/const.js
Expand Up @@ -4,6 +4,6 @@ module.exports = {
code: ({fail, data, schemaCode}) => fail(`!equal(${data}, ${schemaCode})`),
error: {
message: () => '"should be equal to constant"',
params: ({schemaCode}) => `{ allowedValue: ${schemaCode} }`,
params: ({schemaCode}) => `{allowedValue: ${schemaCode}}`,
},
}
6 changes: 6 additions & 0 deletions lib/keywords/index.js
Expand Up @@ -4,4 +4,10 @@ module.exports = {
exclusiveMinimum: require("./limit"),
maximum: require("./limit"),
minimum: require("./limit"),
maxItems: require("./limitItems"),
minItems: require("./limitItems"),
maxProperties: require("./limitProperties"),
minProperties: require("./limitProperties"),
maxLength: require("./limitLength"),
minLength: require("./limitLength"),
}
10 changes: 6 additions & 4 deletions lib/keywords/limit.js
Expand Up @@ -7,19 +7,21 @@ const OPS = {
exclusiveMinimum: {fail: "<=", ok: ">"},
}

const SCHEMA_TYPE = "number"

module.exports = {
keywords: ["maximum", "minimum", "exclusiveMaximum", "exclusiveMinimum"],
type: "number",
schemaType: "number",
schemaType: SCHEMA_TYPE,
$data: true,
code: ({keyword, fail, data, $data, schemaCode, schemaType}) => {
const dnt = dataNotType($data, schemaCode, schemaType)
code({fail, keyword, data, $data, schemaCode}) {
const dnt = dataNotType($data, schemaCode, SCHEMA_TYPE)
fail(dnt + data + OPS[keyword].fail + schemaCode + ` || ${data}!==${data}`)
},
error: {
message: ({keyword, $data, schemaCode}) =>
`"should be ${OPS[keyword].ok} ${appendSchema($data, schemaCode)}`,
params: ({keyword, schemaCode}) =>
`{ comparison: "${OPS[keyword].ok}", limit: ${schemaCode} }`,
`{comparison: "${OPS[keyword].ok}", limit: ${schemaCode}}`,
},
}
23 changes: 23 additions & 0 deletions lib/keywords/limitItems.js
@@ -0,0 +1,23 @@
const {concatSchema, dataNotType} = require("../compile/util")

const SCHEMA_TYPE = "number"

module.exports = {
keywords: ["maxItems", "minItems"],
type: "array",
schemaType: SCHEMA_TYPE,
$data: true,
code({fail, keyword, data, $data, schemaCode}) {
const op = keyword == "maxItems" ? ">" : "<"
const dnt = dataNotType($data, schemaCode, SCHEMA_TYPE)
fail(dnt + `${data}.length` + op + schemaCode)
},
error: {
message({keyword, $data, schemaCode}) {
const comp = keyword == "maxItems" ? "more" : "fewer"
const sch = concatSchema($data, schemaCode)
return `"should NOT have ${comp} than ${sch} items"`
},
params: ({schemaCode}) => `{limit: ${schemaCode}}`,
},
}
25 changes: 25 additions & 0 deletions lib/keywords/limitLength.js
@@ -0,0 +1,25 @@
const {concatSchema, dataNotType} = require("../compile/util")

const SCHEMA_TYPE = "number"

module.exports = {
keywords: ["maxLength", "minLength"],
type: "string",
schemaType: SCHEMA_TYPE,
$data: true,
code({fail, keyword, data, $data, schemaCode, opts}) {
const op = keyword == "maxLength" ? ">" : "<"
const dnt = dataNotType($data, schemaCode, SCHEMA_TYPE)
const len =
opts.unicode === false ? `${data}.length` : `ucs2length(${data})`
fail(dnt + len + op + schemaCode)
},
error: {
message({keyword, $data, schemaCode}) {
const comp = keyword == "maxLength" ? "more" : "fewer"
const sch = concatSchema($data, schemaCode)
return `"should NOT have ${comp} than ${sch} items"`
},
params: ({schemaCode}) => `{limit: ${schemaCode}}`,
},
}
23 changes: 23 additions & 0 deletions lib/keywords/limitProperties.js
@@ -0,0 +1,23 @@
const {concatSchema, dataNotType} = require("../compile/util")

const SCHEMA_TYPE = "number"

module.exports = {
keywords: ["maxProperties", "minProperties"],
type: "object",
schemaType: SCHEMA_TYPE,
$data: true,
code({fail, keyword, data, $data, schemaCode}) {
const op = keyword == "maxProperties" ? ">" : "<"
const dnt = dataNotType($data, schemaCode, SCHEMA_TYPE)
fail(dnt + `Object.keys(${data}).length` + op + schemaCode)
},
error: {
message({keyword, $data, schemaCode}) {
const comp = keyword == "maxProperties" ? "more" : "fewer"
const sch = concatSchema($data, schemaCode)
return `"should NOT have ${comp} than ${sch} items"`
},
params: ({schemaCode}) => `{limit: ${schemaCode}}`,
},
}

0 comments on commit 43d80f6

Please sign in to comment.