Skip to content

Commit fe92395

Browse files
committed
should not replace null when field is not explicitly nullable
1 parent 927e0c4 commit fe92395

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

lib/validator.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,26 @@ class Validator {
113113
*/
114114
wrapRequiredCheckSourceCode(rule, innerSrc, context, resVar) {
115115
const src = [];
116+
const {considerNullAsAValue = false} = this.opts;
116117
let handleNoValue;
117118

118119
let skipUndefinedValue = rule.schema.optional === true || rule.schema.type === "forbidden";
119-
let skipNullValue = this.opts.considerNullAsAValue ?
120+
let skipNullValue = considerNullAsAValue ?
120121
rule.schema.nullable !== false || rule.schema.type === "forbidden" :
121122
rule.schema.optional === true || rule.schema.nullable === true || rule.schema.type === "forbidden";
122123

123-
if (rule.schema.default != null) {
124+
const ruleHasDefault = considerNullAsAValue ?
125+
rule.schema.default != undefined && rule.schema.default != null :
126+
rule.schema.default != undefined;
127+
128+
if (ruleHasDefault) {
124129
// We should set default-value when value is undefined or null, not skip! (Except when null is allowed)
125130
skipUndefinedValue = false;
126-
if (rule.schema.nullable !== true) skipNullValue = false;
131+
if (considerNullAsAValue) {
132+
if (rule.schema.nullable === false) skipNullValue = false;
133+
} else {
134+
if (rule.schema.nullable !== true) skipNullValue = false;
135+
}
127136

128137
let defaultValue;
129138
if (typeof rule.schema.default === "function") {

test/integration.spec.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,15 +1216,15 @@ describe("Test nullable option", () => {
12161216
const v = new Validator({considerNullAsAValue: true});
12171217

12181218
it("should throw error if value is undefined", () => {
1219-
const schema = { foo: { type: "number", nullable: true } };
1219+
const schema = { foo: { type: "number" } };
12201220
const check = v.compile(schema);
12211221

12221222
expect(check(check)).toBeInstanceOf(Array);
12231223
expect(check({ foo: undefined })).toBeInstanceOf(Array);
12241224
});
12251225

12261226
it("should not throw error if value is null", () => {
1227-
const schema = { foo: { type: "number", nullable: true } };
1227+
const schema = { foo: { type: "number" } };
12281228
const check = v.compile(schema);
12291229

12301230
const o = { foo: null };
@@ -1233,13 +1233,13 @@ describe("Test nullable option", () => {
12331233
});
12341234

12351235
it("should not throw error if value exist", () => {
1236-
const schema = { foo: { type: "number", nullable: true } };
1236+
const schema = { foo: { type: "number" } };
12371237
const check = v.compile(schema);
12381238
expect(check({ foo: 2 })).toBe(true);
12391239
});
12401240

12411241
it("should set default value if there is a default", () => {
1242-
const schema = { foo: { type: "number", nullable: true, default: 5 } };
1242+
const schema = { foo: { type: "number", default: 5 } };
12431243
const check = v.compile(schema);
12441244

12451245
const o1 = { foo: 2 };
@@ -1252,7 +1252,7 @@ describe("Test nullable option", () => {
12521252
});
12531253

12541254
it("should not set default value if current value is null", () => {
1255-
const schema = { foo: { type: "number", nullable: true, default: 5 } };
1255+
const schema = { foo: { type: "number", default: 5 } };
12561256
const check = v.compile(schema);
12571257

12581258
const o = { foo: null };
@@ -1261,7 +1261,7 @@ describe("Test nullable option", () => {
12611261
});
12621262

12631263
it("should work with optional", () => {
1264-
const schema = { foo: { type: "number", nullable: true, optional: true } };
1264+
const schema = { foo: { type: "number", optional: true } };
12651265
const check = v.compile(schema);
12661266

12671267
expect(check({ foo: 3 })).toBe(true);
@@ -1270,7 +1270,7 @@ describe("Test nullable option", () => {
12701270
});
12711271

12721272
it("should work with optional and default", () => {
1273-
const schema = { foo: { type: "number", nullable: true, optional: true, default: 5 } };
1273+
const schema = { foo: { type: "number", optional: true, default: 5 } };
12741274
const check = v.compile(schema);
12751275

12761276
expect(check({ foo: 3 })).toBe(true);
@@ -1294,14 +1294,14 @@ describe("Test nullable option", () => {
12941294
expect(check({ foo: null })).toEqual([{"actual": null, "field": "foo", "message": "The 'foo' field is required.", "type": "required"}]);
12951295
});
12961296

1297-
it("should accept null as value", () => {
1298-
const schema = {foo: {type: "number", nullable: true, optional: false}};
1297+
it("should not accept null as value", () => {
1298+
const schema = {foo: {type: "number", nullable: false}};
12991299
const check = v.compile(schema);
13001300

13011301
expect(check({ foo: 3 })).toBe(true);
13021302
expect(check({ foo: undefined })).toEqual([{"actual": undefined, "field": "foo", "message": "The 'foo' field is required.", "type": "required"}]);
13031303
expect(check({})).toEqual([{"actual": undefined, "field": "foo", "message": "The 'foo' field is required.", "type": "required"}]);
1304-
expect(check({ foo: null })).toBe(true);
1304+
expect(check({ foo: null })).toEqual([{"actual": null, "field": "foo", "message": "The 'foo' field is required.", "type": "required"}]);
13051305
});
13061306
});
13071307
});

test/typescript/integration.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,15 +1235,15 @@ describe('TypeScript Definitions', () => {
12351235
const v = new Validator({considerNullAsAValue: true});
12361236

12371237
it("should throw error if value is undefined", () => {
1238-
const schema = { foo: { type: "number", nullable: true } };
1238+
const schema = { foo: { type: "number" } };
12391239
const check = v.compile(schema);
12401240

12411241
expect(check(check)).toBeInstanceOf(Array);
12421242
expect(check({ foo: undefined })).toBeInstanceOf(Array);
12431243
});
12441244

12451245
it("should not throw error if value is null", () => {
1246-
const schema = { foo: { type: "number", nullable: true } };
1246+
const schema = { foo: { type: "number" } };
12471247
const check = v.compile(schema);
12481248

12491249
const o = { foo: null };
@@ -1252,13 +1252,13 @@ describe('TypeScript Definitions', () => {
12521252
});
12531253

12541254
it("should not throw error if value exist", () => {
1255-
const schema = { foo: { type: "number", nullable: true } };
1255+
const schema = { foo: { type: "number" } };
12561256
const check = v.compile(schema);
12571257
expect(check({ foo: 2 })).toBe(true);
12581258
});
12591259

12601260
it("should set default value if there is a default", () => {
1261-
const schema = { foo: { type: "number", nullable: true, default: 5 } };
1261+
const schema = { foo: { type: "number", default: 5 } };
12621262
const check = v.compile(schema);
12631263

12641264
const o1 = { foo: 2 };
@@ -1271,7 +1271,7 @@ describe('TypeScript Definitions', () => {
12711271
});
12721272

12731273
it("should not set default value if current value is null", () => {
1274-
const schema = { foo: { type: "number", nullable: true, default: 5 } };
1274+
const schema = { foo: { type: "number", default: 5 } };
12751275
const check = v.compile(schema);
12761276

12771277
const o = { foo: null };
@@ -1280,7 +1280,7 @@ describe('TypeScript Definitions', () => {
12801280
});
12811281

12821282
it("should work with optional", () => {
1283-
const schema = { foo: { type: "number", nullable: true, optional: true } };
1283+
const schema = { foo: { type: "number", optional: true } };
12841284
const check = v.compile(schema);
12851285

12861286
expect(check({ foo: 3 })).toBe(true);
@@ -1289,7 +1289,7 @@ describe('TypeScript Definitions', () => {
12891289
});
12901290

12911291
it("should work with optional and default", () => {
1292-
const schema = { foo: { type: "number", nullable: true, optional: true, default: 5 } };
1292+
const schema = { foo: { type: "number", optional: true, default: 5 } };
12931293
const check = v.compile(schema);
12941294

12951295
expect(check({ foo: 3 })).toBe(true);
@@ -1313,14 +1313,14 @@ describe('TypeScript Definitions', () => {
13131313
expect(check({ foo: null })).toEqual([{"actual": null, "field": "foo", "message": "The 'foo' field is required.", "type": "required"}]);
13141314
});
13151315

1316-
it("should accept null as value", () => {
1317-
const schema = {foo: {type: "number", nullable: true, optional: false}};
1316+
it("should not accept null as value", () => {
1317+
const schema = {foo: {type: "number", nullable: false}};
13181318
const check = v.compile(schema);
1319-
1319+
13201320
expect(check({ foo: 3 })).toBe(true);
13211321
expect(check({ foo: undefined })).toEqual([{"actual": undefined, "field": "foo", "message": "The 'foo' field is required.", "type": "required"}]);
13221322
expect(check({})).toEqual([{"actual": undefined, "field": "foo", "message": "The 'foo' field is required.", "type": "required"}]);
1323-
expect(check({ foo: null })).toBe(true);
1323+
expect(check({ foo: null })).toEqual([{"actual": null, "field": "foo", "message": "The 'foo' field is required.", "type": "required"}]);
13241324
});
13251325
});
13261326
});

0 commit comments

Comments
 (0)