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

JTD empty values schema, fixes #1949 #2191

Merged
merged 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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