Skip to content

Commit

Permalink
Merge 0aad959 into f098b51
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 25, 2020
2 parents f098b51 + 0aad959 commit ec0e2b1
Show file tree
Hide file tree
Showing 164 changed files with 5,021 additions and 4,783 deletions.
2 changes: 0 additions & 2 deletions .codeclimate.yml

This file was deleted.

92 changes: 68 additions & 24 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,74 @@
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
"@typescript-eslint/no-empty-function": off
"@typescript-eslint/no-this-alias": off
"@typescript-eslint/no-implied-eval": 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, 17]
curly: [error, multi-line, consistent]
dot-location: [error, property]
dot-notation: error
eqeqeq: [error, smart]
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-arrow-callback: error
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
# no-var: error
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ node_modules

.DS_Store

# Compiled templates
lib/dotjs/*.js

# Browserified tests
.browser

Expand Down
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ spec/JSON-Schema-Test-Suite
coverage
dist
.nyc_output
lib/dotjs
6 changes: 2 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,9 @@ npm run test-fast
git commit -nm 'type: message'
```

All validation functions are generated using doT templates in [dot](https://github.com/ajv-validator/ajv/tree/master/lib/dot) folder. Templates are precompiled so doT is not a run-time dependency.
`npm run build` - compiles typescript to dist folder.

`npm run build` - compiles templates to [dotjs](https://github.com/ajv-validator/ajv/tree/master/lib/dotjs) folder.

`npm run watch` - automatically compiles templates when files in dot folder change
`npm run watch` - automatically compiles typescript when files in lib folder change

#### Pull requests

Expand Down
28 changes: 12 additions & 16 deletions CUSTOM.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ Example. `constant` keyword (a synonym for draft-06 keyword `const`, it is equiv
```javascript
ajv.addKeyword("constant", {
validate: function (schema, data) {
return typeof schema == "object" && schema !== null
? deepEqual(schema, data)
: schema === data
return typeof schema == "object" && schema !== null ? deepEqual(schema, data) : schema === data
},
errors: false,
})
Expand Down Expand Up @@ -309,16 +307,12 @@ There is a number of variables and expressions you can use in the generated (val

There are sevral useful functions you can use in your inline keywords. These functions are available as properties of `it.util` object:

##### .copy(Object obj[, Object target]) -> Object

Clone or extend the object. If one object is passed, it is cloned. If two objects are passed, the second object is extended with the properties of the first.

##### .toHash(Array arr) -> Object

Converts the array of strings to the object where each string becomes the key with the value of `true`.

```javascript
it.util.toHash(["a", "b", "c"]) // { a: true, b: true, c: true }
toHash(["a", "b", "c"]) // { a: true, b: true, c: true }
```

##### .equal(value1, value2) -> Boolean
Expand All @@ -330,18 +324,18 @@ Performs deep equality comparison. This function is used in keywords `enum`, `co
Converts the string that is the key/index to access the property/item to the JavaScript syntax to access the property (either "." notation or "[...]" notation).

```javascript
it.util.getProperty("a") // ".a"
it.util.getProperty("1") // "['1']"
it.util.getProperty("a'b") // "['a\\'b']"
it.util.getProperty(1) // "[1]"
getProperty("a") // ".a"
getProperty("1") // "['1']"
getProperty("a'b") // "['a\\'b']"
getProperty(1) // "[1]"
```

##### .schemaHasRules(Object schema, Object rules) -> String

Determines whether the passed schema has rules that should be validated. This function should be used before calling `it.validate` to compile subschemas.

```javascript
it.util.schemaHasRules(schema, it.RULES.all) // true or false
schemaHasRules(schema, it.RULES.all) // true or false
```

##### .escapeQuotes(String str) -> String
Expand All @@ -353,15 +347,15 @@ Escapes single quotes in the string, so it can be inserted in the generated code
Converts the string to the JavaScript string constant in single quotes (using the escaped string).

```javascript
it.util.toQuotedString("a'b") // "'a\\'b'"
toQuotedString("a'b") // "'a\\'b'"
```

##### .getData(String jsonPointer, Number dataLevel, Array paths) -> String

Returns the validation-time expression to safely access data based on the passed [relative json pointer](https://tools.ietf.org/html/draft-luff-relative-json-pointer-00) (See [examples](https://gist.github.com/geraintluff/5911303)).

```javascript
it.util.getData("2/test/1", it.dataLevel, it.dataPathArr)
getData("2/test/1", it.dataLevel, it.dataPathArr)
// The result depends on the current level
// if it.dataLevel is 3 the result is "data1 && data1.test && data1.test[1]"
```
Expand All @@ -388,7 +382,9 @@ All custom keywords but macro keywords can optionally create custom error messag

Synchronous validating and compiled keywords should define errors by assigning them to `.errors` property of the validation function. Asynchronous keywords can return promise that rejects with `new Ajv.ValidationError(errors)`, where `errors` is an array of custom validation errors (if you don't want to define custom errors in asynchronous keyword, its validation function can return the promise that resolves with `false`).

Inline custom keyword should increase error counter `errors` and add error to `vErrors` array (it can be null). This can be done for both synchronous and asynchronous keywords. See [example range keyword](https://github.com/ajv-validator/ajv/blob/master/spec/custom_rules/range_with_errors.jst).
TODO replace "inline" keywords with "code" keywords

Inline custom keyword should increase error counter `errors` and add error to `vErrors` array (it can be null). This can be done for both synchronous and asynchronous keywords.

When inline keyword performs validation Ajv checks whether it created errors by comparing errors count before and after validation. To skip this check add option `errors` (can be `"full"`, `true` or `false`) to keyword definition:

Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ ajv.addMetaSchema(require("ajv/lib/refs/json-schema-draft-06.json"))

## Performance

Ajv generates code using [doT templates](https://github.com/olado/doT) to turn JSON Schemas into super-fast validation functions that are efficient for v8 optimization.
Ajv generates code to turn JSON Schemas into super-fast validation functions that are efficient for v8 optimization.

Currently Ajv is the fastest and the most standard compliant validator according to these benchmarks:

Expand Down Expand Up @@ -1128,9 +1128,9 @@ Defaults:
inlineRefs: true,
passContext: false,
loopRequired: Infinity,
loopEnum: Infinity,
ownProperties: false,
multipleOfPrecision: false,
errorDataPath: 'object', // deprecated
messages: true,
sourceCode: false,
processCode: undefined, // function (str: string, schema: object): string {}
Expand Down Expand Up @@ -1230,9 +1230,9 @@ Defaults:
- integer number - to limit the maximum number of keywords of the schema that will be inlined.
- _passContext_: pass validation context to custom keyword functions. If this option is `true` and you pass some context to the compiled validation function with `validate.call(context, data)`, the `context` will be available as `this` in your custom keywords. By default `this` is Ajv instance.
- _loopRequired_: by default `required` keyword is compiled into a single expression (or a sequence of statements in `allErrors` mode). In case of a very large number of properties in this keyword it may result in a very big validation function. Pass integer to set the number of properties above which `required` keyword will be validated in a loop - smaller validation function size but also worse performance.
- _loopEnum_: by default `enum` keyword is compiled into a single expression. In case of a very large number of allowed values it may result in a large validation function. Pass integer to set the number of values above which `enum` keyword will be validated in a loop.
- _ownProperties_: by default Ajv iterates over all enumerable object properties; when this option is `true` only own enumerable object properties (i.e. found directly on the object rather than on its prototype) are iterated. Contributed by @mbroadst.
- _multipleOfPrecision_: by default `multipleOf` keyword is validated by comparing the result of division with parseInt() of that result. It works for dividers that are bigger than 1. For small dividers such as 0.01 the result of the division is usually not integer (even when it should be integer, see issue [#84](https://github.com/ajv-validator/ajv/issues/84)). If you need to use fractional dividers set this option to some positive integer N to have `multipleOf` validated using this formula: `Math.abs(Math.round(division) - division) < 1e-N` (it is slower but allows for float arithmetics deviations).
- _errorDataPath_ (deprecated): set `dataPath` to point to 'object' (default) or to 'property' when validating keywords `required`, `additionalProperties` and `dependencies`.
- _messages_: Include human-readable messages in errors. `true` by default. `false` can be passed when custom messages are used (e.g. with [ajv-i18n](https://github.com/ajv-validator/ajv-i18n)).
- _sourceCode_: add `sourceCode` property to validating function (for debugging; this code can be different from the result of toString call).
- _processCode_: an optional function to process generated code before it is passed to Function constructor. It can be used to either beautify (the validating function is generated without line-breaks) or to transpile code. Starting from version 5.0.0 this option replaced options:
Expand Down Expand Up @@ -1370,11 +1370,9 @@ npm test

## Contributing

All validation functions are generated using doT templates in [dot](https://github.com/ajv-validator/ajv/tree/master/lib/dot) folder. Templates are precompiled so doT is not a run-time dependency.
`npm run build` - compiles typescript to dist folder.

`npm run build` - compiles templates to [dotjs](https://github.com/ajv-validator/ajv/tree/master/lib/dotjs) folder.

`npm run watch` - automatically compiles templates when files in dot folder change
`npm run watch` - automatically compiles typescript when files in lib folder change

Please see [Contributing guidelines](https://github.com/ajv-validator/ajv/blob/master/CONTRIBUTING.md)

Expand Down
16 changes: 8 additions & 8 deletions lib/ajv.d.ts → 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
Loading

0 comments on commit ec0e2b1

Please sign in to comment.