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

fix JTD discriminator with more than 8 properties, fixes #1971 #2194

Merged
merged 2 commits into from Jan 3, 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
19 changes: 13 additions & 6 deletions lib/vocabularies/jtd/properties.ts
Expand Up @@ -138,9 +138,7 @@ export function validateProperties(cxt: KeywordCxt): void {

function validateAdditional(): void {
gen.forIn("key", data, (key: Name) => {
const _allProps =
it.jtdDiscriminator === undefined ? allProps : [it.jtdDiscriminator].concat(allProps)
const addProp = isAdditional(key, _allProps, "properties")
const addProp = isAdditional(key, allProps, "properties", it.jtdDiscriminator)
const addOptProp = isAdditional(key, allOptProps, "optionalProperties")
const extra =
addProp === true ? addOptProp : addOptProp === true ? addProp : and(addProp, addOptProp)
Expand All @@ -159,14 +157,23 @@ export function validateProperties(cxt: KeywordCxt): void {
})
}

function isAdditional(key: Name, props: string[], keyword: string): Code | true {
function isAdditional(
key: Name,
props: string[],
keyword: string,
jtdDiscriminator?: string
): Code | true {
let additional: Code | boolean
if (props.length > 8) {
// TODO maybe an option instead of hard-coded 8?
const propsSchema = schemaRefOrVal(it, parentSchema[keyword], keyword)
additional = not(isOwnProperty(gen, propsSchema as Code, key))
} else if (props.length) {
additional = and(...props.map((p) => _`${key} !== ${p}`))
if (jtdDiscriminator !== undefined) {
additional = and(additional, _`${key} !== ${jtdDiscriminator}`)
}
} else if (props.length || jtdDiscriminator !== undefined) {
const ps = jtdDiscriminator === undefined ? props : [jtdDiscriminator].concat(props)
additional = and(...ps.map((p) => _`${key} !== ${p}`))
} else {
additional = true
}
Expand Down
61 changes: 61 additions & 0 deletions spec/issues/1971_jtd_discriminator.spec.ts
@@ -0,0 +1,61 @@
import _Ajv from "../ajv_jtd"
import * as assert from "assert"

describe("JTD discriminator with more than 8 (hardcoded in properties.ts) properties (issue #1971)", () => {
const ajv = new _Ajv()

it("should correctly validate empty values form", () => {
const schema = {
discriminator: "tag",
mapping: {
manual: {
properties: {
first: {type: "uint16"},
second: {type: "uint16"},
third: {type: "uint16"},
fourth: {type: "uint16"},
fifth: {type: "uint16"},
sixth: {type: "uint16"},
additionalOne: {type: "uint16"},
additionalTwo: {type: "uint16"},
},
},
auto: {
properties: {
first: {type: "uint16"},
second: {type: "uint16"},
third: {type: "uint16"},
fourth: {type: "uint16"},
fifth: {type: "uint16"},
sixth: {type: "uint16"},
additionalThree: {type: "uint16"},
},
},
},
}
const data1 = {
tag: "manual",
first: 1,
second: 1,
third: 1,
fourth: 1,
fifth: 1,
sixth: 1,
additionalOne: 1,
additionalTwo: 1,
}
const data2 = {
tag: "auto",
first: 1,
second: 1,
third: 1,
fourth: 1,
fifth: 1,
sixth: 1,
additionalThree: 1,
}
const validate = ajv.compile(schema)
assert.strictEqual(validate(data1), true)
assert.strictEqual(validate(data2), true)
})
})