Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,20 @@ describe.each(testVersions)(

await expect(execute(undefined)).resolves.toBe(0)
})

it("supports default values of null when nullable", async () => {
const {code, execute} = await getActualFromModel({
...base,
nullable: true,
default: null,
})

expect(code).toMatchInlineSnapshot(
`"const x = joi.number().allow(null).default(null)"`,
)

await expect(execute(undefined)).resolves.toBeNull()
})
})

describe("strings", () => {
Expand Down Expand Up @@ -766,6 +780,20 @@ describe.each(testVersions)(
await expect(execute(undefined)).resolves.toBe("example")
})

it("supports default values of null when nullable", async () => {
const {code, execute} = await getActualFromModel({
...base,
nullable: true,
default: null,
})

expect(code).toMatchInlineSnapshot(
`"const x = joi.string().allow(null).default(null)"`,
)

await expect(execute(undefined)).resolves.toBeNull()
})

it("supports empty string default values", async () => {
const {code, execute} = await getActualFromModel({
...base,
Expand Down Expand Up @@ -906,6 +934,20 @@ describe.each(testVersions)(
await expect(execute(undefined)).resolves.toBe(true)
})

it("supports default values of null when nullable", async () => {
const {code, execute} = await getActualFromModel({
...base,
nullable: true,
default: null,
})

expect(code).toMatchInlineSnapshot(
`"const x = joi.boolean().truthy(1, "1").falsy(0, "0").allow(null).default(null)"`,
)

await expect(execute(undefined)).resolves.toBeNull()
})

it("support enum of 'true'", async () => {
const {code, execute} = await getActualFromModel({
...base,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ export class JoiBuilder extends AbstractSchemaBuilder<
// for stuff like this, eg: https://github.com/github/rest-api-description/issues/3878
// lets coerce defaults to be strings when the model type is expecting that.
const needsWrapping =
model.type === "string" && typeof model.default !== "string"
model.type === "string" &&
typeof model.default !== "string" &&
!(model.nullable && model.default === null)

const defaultValue = JSON.stringify(model.default)

return [
Expand Down