Skip to content

Commit

Permalink
ensure that nullable is not set for required parameters (ajv-validato…
Browse files Browse the repository at this point in the history
…r#2079)

nullable was enforced for optional parameters, but not forbidden for
required parameters. This tests and enforces the latter case.

fixes ajv-validator#2030

Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
  • Loading branch information
erikbrinkman and epoberezkin committed Nov 13, 2022
1 parent 69d7897 commit 00b3939
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/types/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ type Nullable<T> = undefined extends T
default?: T | null
}
: {
nullable?: false
const?: T
enum?: Readonly<T[]>
default?: T
Expand Down
17 changes: 17 additions & 0 deletions spec/types/json-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@ describe("JSONSchemaType type and validation as a type guard", () => {
// eslint-disable-next-line no-void
void optionalSchema
})

it("won't accept nullable for non-null types", () => {
// @ts-expect-error can't set nullable
const nonNullSchema: JSONSchemaType<{a: number}> = {
type: "object",
properties: {
a: {
type: "number",
nullable: true,
},
},
required: [],
}

// eslint-disable-next-line no-void
void nonNullSchema
})
})

describe("schema works for primitives", () => {
Expand Down

0 comments on commit 00b3939

Please sign in to comment.