Skip to content

Commit

Permalink
fix: skip validating null values
Browse files Browse the repository at this point in the history
  • Loading branch information
thondery committed Nov 2, 2023
1 parent fdcc861 commit d3982b1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
9 changes: 7 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ function validateRule(rules, customize) {
for (var rules_1 = __values(rules), rules_1_1 = rules_1.next(); !rules_1_1.done; rules_1_1 = rules_1.next()) {
var rule = rules_1_1.value;
var required = rule.required, message = rule.message, min = rule.min, max = rule.max, pattern = rule.pattern, validator = rule.validator, code = rule.code;
if (required && (lodash_1.isUndefined(value) || value === '')) {
throw errorInfo(message, code || errorCode);
if (lodash_1.isUndefined(value) || value === '') {
if (required) {
throw errorInfo(message, code || errorCode);
}
else {
break;
}
}
if (lodash_1.isString(value)) {
if (min && checkLength(value) < min) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parse-string",
"version": "1.2.7",
"version": "1.2.8",
"description": "Parse the string into a Map.",
"main": "dist/index.js",
"typings": "types/index.d.ts",
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ function validateRule (rules: FilterData.rule[], customize?: Record<string, Func
return (value: any, errorCode?: number) => {
for (let rule of rules ) {
let { required, message, min, max, pattern, validator, code } = rule
if (required && (isUndefined(value) || value === '')) {
throw errorInfo(message, code || errorCode)
if (isUndefined(value) || value === '') {
if (required) {
throw errorInfo(message, code || errorCode)
}
else {
break
}
}
if (isString(value)) {
if (min && checkLength(value) < min) {
Expand Down

0 comments on commit d3982b1

Please sign in to comment.