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

correctly narrow "number" type to "integer", fixes #1935 #2192

Merged
merged 5 commits into from Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/compile/validate/index.ts
Expand Up @@ -286,7 +286,7 @@ function checkContextTypes(it: SchemaObjCxt, types: JSONType[]): void {
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 @@ -316,6 +316,15 @@ function includesType(ts: JSONType[], t: JSONType): boolean {
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
111 changes: 111 additions & 0 deletions spec/issues/1935_integer_narrowing_subschema.spec.ts
@@ -0,0 +1,111 @@
import _Ajv from "../ajv"
import Ajv from "ajv"
import * as assert from "assert"

describe("integer valid type in number sub-schema (issue #1935)", () => {
let ajv: Ajv
before(() => {
ajv = new _Ajv({strict: true})
})

it("should allow integer in `if`", () =>
assert.doesNotThrow(() =>
ajv.compile({
type: "number",
if: {
type: "integer",
maximum: 5,
},
else: {
minimum: 10,
},
})
))

it("should allow integer in `then`", () =>
assert.doesNotThrow(() =>
ajv.compile({
type: "number",
if: {
multipleOf: 2,
},
then: {
type: "integer",
minimum: 10,
},
})
))

it("should allow integer in `else`", () =>
assert.doesNotThrow(() =>
ajv.compile({
type: "number",
if: {
maximum: 5,
},
else: {
type: "integer",
minimum: 10,
},
})
))

it("should allow integer in `allOf`", () =>
assert.doesNotThrow(() =>
ajv.compile({
type: "number",
allOf: [
{
type: "integer",
minimum: 10,
},
{
multipleOf: 2,
},
],
})
))

it("should allow integer in `oneOf`", () =>
assert.doesNotThrow(() =>
ajv.compile({
type: "number",
oneOf: [
{
type: "integer",
minimum: 10,
},
{
multipleOf: 2,
},
],
})
))

it("should allow integer in `anyOf`", () =>
assert.doesNotThrow(() =>
ajv.compile({
type: "number",
oneOf: [
{
type: "integer",
minimum: 10,
},
{
multipleOf: 2,
},
],
})
))

it("should allow integer in `not`", () =>
assert.doesNotThrow(() =>
ajv.compile({
type: "number",
not: {
type: "integer",
minimum: 10,
},
})
))
})