Skip to content

Commit 42c967f

Browse files
authored
Fix optional rule bug (#150)
Fix optional rule bug
2 parents 1cb1d76 + e75dc7f commit 42c967f

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Bug with `optional` rule that ignores validation when an empty string is passed ([#149](https://github.com/imbrn/v8n/issues/149))
13+
1014
## [1.3.0] - 2019-05-19
1115

1216
### Added

src/v8n.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ const availableRules = {
176176
validations.some(validation => validation.test(value)),
177177

178178
optional: (validation, considerTrimmedEmptyString = false) => value => {
179-
if (typeof value === "string" && value.trim() === "")
180-
return considerTrimmedEmptyString;
179+
if (considerTrimmedEmptyString)
180+
return typeof value === "string" && value.trim() === "";
181+
181182
if (value !== undefined && value !== null) validation.check(value);
182183
return true;
183184
}

src/v8n.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,12 @@ describe("rules", () => {
11751175
expect(optional.test(1)).toBe(true);
11761176
expect(optional.test(2)).toBe(true);
11771177
expect(optional.test(1000)).toBe(true);
1178+
1179+
expect(
1180+
v8n()
1181+
.optional(v8n().string())
1182+
.test("")
1183+
).toBe(true);
11781184
});
11791185

11801186
it("should fail when validation fails", () => {

0 commit comments

Comments
 (0)