Skip to content

Commit 9e81f1a

Browse files
committed
add specific test for default value
1 parent fe92395 commit 9e81f1a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

test/integration.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,24 @@ describe("Test nullable option", () => {
12601260
expect(o.foo).toBe(null);
12611261
});
12621262

1263+
it("should set default value if current value is null but can't be", () => {
1264+
const schema = { foo: { type: "number", default: 5, nullable: false } };
1265+
const check = v.compile(schema);
1266+
1267+
const o = { foo: null };
1268+
expect(check(o)).toBe(true);
1269+
expect(o.foo).toBe(5);
1270+
});
1271+
1272+
it("should set default value if current value is null but optional", () => {
1273+
const schema = { foo: { type: "number", default: 5, nullable: false, optional: true } };
1274+
const check = v.compile(schema);
1275+
1276+
const o = { foo: null };
1277+
expect(check(o)).toBe(true);
1278+
expect(o.foo).toBe(5);
1279+
});
1280+
12631281
it("should work with optional", () => {
12641282
const schema = { foo: { type: "number", optional: true } };
12651283
const check = v.compile(schema);

test/typescript/integration.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,24 @@ describe('TypeScript Definitions', () => {
12791279
expect(o.foo).toBe(null);
12801280
});
12811281

1282+
it("should set default value if current value is null but can't be", () => {
1283+
const schema = { foo: { type: "number", default: 5, nullable: false } };
1284+
const check = v.compile(schema);
1285+
1286+
const o = { foo: null };
1287+
expect(check(o)).toBe(true);
1288+
expect(o.foo).toBe(5);
1289+
});
1290+
1291+
it("should set default value if current value is null but optional", () => {
1292+
const schema = { foo: { type: "number", default: 5, nullable: false, optional: true } };
1293+
const check = v.compile(schema);
1294+
1295+
const o = { foo: null };
1296+
expect(check(o)).toBe(true);
1297+
expect(o.foo).toBe(5);
1298+
});
1299+
12821300
it("should work with optional", () => {
12831301
const schema = { foo: { type: "number", optional: true } };
12841302
const check = v.compile(schema);

0 commit comments

Comments
 (0)