Skip to content

Commit

Permalink
Merge 8d77a1d into 6cede7b
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 10, 2020
2 parents 6cede7b + 8d77a1d commit af03b96
Show file tree
Hide file tree
Showing 51 changed files with 1,189 additions and 728 deletions.
90 changes: 66 additions & 24 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,72 @@
extends: eslint:recommended
parserOptions:
ecmaVersion: 6
env:
es6: true
node: true
browser: true
extends:
- "eslint:recommended"
- prettier
parserOptions:
ecmaVersion: 2018
sourceType: module
overrides:
- files: ["*.ts"]
extends:
- "eslint:recommended"
- "plugin:@typescript-eslint/recommended"
- "plugin:@typescript-eslint/recommended-requiring-type-checking"
- "prettier/@typescript-eslint"
parser: "@typescript-eslint/parser"
parserOptions:
project: ["./tsconfig.json"]
plugins: ["@typescript-eslint"]
rules:
no-var: 0
"@typescript-eslint/restrict-template-expressions":
[error, allowBoolean: true]
"@typescript-eslint/ban-types": off
"@typescript-eslint/no-empty-interface": off
"@typescript-eslint/no-explicit-any": off
"@typescript-eslint/no-unsafe-call": off
"@typescript-eslint/no-unsafe-member-access": off
"@typescript-eslint/no-unsafe-assignment": off
"@typescript-eslint/restrict-plus-operands": off
"@typescript-eslint/no-unsafe-return": off
"@typescript-eslint/no-var-requires": off
rules:
block-scoped-var: 2
callback-return: 2
complexity: [2, 16]
curly: [2, multi-or-nest, consistent]
dot-location: [2, property]
dot-notation: 2
linebreak-style: [2, unix]
new-cap: 2
no-console: [2, allow: [warn, error]]
no-else-return: 2
no-eq-null: 2
no-extra-semi: 0
no-fallthrough: 2
no-invalid-this: 2
no-return-assign: 2
no-shadow: 1
no-trailing-spaces: 2
no-use-before-define: [2, nofunc]
block-scoped-var: error
callback-return: error
complexity: [error, 16]
curly: [error, multi-or-nest, consistent]
dot-location: [error, property]
dot-notation: error
id-match: error
linebreak-style: [error, unix]
new-cap: error
no-console: [error, allow: [warn, error]]
no-debugger: error
no-duplicate-imports: error
no-else-return: error
no-eq-null: error
no-eval: error
no-fallthrough: error
no-invalid-this: error
no-new-wrappers: error
no-path-concat: error
no-redeclare: error
no-return-assign: error
no-sequences: error
no-shadow: warn
no-template-curly-in-string: error
no-trailing-spaces: error
no-undef-init: error
no-use-before-define: [error, nofunc]
prefer-const: error
radix: error
semi: 0
strict: [2, global]
valid-jsdoc: [2, requireReturn: false]
valid-jsdoc: [error, requireReturn: false]
no-control-regex: 0
no-useless-escape: 2
no-useless-escape: error
no-void: error
# eqeqeq: [error, smart]
# no-var: error
# prefer-arrow-callback: error
16 changes: 8 additions & 8 deletions lib/ajv.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
declare var ajv: {
(options?: ajv.Options): ajv.Ajv
new (options?: ajv.Options): ajv.Ajv
ValidationError: typeof AjvErrors.ValidationError
MissingRefError: typeof AjvErrors.MissingRefError
$dataMetaSchema: object
}

