Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: v8.12.0 #23

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1657fe4
Update README.md
epoberezkin Sep 27, 2022
13caf47
Update README.md
epoberezkin Sep 27, 2022
948ffbf
Update README.md
epoberezkin Sep 27, 2022
572c843
update typescript, fix broken annotation (#2157)
epoberezkin Nov 13, 2022
58f9e7f
Remove unnecessary console.log (#2138)
vargaeric Nov 13, 2022
d6d8d5f
npm installs version 8 by default (#2088)
VIEWVIEWVIEW Nov 13, 2022
7516a09
Doc: update the documentation of addKeyword args (#2090)
TommyDew42 Nov 13, 2022
b3cbbc1
docs: parsing does not support non-standard JTD keywords (#2072)
avanelli Nov 13, 2022
69d7897
build(deps-dev): bump husky from 7.0.4 to 8.0.2 (#2153)
dependabot[bot] Nov 13, 2022
00b3939
ensure that nullable is not set for required parameters (#2079)
erikbrinkman Nov 13, 2022
d6f3ac9
fix: typo (#2010)
hadrien-toma Nov 13, 2022
5d5c803
docs: fix link for useDefaults (#1955)
bradjones1 Nov 13, 2022
f90a1cf
build(deps-dev): bump @rollup/plugin-commonjs from 21.1.0 to 23.0.2 (…
dependabot[bot] Nov 13, 2022
d0beea5
build(deps-dev): bump fast-uri from 1.0.1 to 2.1.0 (#2007)
dependabot[bot] Nov 13, 2022
67e7f1c
docs: fix broken links (#2006)
smithc10 Nov 13, 2022
b2a109c
Update security.md -> ReDoS Attack Section (#1953)
efebarlas Nov 13, 2022
f38d269
docs: remove invisible character in README (#1990)
is2ei Nov 13, 2022
b8f563b
build(deps-dev): bump @rollup/plugin-node-resolve from 13.3.0 to 15.0…
dependabot[bot] Nov 13, 2022
fa4343b
build(deps-dev): bump @types/node from 17.0.45 to 18.11.9 (#2151)
dependabot[bot] Nov 13, 2022
b3c53ed
build(deps-dev): bump mocha from 9.2.2 to 10.0.0 (#1972)
dependabot[bot] Nov 13, 2022
65220d6
build(deps-dev): bump lint-staged from 12.5.0 to 13.0.3 (#2019)
dependabot[bot] Nov 13, 2022
16af695
export ValidationError and MissingRefError, closes #1926, closes #184…
dannyb648 Nov 13, 2022
92e5a1e
8.11.1
epoberezkin Nov 13, 2022
1f83903
8.11.2
epoberezkin Nov 13, 2022
01e644a
readme: update group link
epoberezkin Nov 13, 2022
4ea1bf0
build(deps-dev): bump @rollup/plugin-json from 4.1.0 to 5.0.1 (#2159)
dependabot[bot] Nov 14, 2022
35034b6
build(deps-dev): bump @types/mocha from 9.1.1 to 10.0.0 (#2162)
dependabot[bot] Nov 14, 2022
5c72864
remove leading comma in JTD serialisation result, fixes #2001, fixes …
epoberezkin Jan 2, 2023
a211e8d
JTD empty values schema, fixes #1949 (#2191)
epoberezkin Jan 2, 2023
a489265
correctly narrow "number" type to "integer", fixes #1935 (#2192)
epoberezkin Jan 2, 2023
d2c57d9
build(deps-dev): bump @rollup/plugin-typescript from 8.5.0 to 9.0.2 (…
dependabot[bot] Jan 2, 2023
dab8504
special case empty object for jtd (#2158)
erikbrinkman Jan 2, 2023
a697668
build(deps-dev): bump @rollup/plugin-json from 5.0.2 to 6.0.0 (#2183)
dependabot[bot] Jan 2, 2023
2e5884b
build(deps-dev): bump @rollup/plugin-typescript from 9.0.2 to 10.0.1 …
dependabot[bot] Jan 2, 2023
527d43a
build(deps-dev): bump @rollup/plugin-commonjs from 23.0.7 to 24.0.0 (…
dependabot[bot] Jan 3, 2023
c5c195b
fix JTD discriminator with more than 8 properties, fixes #1971 (#2194)
epoberezkin Jan 3, 2023
321fad6
update node versions (#2195)
epoberezkin Jan 3, 2023
bf1266a
8.12.0
epoberezkin Jan 3, 2023
eaf4615
docs: update schema language comparison
epoberezkin Jan 8, 2023
45583fd
fix prettier
epoberezkin Jan 8, 2023
490eb8c
update chat badge
epoberezkin May 30, 2023
8be3ccf
chore: v8.12.0
tatomyr Jan 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Supports JSON Schema draft-04/06/07/2019-09/2020-12 ([draft-04 support](https://
[![npm](https://img.shields.io/npm/v/ajv.svg)](https://www.npmjs.com/package/ajv)
[![npm downloads](https://img.shields.io/npm/dm/ajv.svg)](https://www.npmjs.com/package/ajv)
[![Coverage Status](https://coveralls.io/repos/github/ajv-validator/ajv/badge.svg?branch=master)](https://coveralls.io/github/ajv-validator/ajv?branch=master)
[![SimpleX](https://img.shields.io/badge/chat-on%20SimpleX-%2307b4b9)](https://simplex.chat/contact#/?v=1&smp=smp%3A%2F%2Fu2dS9sG8nMNURyZwqASV4yROM28Er0luVTx5X1CsMrU%3D%40smp4.simplex.im%2Fap4lMFzfXF8Hzmh-Vz0WNxp_1jKiOa-h%23MCowBQYDK2VuAyEAcdefddRvDfI8iAuBpztm_J3qFucj8MDZoVs_2EcMTzU%3D)
[![SimpleX](https://img.shields.io/badge/chat-on%20SimpleX-70F0F9)](https://simplex.chat/contact#/?v=1-2&smp=smp%3A%2F%2Fu2dS9sG8nMNURyZwqASV4yROM28Er0luVTx5X1CsMrU%3D%40smp4.simplex.im%2F8KvvURM6J38Gdq9dCuPswMOkMny0xCOJ%23%2F%3Fv%3D1-2%26dh%3DMCowBQYDK2VuAyEAr8rPVRuMOXv6kwF2yUAap-eoVg-9ssOFCi1fIrxTUw0%253D%26srv%3Do5vmywmrnaxalvz6wi3zicyftgio6psuvyniis6gco6bp6ekl4cqj4id.onion&data=%7B%22type%22%3A%22group%22%2C%22groupLinkId%22%3A%224pwLRgWHU9tlroMWHz0uOg%3D%3D%22%7D)
[![Gitter](https://img.shields.io/gitter/room/ajv-validator/ajv.svg)](https://gitter.im/ajv-validator/ajv)
[![GitHub Sponsors](https://img.shields.io/badge/$-sponsors-brightgreen)](https://github.com/sponsors/epoberezkin)

Expand All @@ -35,6 +35,7 @@ Please review [Contributing guidelines](./CONTRIBUTING.md) and [Code components]
All documentation is available on the [Ajv website](https://ajv.js.org).

Some useful site links:

- [Getting started](https://ajv.js.org/guide/getting-started.html)
- [JSON Schema vs JSON Type Definition](https://ajv.js.org/guide/schema-language.html)
- [API reference](https://ajv.js.org/api.html)
Expand All @@ -53,7 +54,7 @@ Your continuing support is very important - the funds will be used to develop an
Please sponsor Ajv via:

- [GitHub sponsors page](https://github.com/sponsors/epoberezkin) (GitHub will match it)
- [Ajv Open Collective️](https://opencollective.com/ajv)
- [Ajv Open Collective](https://opencollective.com/ajv)

Thank you.

Expand All @@ -73,6 +74,19 @@ Thank you.
<a href="https://opencollective.com/ajv/organization/9/website"><img src="https://opencollective.com/ajv/organization/9/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/10/website"><img src="https://opencollective.com/ajv/organization/10/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/11/website"><img src="https://opencollective.com/ajv/organization/11/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/12/website"><img src="https://opencollective.com/ajv/organization/12/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/13/website"><img src="https://opencollective.com/ajv/organization/13/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/14/website"><img src="https://opencollective.com/ajv/organization/14/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/15/website"><img src="https://opencollective.com/ajv/organization/15/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/16/website"><img src="https://opencollective.com/ajv/organization/16/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/17/website"><img src="https://opencollective.com/ajv/organization/17/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/18/website"><img src="https://opencollective.com/ajv/organization/18/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/19/website"><img src="https://opencollective.com/ajv/organization/19/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/20/website"><img src="https://opencollective.com/ajv/organization/20/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/21/website"><img src="https://opencollective.com/ajv/organization/21/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/22/website"><img src="https://opencollective.com/ajv/organization/22/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/23/website"><img src="https://opencollective.com/ajv/organization/23/avatar.svg"></a>
<a href="https://opencollective.com/ajv/organization/24/website"><img src="https://opencollective.com/ajv/organization/24/avatar.svg"></a>

## Performance

Expand All @@ -87,7 +101,7 @@ Currently Ajv is the fastest and the most standard compliant validator according

Performance of different validators by [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark):

[![performance](https://chart.googleapis.com/chart?chxt=x,y&cht=bhs&chco=76A4FB&chls=2.0&chbh=62,4,1&chs=600x416&chxl=-1:|ajv|@exodus&#x2F;schemasafe|is-my-json-valid|djv|@cfworker&#x2F;json-schema|jsonschema&chd=t:100,69.2,51.5,13.1,5.1,1.2)](https://github.com/ebdrup/json-schema-benchmark/blob/master/README.md#performance)
[![performance](https://chart.googleapis.com/chart?chxt=x,y&cht=bhs&chco=76A4FB&chls=2.0&chbh=62,4,1&chs=600x416&chxl=-1:|ajv|@exodus/schemasafe|is-my-json-valid|djv|@cfworker/json-schema|jsonschema/=t:100,69.2,51.5,13.1,5.1,1.2)](https://github.com/ebdrup/json-schema-benchmark/blob/master/README.md#performance)

## Features

Expand Down Expand Up @@ -144,15 +158,15 @@ const schema = {
type: "object",
properties: {
foo: {type: "integer"},
bar: {type: "string"}
bar: {type: "string"},
},
required: ["foo"],
additionalProperties: false,
}

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

const validate = ajv.compile(schema)
Expand Down
6 changes: 4 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Every time this method is called the errors are overwritten so you need to copy

If the schema is asynchronous (has `$async` keyword on the top level) this method returns a Promise. See [Asynchronous validation](./guide/async-validation.md).

<a name="add-schema"></a>
<a id="add-schema"></a>

### ajv.addSchema(schema: object | object[], key?: string): Ajv

Expand Down Expand Up @@ -245,7 +245,7 @@ Formats can be also added via `formats` option.

<a name="api-addkeyword"></a>

### ajv.addKeyword(definition: object): Ajv
### ajv.addKeyword(definition: string | object): Ajv

Add validation keyword to Ajv instance.

Expand Down Expand Up @@ -297,6 +297,8 @@ interface KeywordDefinition {
}
```

If only the property `keyword` is provided in the definition object, you can also pass the keyword name as the argument.

`compile`, `macro` and `code` are mutually exclusive, only one should be used at a time. `validate` can be used separately or in addition to `compile` or `macro` to support [\$data reference](./guide/combining-schemas.md#data-reference).

::: tip Keyword is validated only for applicable data types
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 @@ -41,7 +41,7 @@ const ajv = new Ajv()
const validate = ajv.addSchema(defsSchema).compile(schema)
```

See [Options](./api.md#options) and [addSchema](./api.md#add-schema) method.
See [Options](../options.md) and [addSchema](../api.md#add-schema) method.

::: tip Reference resolution
- `$ref` is resolved as the uri-reference using schema \$id as the base URI (see the example).
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
You can try Ajv without installing it in the Node.js REPL: [https://runkit.com/npm/ajv](https://runkit.com/npm/ajv)
:::

To install Ajv version 7:
To install Ajv version 8:

```bash
npm install ajv
Expand Down Expand Up @@ -140,8 +140,8 @@ const parse = ajv.compileParser(schema)
const json = '{"foo": 1, "bar": "abc"}'
const invalidJson = '{"unknown": "abc"}'

console.log(parseAndLog(json)) // logs {foo: 1, bar: "abc"}
console.log(parseAndLog(invalidJson)) // logs error and position
parseAndLog(json) // logs {foo: 1, bar: "abc"}
parseAndLog(invalidJson) // logs error and position

function parseAndLog(json) {
const data = parse(json)
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/modifying-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ See [discriminator](../json-schema.md#discriminator) keyword.

## Assigning defaults

With [option `useDefaults`](./api.md#options) Ajv will assign values from `default` keyword in the schemas of `properties` and `items` (when it is the array of schemas) to the missing properties and items.
With [option `useDefaults`](./options.md#options) Ajv will assign values from `default` keyword in the schemas of `properties` and `items` (when it is the array of schemas) to the missing properties and items.

With the option value `"empty"` properties and items equal to `null` or `""` (empty string) will be considered missing and assigned defaults.

Expand Down
17 changes: 10 additions & 7 deletions docs/guide/schema-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
tags:
- JTD
---

# Choosing schema language

[[toc]]
Expand Down Expand Up @@ -123,17 +124,19 @@ See [JSON Schema](../json-schema.md) for more information and the list of define
- Defines the shape of JSON data via strictly defined schema forms (rather than the collection of restrictions).
- Effective support for tagged unions.
- Designed to protect against user mistakes.
- Supports compilation of schemas to efficient [serializers and parsers](./getting-started.md#parsing-and-serializing-json) (no need to validate as a separate step)
- Approved as [RFC8927](https://datatracker.ietf.org/doc/rfc8927/)
- Supports compilation of schemas to efficient [serializers and parsers](./getting-started.md#parsing-and-serializing-json) (no need to validate as a separate step).
- Approved as [RFC8927](https://datatracker.ietf.org/doc/rfc8927/).
- Substantial industry adoption since it was standardized in 2020, Ajv v8.12.0 fixed all reported JTD bugs.

**Cons**:

- Limited, compared with JSON Schema - no support for untagged unions<sup>\*</sup>, conditionals, references between different schema files<sup>\*\*</sup>, etc.
- No meta-schema in the specification<sup>\*</sup>.
- Brand new - limited industry adoption (as of January 2021).
- Limited, compared with JSON Schema - no support for untagged unions<sup>1</sup>, conditionals, references between different schema files<sup>2</sup>, etc.
- No meta-schema in the specification<sup>3</sup>.

<sup>1</sup> Ajv defines non-standard keyword "union" that can be used inside "metadata" object.

<sup>\*</sup> Ajv defines meta-schema for JTD schemas and non-standard keyword "union" that can be used inside "metadata" object.
<sup>2</sup> You can still combine schemas from multiple files in the application code.

<sup>\*\*</sup> You can still combine schemas from multiple files in the application code.
<sup>3</sup> Ajv defines meta-schema for JTD schemas.

See [JSON Type Definition](../json-type-definition.md) for more information and the list of defined schema forms.
4 changes: 4 additions & 0 deletions docs/json-type-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ Any user-defined keywords that can be used in JSON Schema schemas can also be us
It is strongly recommended to only use it to simplify migration from JSON Schema to JTD and not to use non-standard keywords in the new schemas, as these keywords are not supported by any other tools.
:::

::: warning Parsing does NOT support non-standard JTD keywords
compileParser method does not support non-standard JTD keywords, you will have to use JSON.parse and then validates.
:::

## Validation errors

TODO
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](https://github.com/ajv-validator/ajv/blob/master/spec/keyword.spec.ts#L316).
Macro keywords can 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
2 changes: 1 addition & 1 deletion docs/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ To use a third-party regex engine in Ajv, set the ajv.opts.code.regExp property
```
const Ajv = require("ajv")
const RE2 = require("re2")
const ajv = new Ajv({regExp: RE2})
const ajv = new Ajv({code: {regExp: RE2}})
```

For details about the interface of the `regexp` option, see options.md under the docs folder.
Expand Down
2 changes: 2 additions & 0 deletions lib/2019.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ export {DefinedError} from "./vocabularies/errors"
export {JSONType} from "./compile/rules"
export {JSONSchemaType} from "./types/json-schema"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"
export {default as ValidationError} from "./runtime/validation_error"
export {default as MissingRefError} from "./compile/ref_error"
2 changes: 2 additions & 0 deletions lib/2020.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ export {DefinedError} from "./vocabularies/errors"
export {JSONType} from "./compile/rules"
export {JSONSchemaType} from "./types/json-schema"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"
export {default as ValidationError} from "./runtime/validation_error"
export {default as MissingRefError} from "./compile/ref_error"
2 changes: 2 additions & 0 deletions lib/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ export {DefinedError} from "./vocabularies/errors"
export {JSONType} from "./compile/rules"
export {JSONSchemaType} from "./types/json-schema"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"
export {default as ValidationError} from "./runtime/validation_error"
export {default as MissingRefError} from "./compile/ref_error"
34 changes: 20 additions & 14 deletions lib/compile/jtd/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function serializeValues(cxt: SerializeCxt): void {
gen.add(N.json, str`}`)
}

function serializeKeyValue(cxt: SerializeCxt, key: Name, schema: SchemaObject, first: Name): void {
function serializeKeyValue(cxt: SerializeCxt, key: Name, schema: SchemaObject, first?: Name): void {
const {gen, data} = cxt
addComma(cxt, first)
serializeString({...cxt, data: key})
Expand Down Expand Up @@ -158,20 +158,24 @@ function serializeSchemaProperties(cxt: SerializeCxt, discriminator?: string): v
const optProps = keys(optionalProperties)
const allProps = allProperties(props.concat(optProps))
let first = !discriminator
let firstProp: Name | undefined

for (const key of props) {
if (first) first = false
else gen.add(N.json, str`,`)
serializeProperty(key, properties[key], keyValue(key))
}
if (first) firstProp = gen.let("first", true)
for (const key of optProps) {
const value = keyValue(key)
gen.if(and(_`${value} !== undefined`, isOwnProperty(gen, data, key)), () =>
gen.if(and(_`${value} !== undefined`, isOwnProperty(gen, data, key)), () => {
addComma(cxt, firstProp)
serializeProperty(key, optionalProperties[key], value)
)
})
}
if (schema.additionalProperties) {
gen.forIn("key", data, (key) =>
gen.if(isAdditional(key, allProps), () =>
serializeKeyValue(cxt, key, {}, gen.let("first", first))
)
gen.if(isAdditional(key, allProps), () => serializeKeyValue(cxt, key, {}, firstProp))
)
}

Expand All @@ -192,8 +196,6 @@ function serializeSchemaProperties(cxt: SerializeCxt, discriminator?: string): v
}

function serializeProperty(key: string, propSchema: SchemaObject, value: Name): void {
if (first) first = false
else gen.add(N.json, str`,`)
gen.add(N.json, str`${JSON.stringify(key)}:`)
serializeCode({...cxt, schema: propSchema, data: value})
}
Expand Down Expand Up @@ -253,10 +255,14 @@ function serializeEmpty({gen, data}: SerializeCxt): void {
gen.add(N.json, _`JSON.stringify(${data})`)
}

function addComma({gen}: SerializeCxt, first: Name): void {
gen.if(
first,
() => gen.assign(first, false),
() => gen.add(N.json, str`,`)
)
function addComma({gen}: SerializeCxt, first?: Name): void {
if (first) {
gen.if(
first,
() => gen.assign(first, false),
() => gen.add(N.json, str`,`)
)
} else {
gen.add(N.json, str`,`)
}
}
2 changes: 1 addition & 1 deletion lib/compile/jtd/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export const jtdForms = [
"ref",
] as const

export type JTDForm = typeof jtdForms[number]
export type JTDForm = (typeof jtdForms)[number]
2 changes: 1 addition & 1 deletion lib/compile/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {AddedKeywordDefinition} from "../types"

const _jsonTypes = ["string", "number", "integer", "boolean", "null", "object", "array"] as const

export type JSONType = typeof _jsonTypes[number]
export type JSONType = (typeof _jsonTypes)[number]

const jsonTypes: Set<string> = new Set(_jsonTypes)

Expand Down
11 changes: 10 additions & 1 deletion lib/compile/validate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
}
})

function shouldForceUnevaluatedProperties(schema: AnySchemaObject, rule: Rule): boolean {

Check warning on line 272 in lib/compile/validate/index.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'schema' is already declared in the upper scope on line 260 column 5
return !!(
rule.keyword === "unevaluatedProperties" &&
(schema.properties || schema.patternProperties) &&
Expand Down Expand Up @@ -297,7 +297,7 @@
strictTypesError(it, `type "${t}" not allowed by context "${it.dataTypes.join(",")}"`)
}
})
it.dataTypes = it.dataTypes.filter((t) => includesType(types, t))
narrowSchemaTypes(it, types)
}

function checkMultipleTypes(it: SchemaObjCxt, ts: JSONType[]): void {
Expand Down Expand Up @@ -327,6 +327,15 @@
return ts.includes(t) || (t === "integer" && ts.includes("number"))
}

function narrowSchemaTypes(it: SchemaObjCxt, withTypes: JSONType[]): void {
const ts: JSONType[] = []
for (const t of it.dataTypes) {
if (includesType(withTypes, t)) ts.push(t)
else if (withTypes.includes("integer") && t === "number") ts.push("integer")
}
it.dataTypes = ts
}

function strictTypesError(it: SchemaObjCxt, msg: string): void {
const schemaPath = it.schemaEnv.baseId + it.errSchemaPath
msg += ` at "${schemaPath}" (strictTypes)`
Expand Down
2 changes: 2 additions & 0 deletions lib/jtd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./com

export {JTDSchemaType, SomeJTDSchemaType, JTDDataType}
export {JTDOptions}
export {default as ValidationError} from "./runtime/validation_error"
export {default as MissingRefError} from "./compile/ref_error"
1 change: 1 addition & 0 deletions lib/types/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ type Nullable<T> = undefined extends T
default?: T | null
}
: {
nullable?: false
const?: T
enum?: Readonly<T[]>
default?: T
Expand Down
Loading
Loading