Skip to content

Commit

Permalink
fix incorrect code for additional properties when there are "many" pr…
Browse files Browse the repository at this point in the history
…operties, closes #1501
  • Loading branch information
epoberezkin committed Mar 19, 2021
1 parent 2ada8d6 commit 3ae14a3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/vocabularies/jtd/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {CodeKeywordDefinition} from "../../types"
import type KeywordCxt from "../../compile/context"
import {propertyInData, allSchemaProperties, isOwnProperty} from "../code"
import {alwaysValidSchema, schemaRefOrVal} from "../../compile/util"
import {_, and, Code, Name} from "../../compile/codegen"
import {_, and, not, Code, Name} from "../../compile/codegen"
import {checkMetadata} from "./metadata"
import {checkNullableObject} from "./nullable"

Expand Down Expand Up @@ -116,7 +116,7 @@ export function validateProperties(cxt: KeywordCxt): void {
if (props.length > 8) {
// TODO maybe an option instead of hard-coded 8?
const propsSchema = schemaRefOrVal(it, parentSchema[keyword], keyword)
additional = isOwnProperty(gen, propsSchema as Code, key)
additional = not(isOwnProperty(gen, propsSchema as Code, key))
} else if (props.length) {
additional = and(...props.map((p) => _`${key} !== ${p}`))
} else {
Expand Down
27 changes: 27 additions & 0 deletions spec/issues/1501_jtd_many_properties.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type Ajv from "../.."
import _Ajv from "../ajv_jtd"
import * as assert from "assert"

const PROP_COUNT = 10

describe("schema with many properties", () => {
let ajv: Ajv
const schema = {properties: {}}
const data = {}
const invalidData = {}

before(() => {
ajv = new _Ajv()
for (let i = 0; i < PROP_COUNT; i++) {
const prop = `prop${i}`
schema.properties[prop] = {type: "uint16"}
data[prop] = i
invalidData[prop] = -i
}
})

it("should correctly compile reference to schema", () => {
assert.strictEqual(ajv.validate(schema, data), true)
assert.strictEqual(ajv.validate(schema, invalidData), false)
})
})

0 comments on commit 3ae14a3

Please sign in to comment.