Skip to content

Commit df3c1db

Browse files
committed
Merge branch 'master' into pr/avanelli/304
2 parents 0e24d3a + d3b6b98 commit df3c1db

File tree

7 files changed

+67
-25
lines changed

7 files changed

+67
-25
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"type": "node",
99
"request": "launch",
1010
"name": "Launch dev",
11-
"program": "${workspaceRoot}\\examples\\async.js"
11+
"program": "${workspaceRoot}\\examples\\issue-303.js"
1212
},
1313
{
1414
"type": "node",

examples/issue-303.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const Validator = require("../index");
2+
3+
const v = new Validator({
4+
debug: true,
5+
useNewCustomCheckerFunction: true,
6+
});
7+
8+
const schema = {
9+
dateString: [
10+
{ type: "string", nullable: true },
11+
{ type: "boolean", nullable: true }
12+
]
13+
};
14+
15+
check = v.compile(schema);
16+
17+
console.log("Boolean:", check({ dateString: true })); // Valid
18+
console.log("Date:", check({ dateString: new Date().toISOString() })); // Valid
19+
console.log("Null:", check({ dateString: null })); // Fail
20+

lib/validator.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,13 @@ class Validator {
500500
.every(rule => rule.schema.optional === true);
501501
if (isOptional)
502502
schema.optional = true;
503+
504+
// Check 'nullable' flag
505+
const isNullable = schema.rules
506+
.map(s => this.getRuleFromSchema(s))
507+
.every(rule => rule.schema.nullable === true);
508+
if (isNullable)
509+
schema.nullable = true;
503510
}
504511

505512
if (schema.$$type) {

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/rules/currency.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const v = new Validator();
55

66
describe("Test rule: currency", () => {
77
it("should have decimal optional, and correctly placed if present", () => {
8-
const check = v.compile({$$root: true, type: "currency", 'currencySymbol': '$', 'symbolOptional': true});
8+
const check = v.compile({$$root: true, type: "currency", "currencySymbol": "$", "symbolOptional": true});
99
expect(check("$12.2")).toEqual(true);
1010
expect(check("$12,222.2")).toEqual(true);
1111
expect(check("$12,222")).toEqual(true);
@@ -14,7 +14,7 @@ describe("Test rule: currency", () => {
1414
});
1515

1616
it("should check thousand separator placement is correct", () => {
17-
const check = v.compile({$$root: true, type: "currency", 'currencySymbol': '$', 'symbolOptional': true});
17+
const check = v.compile({$$root: true, type: "currency", "currencySymbol": "$", "symbolOptional": true});
1818
expect(check("$12.2")).toEqual(true);
1919
expect(check("$12,222.2")).toEqual(true);
2020
expect(check("$122,222.2")).toEqual(true);
@@ -29,36 +29,36 @@ describe("Test rule: currency", () => {
2929
});
3030

3131
it("should not allow any other currency symbol, other than supplied in schema", () => {
32-
let check = v.compile({$$root: true, type: "currency", 'currencySymbol': '$', 'symbolOptional': false});
32+
let check = v.compile({$$root: true, type: "currency", "currencySymbol": "$", "symbolOptional": false});
3333
expect(check("$12.2")).toEqual(true);
3434
expect(check("#12.2")).toEqual([{"actual": "#12.2", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}]);
3535
});
3636

3737
it("should keep currency symbol optional, if symbolOptional is true in schema", () => {
38-
let check = v.compile({$$root: true, type: "currency", 'currencySymbol': '$', 'symbolOptional': true});
38+
let check = v.compile({$$root: true, type: "currency", "currencySymbol": "$", "symbolOptional": true});
3939
expect(check("$12.2")).toEqual(true);
4040
expect(check("12.2")).toEqual(true);
4141
expect(check("#12.2")).toEqual([{"actual": "#12.2", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}]
4242
);
4343
});
4444

4545
it("should allow negative currencies", () => {
46-
let check = v.compile({$$root: true, type: "currency", 'currencySymbol': '$', 'symbolOptional': true});
46+
let check = v.compile({$$root: true, type: "currency", "currencySymbol": "$", "symbolOptional": true});
4747
expect(check("-12.2")).toEqual(true);
4848
expect(check("$-12.2")).toEqual(true);
4949
expect(check("-$12.2")).toEqual(true);
5050
expect(check("-$-12.2")).toEqual([{"actual": "-$-12.2", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}]);
5151
});
5252

5353
it("should work correctly with supplied thousand and decimal separator", () => {
54-
let check = v.compile({$$root: true, type: "currency", 'currencySymbol': '$', 'symbolOptional': true, 'thousandSeparator':'.', 'decimalSeparator':','});
54+
let check = v.compile({$$root: true, type: "currency", "currencySymbol": "$", "symbolOptional": true, "thousandSeparator":".", "decimalSeparator":","});
5555
expect(check("$12,2")).toEqual(true);
5656
expect(check("$12.222")).toEqual(true);
5757
expect(check("$12.222,2")).toEqual(true);
5858
expect(check("$12,222.2")).toEqual([{"actual": "$12,222.2", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}]);
5959
});
6060
it("should work correctly with supplied regex pattern", () => {
61-
let check = v.compile({$$root: true, type: "currency", 'customRegex': /123/g});
61+
let check = v.compile({$$root: true, type: "currency", "customRegex": /123/g});
6262
expect(check("123")).toEqual(true);
6363
expect(check("134")).toEqual([{"actual": "134", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}]);
6464
});

test/rules/multi.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ describe("Test rule: multi", () => {
9999

100100
expect(check({ address: "London", age: "22", name: "John", surname: "Doe" })).toEqual([{"type":"number","message":"The 'age' field must be a number.","field":"age","actual":"22"},{"type":"objectStrict","message":"The object '' contains forbidden keys: 'address'.","expected":"age, name, surname","actual":"address"}] );
101101
});
102+
103+
it("issue #303 (nullable with shorthard format)", () => {
104+
const v = new Validator();
105+
const check = v.compile({
106+
dateString: [
107+
{ type: "string", nullable: true },
108+
{ type: "boolean", nullable: true }
109+
]
110+
});
111+
112+
expect(check({ dateString: true })).toBe(true);
113+
expect(check({ dateString: new Date().toISOString() })).toBe(true);
114+
expect(check({ dateString: null })).toBe(true);
115+
expect(check({})).toEqual([{"type":"required","message":"The 'dateString' field is required.","field":"dateString","actual":undefined}] );
116+
});
102117
});
103118

104119
describe("should work with custom validator", () => {

test/rules/number.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe("Test rule: number", () => {
3636
expect(check(5)).toEqual(true);
3737
expect(check(8)).toEqual(true);
3838

39-
expect(v.validate(-1, { $$root: true, type: "number", min: 0})).toEqual([{actual: -1, expected: 0, field: undefined, message: "The '' field must be greater than or equal to 0.", type: 'numberMin'}]);
39+
expect(v.validate(-1, { $$root: true, type: "number", min: 0})).toEqual([{actual: -1, expected: 0, field: undefined, message: "The '' field must be greater than or equal to 0.", type: "numberMin"}]);
4040
});
4141

4242
it("check max", () => {

0 commit comments

Comments
 (0)