Skip to content

Commit

Permalink
JTD empty values schema, fixes #1949 (#2191)
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin authored Jan 2, 2023
1 parent 5c72864 commit a211e8d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
17 changes: 10 additions & 7 deletions lib/vocabularies/jtd/values.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {CodeKeywordDefinition, SchemaObject} from "../../types"
import type {KeywordCxt} from "../../compile/validate"
import {alwaysValidSchema, Type} from "../../compile/util"
import {not, Name} from "../../compile/codegen"
import {not, or, Name} from "../../compile/codegen"
import {checkMetadata} from "./metadata"
import {checkNullableObject} from "./nullable"
import {typeError, _JTDTypeError} from "./error"
Expand All @@ -15,13 +15,16 @@ const def: CodeKeywordDefinition = {
code(cxt: KeywordCxt) {
checkMetadata(cxt)
const {gen, data, schema, it} = cxt
if (alwaysValidSchema(it, schema)) return
const [valid, cond] = checkNullableObject(cxt, data)
gen.if(cond)
gen.assign(valid, validateMap())
gen.elseIf(not(valid))
cxt.error()
gen.endIf()
if (alwaysValidSchema(it, schema)) {
gen.if(not(or(cond, valid)), () => cxt.error())
} else {
gen.if(cond)
gen.assign(valid, validateMap())
gen.elseIf(not(valid))
cxt.error()
gen.endIf()
}
cxt.ok(valid)

function validateMap(): Name | boolean {
Expand Down
28 changes: 28 additions & 0 deletions spec/issues/1949_jtd_empty_values.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import _Ajv from "../ajv_jtd"
import * as assert from "assert"

describe("JTD values with empty schema (issue #1949)", () => {
const ajv = new _Ajv()

it("should correctly validate empty values form", () => {
const schema = {values: {}}
const validate = ajv.compile(schema)
assert.strictEqual(validate({prop1: 1, prop2: 2}), true)
assert.strictEqual(validate({}), true)
assert.strictEqual(validate(null), false)
assert.strictEqual(validate(1), false)
assert.strictEqual(validate("foo"), false)
assert.strictEqual(validate(undefined), false)
})

it("should correctly validate nullable empty values form", () => {
const schema = {values: {}, nullable: true}
const validate = ajv.compile(schema)
assert.strictEqual(validate({prop1: 1, prop2: 2}), true)
assert.strictEqual(validate({}), true)
assert.strictEqual(validate(null), true)
assert.strictEqual(validate(1), false)
assert.strictEqual(validate("foo"), false)
assert.strictEqual(validate(undefined), false)
})
})
2 changes: 1 addition & 1 deletion spec/issues/2001_jtd_only_optional_properties.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _Ajv from "../ajv_jtd"
import * as assert from "assert"

describe("schema with optional/additional properties only", () => {
describe("JTD schema with optional/additional properties only (issue #2001)", () => {
const ajv = new _Ajv()

it("should correctly serialize optional properties", () => {
Expand Down

0 comments on commit a211e8d

Please sign in to comment.