declare namespace AjvErrors {
class ValidationError extends Error {
constructor(errors: Array<ajv.ErrorObject>)
Expand All @@ -26,6 +18,14 @@ declare namespace AjvErrors {
}
}

declare const ajv: {
(options?: ajv.Options): ajv.Ajv
new (options?: ajv.Options): ajv.Ajv
ValidationError: typeof AjvErrors.ValidationError
MissingRefError: typeof AjvErrors.MissingRefError
$dataMetaSchema: object
}

declare namespace ajv {
type ValidationError = AjvErrors.ValidationError

Expand Down
31 changes: 16 additions & 15 deletions lib/ajv.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"use strict"
import SchemaObject from "./compile/schema_obj"
import Cache from "./cache"

var compileSchema = require("./compile"),
resolve = require("./compile/resolve"),
Cache = require("./cache"),
SchemaObject = require("./compile/schema_obj"),
stableStringify = require("fast-json-stable-stringify"),
rules = require("./compile/rules"),
$dataMetaSchema = require("./data"),
util = require("./compile/util")
validationVocabulary = require("./vocabularies/validation")

module.exports = Ajv

Expand All @@ -26,10 +25,11 @@ 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.addVocabulary = customKeyword.addVocabulary
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 All @@ -54,7 +54,7 @@ var META_SUPPORT_DATA = ["/properties"]
*/
function Ajv(opts) {
if (!(this instanceof Ajv)) return new Ajv(opts)
opts = this._opts = util.copy(opts) || {}
opts = this._opts = {...(opts || {})}
setLogger(this)
this._schemas = {}
this._refs = {}
Expand All @@ -76,7 +76,8 @@ function Ajv(opts) {
this._metaOpts = getMetaSchemaOptions(this)

if (opts.formats) addInitialFormats(this)
if (opts.keywords) addInitialKeywords(this)
this.addVocabulary(validationVocabulary, true)
if (opts.keywords) addInitialKeywords(this, opts.keywords)
addDefaultMetaSchema(this)
if (typeof opts.meta == "object") this.addMetaSchema(opts.meta)
if (opts.nullable)
Expand Down Expand Up @@ -436,10 +437,10 @@ function addInitialFormats(self) {
}
}

function addInitialKeywords(self) {
for (var name in self._opts.keywords) {
var keyword = self._opts.keywords[name]
self.addKeyword(name, keyword)
function addInitialKeywords(self, keywords, skipValidation) {
for (var name in keywords) {
var keyword = keywords[name]
self.addKeyword(name, keyword, skipValidation)
}
}

Expand All @@ -449,7 +450,7 @@ function checkUnique(self, id) {
}

function getMetaSchemaOptions(self) {
var metaOpts = util.copy(self._opts)
var metaOpts = {...self._opts}
for (var i = 0; i < META_IGNORE_OPTIONS.length; i++)
delete metaOpts[META_IGNORE_OPTIONS[i]]
return metaOpts
Expand Down
21 changes: 0 additions & 21 deletions lib/cache.js

This file was deleted.

25 changes: 25 additions & 0 deletions lib/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import SchemaObject from "./compile/schema_obj"

export default class Cache {
_cache: {[key: string]: SchemaObject}

constructor() {
this._cache = {}
}

put(key: string, value: SchemaObject): void {
this._cache[key] = value
}

get(key: string): SchemaObject {
return this._cache[key]
}

del(key: string): void {
delete this._cache[key]
}

clear(): void {
this._cache = {}
}
}
3 changes: 0 additions & 3 deletions lib/compile/async.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict"

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

module.exports = compileAsync
Expand All @@ -15,7 +13,6 @@ module.exports = compileAsync
*/
function compileAsync(schema, meta, callback) {
/* eslint no-shadow: 0 */
/* global Promise */
/* jshint validthis: true */
var self = this
if (typeof this._opts.loadSchema != "function")
Expand Down
2 changes: 0 additions & 2 deletions lib/compile/equal.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict"

var resolve = require("./resolve")

module.exports = {
Expand Down
17 changes: 7 additions & 10 deletions lib/compile/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"use strict"
import Scope from "./scope"
import ucs2length from "./ucs2length"
import {toQuotedString} from "./util"
const equal = require("fast-deep-equal")

var resolve = require("./resolve"),
util = require("./util"),
Expand All @@ -11,9 +14,6 @@ var validateGenerator = require("../dotjs/validate")
* Functions below are used inside compiled validations function
*/

var ucs2length = util.ucs2length
var equal = require("fast-deep-equal")

// this error is thrown by async schemas to return validation errors via exception
var ValidationError = errorClasses.Validation

Expand Down Expand Up @@ -93,6 +93,7 @@ function compile(schema, root, localRefs, baseId) {
schemaPath: "",
errSchemaPath: "#",
errorPath: '""',
scope: new Scope(),
MissingRefError: errorClasses.MissingRef,
RULES: RULES,
validate: validateGenerator,
Expand Down Expand Up @@ -243,7 +244,7 @@ function compile(schema, root, localRefs, baseId) {
case "number":
return "" + value
case "string":
return util.toQuotedString(value)
return toQuotedString(value)
case "object":
if (value === null) return "null"
var valueStr = stableStringify(value)
Expand Down Expand Up @@ -366,11 +367,7 @@ function compIndex(schema, root, baseId) {

function patternCode(i, patterns) {
return (
"var pattern" +
i +
" = new RegExp(" +
util.toQuotedString(patterns[i]) +
");"
"var pattern" + i + " = new RegExp(" + toQuotedString(patterns[i]) + ");"
)
}

Expand Down
14 changes: 6 additions & 8 deletions lib/compile/resolve.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"use strict"

import SchemaObject from "./schema_obj"
import {toHash, escapeFragment, unescapeFragment} from "./util"
var URI = require("uri-js"),
equal = require("fast-deep-equal"),
util = require("./util"),
SchemaObject = require("./schema_obj"),
traverse = require("json-schema-traverse")

module.exports = resolve
Expand Down Expand Up @@ -110,7 +108,7 @@ function resolveRecursive(root, ref, parsedRef) {
}
}

var PREVENT_SCOPE_CHANGE = util.toHash([
var PREVENT_SCOPE_CHANGE = toHash([
"properties",
"patternProperties",
"enum",
Expand All @@ -127,7 +125,7 @@ function getJsonPointer(parsedRef, baseId, schema, root) {
for (var i = 1; i < parts.length; i++) {
var part = parts[i]
if (part) {
part = util.unescapeFragment(part)
part = unescapeFragment(part)
schema = schema[part]
if (schema === undefined) break
var id
Expand All @@ -150,7 +148,7 @@ function getJsonPointer(parsedRef, baseId, schema, root) {
return {schema: schema, root: root, baseId: baseId}
}

var SIMPLE_INLINED = util.toHash([
var SIMPLE_INLINED = toHash([
"type",
"format",
"pattern",
Expand Down Expand Up @@ -258,7 +256,7 @@ function resolveIds(schema) {
if (keyIndex !== undefined) {
fullPath +=
"/" +
(typeof keyIndex == "number" ? keyIndex : util.escapeFragment(keyIndex))
(typeof keyIndex == "number" ? keyIndex : escapeFragment(keyIndex))
}

if (typeof id == "string") {
Expand Down
Loading

0 comments on commit af03b96

Please sign in to comment.