Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Sep 12, 2021
2 parents 698f411 + a9f38cd commit 41dd4bc
Show file tree
Hide file tree
Showing 27 changed files with 210 additions and 95 deletions.
26 changes: 26 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
ignore:
- dependency-name: "@types/node"
versions:
- 15.0.0
- dependency-name: eslint-config-prettier
versions:
- 8.0.0
- 8.1.0
- 8.2.0
- dependency-name: karma
versions:
- 6.0.3
- 6.0.4
- 6.1.0
- 6.1.1
- 6.1.2
- 6.2.0
- 6.3.0
- 6.3.1
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: git submodule update --init
- name: update website
if: ${{ github.event_name == 'push' && matrix.node-version == '14.x' }}
run: ./scripts/publish-site
env:
GH_TOKEN_PUBLIC: ${{ secrets.GH_TOKEN_PUBLIC }}
GIT_USER_EMAIL: ${{ secrets.GIT_USER_EMAIL }}
GIT_USER_NAME: ${{ secrets.GIT_USER_NAME }}
# - name: update website
# if: ${{ github.event_name == 'push' && matrix.node-version == '14.x' }}
# run: ./scripts/publish-site
# env:
# GH_TOKEN_PUBLIC: ${{ secrets.GH_TOKEN_PUBLIC }}
# GIT_USER_EMAIL: ${{ secrets.GIT_USER_EMAIL }}
# GIT_USER_NAME: ${{ secrets.GIT_USER_NAME }}
- run: npm run build
- run: npm run test-ci
- name: coveralls
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Ajv stands out as the implementation of choice - it provides a rich API which ma
[Vega-Lite](https://vega.github.io/vega-lite/)
[![middy](./projects/middy.png)](https://middy.js.org)
[JSDoc](https://github.com/jsdoc/jsdoc)
[![Ts.ED](./projects/tsed.png)](https://tsed.io)

</Projects>
</HomeSection>
Expand Down
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ if (validate(data)) {
}
```

See more advanced example in [the test](../spec/types/json-schema.spec.ts).
See more advanced example in [the test](https://github.com/ajv-validator/ajv/blob/master/spec/types/json-schema.spec.ts).

<a name="jtd-serialize"></a>

Expand Down Expand Up @@ -385,7 +385,7 @@ if (validate(data)) {
}
```

Also see an example in [this test](../spec/types/error-parameters.spec.ts)
Also see an example in [this test](https://github.com/ajv-validator/ajv/blob/master/spec/types/error-parameters.spec.ts)

- `maxItems`, `minItems`, `maxLength`, `minLength`, `maxProperties`, `minProperties`:

Expand Down
4 changes: 2 additions & 2 deletions docs/codegen.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if (num0 > 0) {
}
```

`.const`, `.if` and `.code` above are methods of CodeGen class that generate code inside class instance `gen` - see [source code](https://github.com/ajv-validator/ajv/blob/master/lib/compile/codegen/index.ts) for all available methods and [tests](../spec/codegen.spec.ts) for other code generation examples.
`.const`, `.if` and `.code` above are methods of CodeGen class that generate code inside class instance `gen` - see [source code](https://github.com/ajv-validator/ajv/blob/master/lib/compile/codegen/index.ts) for all available methods and [tests](https://github.com/ajv-validator/ajv/blob/master/spec/codegen.spec.ts) for other code generation examples.

These methods only accept instances of private class `_Code`, other values will be rejected by Typescript compiler - the risk to pass unsafe string is mitigated on type level.

Expand All @@ -72,7 +72,7 @@ CodeGen class generates code trees and performs several optimizations before the
These optimizations assume that the expressions in `if` conditions, `for` statement headers and assigned expressions are free of any side effects - this is the case for all pre-defined validation keywords.
:::

See [these tests](../spec/codegen.spec.ts) for examples.
See [these tests](https://github.com/ajv-validator/ajv/blob/master/spec/codegen.spec.ts) for examples.

By default Ajv does 1-pass optimization - based on the test suite it reduces the code size by 10.5% and the number of tree nodes by 16.7% (TODO benchmark the validation time). The second optimization pass changes it by less than 0.1%, so you won't need it unless you have really complex schemas or if you generate standalone code and want it to pass relevant eslint rules.

Expand Down
2 changes: 1 addition & 1 deletion docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ There are several ways to implement the described logic that would allow two pro

This problem is related to the problem explained above - properties treated as additional in the sense of `additionalProperties` keyword, based on `properties`/`patternProperties` keyword in the same schema object.

See the example in [Filtering Data](https://github.com/ajv-validator/ajv#filtering-data) section of readme.
See the example in the [Removing Additional Data](https://ajv.js.org/guide/modifying-data.html#removing-additional-properties) section of the docs.

## Generating schemas with resolved references ($ref)

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/async-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Example:
const ajv = new Ajv()

ajv.addKeyword({
keyword: "idExists"
keyword: "idExists",
async: true,
type: "number",
validate: checkIdExists,
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/combining-schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const ajv = new Ajv2019({
const validate = ajv.getSchema("https://example.com/strict-tree")
```

See [dynamic-refs](../spec/dynamic-ref.spec.ts) test for the example using `$dynamicAnchor`/`$dynamicRef`.
See [dynamic-refs](https://github.com/ajv-validator/ajv/blob/master/spec/dynamic-ref.spec.ts) test for the example using `$dynamicAnchor`/`$dynamicRef`.

At the moment Ajv implements the spec for dynamic recursive references with these limitations:

Expand Down
7 changes: 4 additions & 3 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,16 @@ const schema = {
}

const serialize = ajv.compileSerializer(schema)
console.log(serialize(data))

const parse = ajv.compileParser(schema)

const data = {
foo: 1,
bar: "abc"
}

console.log(serialize(data))

const parse = ajv.compileParser(schema)

const json = '{"foo": 1, "bar": "abc"}'
const invalidJson = '{"unknown": "abc"}'

Expand Down
18 changes: 9 additions & 9 deletions docs/json-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ The value of the keywords should be a number. The data to be valid should have l

_invalid_: `"abcdef"`

2) _schema_: `{type: "string", minLength": 2}`
2) _schema_: `{type: "string", minLength: 2}`

_valid_: `"ab"`, `"😀😀"`

Expand Down Expand Up @@ -453,16 +453,16 @@ For the data array to be valid, the items with indices less than the number of s
_schema_:
```javascript
{
type: "array",
prefixItems: [{type: "integer"}, {type: "string"}]
}
```
```javascript
{
type: "array",
prefixItems: [{type: "integer"}, {type: "string"}]
}
```

_valid_: `[1]`, `[1, "abc"]`, `[1, "abc", 2]`, `[]`
_valid_: `[1]`, `[1, "abc"]`, `[1, "abc", 2]`, `[]`

_invalid_: `["abc", 1]`, `["abc"]`
_invalid_: `["abc", 1]`, `["abc"]`

The schema in example will log warning by default (see `strictTuples` option), because it defines unconstrained tuple. To define a tuple with exactly 2 elements use [minItems](#minitems) and [items](#items-in-draft-2020-12) keywords (see example 2 in [items](#items-in-draft-2020-12)).

Expand Down
2 changes: 1 addition & 1 deletion docs/json-type-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ const schema: JTDSchemaType<LinkedList, {node: LinkedList}> = {

### Notable Omissions

`JTDSchemaType` currently validats that if the schema compiles it will verify an accurate type, but there are a few places with potentially unexpected behavior.
`JTDSchemaType` currently validates that if the schema compiles it will verify an accurate type, but there are a few places with potentially unexpected behavior.
`JTDSchemaType` doesn't verify the schema is correct. It won't reject schemas that definitions anywhere by the root, and it won't reject discriminator schemas that still define the descriminator in mapping properties. It also won't verify that enum schemas have every enum member as this isn't generally feasible in typescript yet.

## Extending JTD
Expand Down
2 changes: 1 addition & 1 deletion docs/keywords.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ ajv.addKeyword({
})
```

Macro keywords an be recursive - i.e. return schemas containing the same keyword. See the example of defining a recursive macro keyword `deepProperties` in the [test](../spec/keyword.spec.ts#L316).
Macro keywords an be recursive - i.e. return schemas containing the same keyword. See the example of defining a recursive macro keyword `deepProperties` in the [test](https://github.com/ajv-validator/ajv/blob/master/spec/keyword.spec.ts#L316).

## Schema compilation context

Expand Down
10 changes: 10 additions & 0 deletions docs/news/2021-05-24-ajv-online-event-video.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
news: true
title: Ajv online event video uploaded
date: 2021-05-24
more: false
---

Huge thanks to everybody who joined, and to the speakers! The video of the event is [available on YouTube](https://www.youtube.com/watch?v=KxSKqXEBB7A).

<!-- more -->
12 changes: 12 additions & 0 deletions docs/news/2021-07-22-ajv-microsoft-foss-fund-award.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
news: true
title: Microsoft FOSS award
date: 2021-07-22
more: false
---

Ajv was awarded a sponsorship from [Microsoft FOSS fund](https://github.com/microsoft/foss-fund/blob/main/README.md#2021) - huge thanks to Microsoft and the engineers who voted to support Ajv development.

This award will contribute to a long term maintenance of Ajv.

<!-- more -->
Binary file added docs/projects/tsed.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/standalone.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ npm install ajv
```javascript
const Ajv = require("ajv") // version >= v7.0.0
const ajv = new Ajv({code: {source: true}}) // this option is required to generate standalone code
const standaloneCode = require("ajv/dist/standalone")
const standaloneCode = require("ajv/dist/standalone").default

const schema = {
$id: "https://example.com/object.json",
Expand Down
2 changes: 1 addition & 1 deletion lib/compile/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {getErrorPath, Type} from "./util"
import N from "./names"

export const keywordError: KeywordErrorDefinition = {
message: ({keyword}) => str`should pass "${keyword}" keyword validation`,
message: ({keyword}) => str`must pass "${keyword}" keyword validation`,
}

export const keyword$DataError: KeywordErrorDefinition = {
Expand Down
9 changes: 5 additions & 4 deletions lib/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,12 @@ function getJsonPointer(
): SchemaEnv | undefined {
if (parsedRef.fragment?.[0] !== "/") return
for (const part of parsedRef.fragment.slice(1).split("/")) {
if (typeof schema == "boolean") return
schema = schema[unescapeFragment(part)]
if (schema === undefined) return
if (typeof schema === "boolean") return
const partSchema = schema[unescapeFragment(part)]
if (partSchema === undefined) return
schema = partSchema
// TODO PREVENT_SCOPE_CHANGE could be defined in keyword def?
const schId = typeof schema == "object" && schema[this.opts.schemaId]
const schId = typeof schema === "object" && schema[this.opts.schemaId]
if (!PREVENT_SCOPE_CHANGE.has(part) && schId) {
baseId = resolveUrl(baseId, schId)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compile/jtd/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function serializeType(cxt: SerializeCxt): void {
case "timestamp":
gen.if(
_`${data} instanceof Date`,
() => gen.add(N.json, _`${data}.toISOString()`),
() => gen.add(N.json, _`'"' + ${data}.toISOString() + '"'`),
() => serializeString(cxt)
)
break
Expand Down
13 changes: 8 additions & 5 deletions lib/runtime/parseJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function parseJson(s: string, pos: number): unknown {
parseJson.position = pos + s.length
return JSON.parse(s)
} catch (e) {
matches = rxParseJson.exec(e.message)
matches = rxParseJson.exec((e as Error).message)
if (!matches) {
parseJson.message = "unexpected end"
return undefined
Expand Down Expand Up @@ -128,14 +128,16 @@ export function parseJsonString(s: string, pos: number): string | undefined {
let code = 0
while (count--) {
code <<= 4
c = s[pos].toLowerCase()
c = s[pos]
if (c === undefined) {
errorMessage("unexpected end")
return undefined
}
c = c.toLowerCase()
if (c >= "a" && c <= "f") {
code += c.charCodeAt(0) - CODE_A + 10
} else if (c >= "0" && c <= "9") {
code += c.charCodeAt(0) - CODE_0
} else if (c === undefined) {
errorMessage("unexpected end")
return undefined
} else {
errorMessage(`unexpected token ${c}`)
return undefined
Expand All @@ -147,6 +149,7 @@ export function parseJsonString(s: string, pos: number): string | undefined {
errorMessage(`unexpected token ${c}`)
return undefined
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
} else if (c === undefined) {
errorMessage("unexpected end")
return undefined
Expand Down

0 comments on commit 41dd4bc

Please sign in to comment.