Skip to content

Commit dd078e5

Browse files
committed
feat: alter really empty behaviour
1 parent 95218bb commit dd078e5

File tree

13 files changed

+4100
-36
lines changed

13 files changed

+4100
-36
lines changed

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const messages = require('./messages/index');
22
const rules = require('./rules/index');
33
const Validator = require('./validator');
44
const objUtil = require('./util/obj');
5+
const empty = require('./util/empty');
56

67
/**
78
* set default language
@@ -11,7 +12,7 @@ function setLang(lang) {
1112
messages.defaultLang = lang;
1213
}
1314

14-
function extend(name, callback, isImplicit=false) {
15+
function extend(name, callback, isImplicit = false) {
1516
rules[name] = callback;
1617
if (isImplicit) Validator.addImplicitRule(name);
1718
}
@@ -132,4 +133,5 @@ module.exports = {
132133
bailable,
133134
assert,
134135
setStrNotationRepetition: objUtil.setStrNotationRepetition,
136+
empty,
135137
};

lib/rules/acceptedIf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { reallyEmpty } = require('../util/empty');
1+
const empty = require('../util/empty');
22
const { pathIndex } = require('../util/ObjectIndex');
33

44
module.exports = function acceptedIf({ attr, value, args }, validator) {
@@ -22,7 +22,7 @@ module.exports = function acceptedIf({ attr, value, args }, validator) {
2222
}
2323

2424
// field can be true if all values are presented
25-
if (!reallyEmpty(pathIndex(validator.inputs, requiredField))
25+
if (!empty.reallyEmptyTrimmed(pathIndex(validator.inputs, requiredField))
2626
&& pathIndex(validator.inputs, requiredField).toString() === requiredValue) {
2727
canbetrue = true;
2828
} else {

lib/rules/acceptedNotIf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { reallyEmpty } = require('../util/empty');
1+
const empty = require('../util/empty');
22
const { pathIndex } = require('../util/ObjectIndex');
33

44
module.exports = function acceptedNotIf({ attr, value, args }, validator) {
@@ -22,7 +22,7 @@ module.exports = function acceptedNotIf({ attr, value, args }, validator) {
2222
}
2323

2424
// field can be true if all values are presented
25-
if (!reallyEmpty(pathIndex(validator.inputs, requiredField))
25+
if (!empty.reallyEmptyTrimmed(pathIndex(validator.inputs, requiredField))
2626
&& pathIndex(validator.inputs, requiredField).toString() === requiredValue) {
2727
canbetrue = true;
2828
} else {

lib/rules/required.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { reallyEmpty } = require('../util/empty');
1+
const empty = require('../util/empty');
22

33
module.exports = function required({ value }) {
4-
if (reallyEmpty(value)) {
4+
if (empty.reallyEmptyTrimmed(value)) {
55
return false;
66
}
77
return true;

lib/rules/requiredIf.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { reallyEmpty } = require('../util/empty');
1+
const empty = require('../util/empty');
22
const { pathIndex } = require('../util/ObjectIndex');
33

44
module.exports = function requiredIf({ attr, value, args }, v) {
@@ -20,7 +20,7 @@ module.exports = function requiredIf({ attr, value, args }, v) {
2020
}
2121

2222
// field is required if all values are presented
23-
if (!reallyEmpty(pathIndex(v.inputs, requiredField))
23+
if (!empty.reallyEmptyTrimmed(pathIndex(v.inputs, requiredField))
2424
&& pathIndex(v.inputs, requiredField).toString() === requiredValue) {
2525
required = true;
2626
} else {
@@ -29,7 +29,7 @@ module.exports = function requiredIf({ attr, value, args }, v) {
2929
}
3030
}
3131

32-
if (required && reallyEmpty(value)) {
32+
if (required && empty.reallyEmptyTrimmed(value)) {
3333
return false;
3434
}
3535

lib/rules/requiredWith.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { reallyEmpty } = require('../util/empty');
1+
const empty = require('../util/empty');
22
const { pathIndex } = require('../util/ObjectIndex');
33

44
module.exports = function requiredWith({ attr, value, args }, v) {
@@ -13,13 +13,13 @@ module.exports = function requiredWith({ attr, value, args }, v) {
1313
continue;
1414
}
1515

16-
if (!reallyEmpty(pathIndex(v.inputs, args[i]))) {
16+
if (!empty.reallyEmptyTrimmed(pathIndex(v.inputs, args[i]))) {
1717
required = true;
1818
break;
1919
}
2020
}
2121

22-
if (required && reallyEmpty(value)) {
22+
if (required && empty.reallyEmptyTrimmed(value)) {
2323
return false;
2424
}
2525

lib/rules/requiredWithout.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { reallyEmpty } = require('../util/empty');
1+
const empty = require('../util/empty');
22
const { pathIndex } = require('../util/ObjectIndex');
33

44
module.exports = function requiredWithout({ attr, value, args }, v) {
@@ -14,13 +14,13 @@ module.exports = function requiredWithout({ attr, value, args }, v) {
1414
}
1515

1616
// @ts-ignore
17-
if (reallyEmpty(pathIndex(v.inputs, args[i]))) {
17+
if (empty.reallyEmptyTrimmed(pathIndex(v.inputs, args[i]))) {
1818
required = true;
1919
break;
2020
}
2121
}
2222

23-
if (required && reallyEmpty(value)) {
23+
if (required && empty.reallyEmptyTrimmed(value)) {
2424
return false;
2525
}
2626

lib/rules/sometimes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const _has = require('lodash.has');
2-
const { reallyEmpty } = require('../util/empty');
2+
const empty = require('../util/empty');
33

44
module.exports = function sometimes({ attr, value }, v) {
55
// @ts-ignore
66
if (!_has(v.inputs, attr)) {
77
return true;
88
}
99

10-
if (reallyEmpty(value)) {
10+
if (empty.reallyEmptyTrimmed(value)) {
1111
return false;
1212
}
1313

lib/util/empty.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1-
module.exports.reallyEmpty = function reallyEmpty(value) {
1+
function reallyEmpty(value) {
2+
if (!value && [false, 0].indexOf(value) < 0) {
3+
return true;
4+
}
5+
6+
return !value.toString();
7+
};
8+
9+
function reallyEmptyTrimmed(value) {
210
if (!value && [false, 0].indexOf(value) < 0) {
311
return true;
412
}
513

614
return !value.toString().trim();
715
};
16+
17+
module.exports = {
18+
reallyEmpty,
19+
reallyEmptyTrimmed,
20+
alter(prop, value) {
21+
module.exports[prop] = value;
22+
},
23+
};
24+

lib/validator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const postRules = require('./postRules/index');
33
// const filters = require('./filters/index');
44
const messages = require('./messages/index');
55
const messageParser = require('./util/messageParser');
6-
const { reallyEmpty } = require('./util/empty');
6+
const empty = require('./util/empty');
77
const { strNotations } = require('./util/obj');
88

99
// you can change this value with static method
@@ -348,7 +348,7 @@ class Validator {
348348
}
349349

350350
// if value is really empty, skip validation
351-
if (!validation.required && reallyEmpty(validation.value)) {
351+
if (!validation.required && empty.reallyEmpty(validation.value)) {
352352
continue;
353353
}
354354

0 commit comments

Comments
 (0)