Skip to content

Commit

Permalink
Merge de3d047 into 6cede7b
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 10, 2020
2 parents 6cede7b + de3d047 commit 8aa3489
Show file tree
Hide file tree
Showing 55 changed files with 1,245 additions and 776 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
39 changes: 20 additions & 19 deletions lib/ajv.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict"
import SchemaObject from "./compile/schema_obj"
import Cache from "./cache"
import {ValidationError, MissingRefError} from "./compile/error_classes"

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,14 +26,14 @@ 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

var errorClasses = require("./compile/error_classes")
Ajv.ValidationError = errorClasses.Validation
Ajv.MissingRefError = errorClasses.MissingRef
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

Ajv.ValidationError = ValidationError
Ajv.MissingRefError = MissingRefError
Ajv.$dataMetaSchema = $dataMetaSchema

var META_SCHEMA_ID = "http://json-schema.org/draft-07/schema"
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 = {}
}
}
5 changes: 1 addition & 4 deletions lib/compile/async.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict"

var MissingRefError = require("./error_classes").MissingRef
import {MissingRefError} from "./error_classes"

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")
30 changes: 0 additions & 30 deletions lib/compile/error_classes.js

This file was deleted.

35 changes: 35 additions & 0 deletions lib/compile/error_classes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {ErrorObject} from "../types"

const resolve = require("./resolve")

export class ValidationError extends Error {
errors: ErrorObject[]
ajv: true
validation: true

constructor(errors: ErrorObject[]) {
super("validation failed")
this.errors = errors
this.ajv = this.validation = true
}
}

export class MissingRefError extends Error {
missingRef: string
missingSchema: string

static message(baseId: string, ref: string): string {
return `can't resolve reference ${ref} from id ${baseId}`
}

constructor(baseId: string, ref: string, message?: string) {
super(message || MissingRefError.message(baseId, ref))
this.missingRef = resolve.url(baseId, ref)
this.missingSchema = resolve.normalizeId(resolve.fullPath(this.missingRef))
}
}

module.exports = {
ValidationError,
MissingRefError,
}
Loading

0 comments on commit 8aa3489

Please sign in to comment.