Skip to content

Commit 896256f

Browse files
authored
Allow 0 numerical values as args. (#7)
1 parent 0bea5d9 commit 896256f

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
- Allow `0` numerical values as validator arguments. This is technically a _breaking change_, but is not _change_ per say because it should have been done since the beginning.
1213
- Empty strings are now valid in every rule validating strings, except `required`. This is technically a _breaking change_, but is not _change_ per say because it should have been done since the beginning.
1314

1415
## [1.0.2] - 2025-04-28

src/__tests__/validator.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { describe, it, expect } from "vitest";
1+
import { describe, it, expect, test } from "vitest";
22

33
import Validator from "../validator";
44
import containsNonAlphanumeric from "../rules/containsNonAlphanumeric";
55
import email from "../rules/email";
6+
import minimumValue from "../rules/minimumValue";
67
import required from "../rules/required";
78
import type {
89
RuleConfiguration,
@@ -293,4 +294,20 @@ describe("Validator", () => {
293294
expect(result.rules.notEmpty.value).toBe("");
294295
expect(JSON.stringify(result.rules.notEmpty.custom)).toBe(JSON.stringify({ value: " ", trimmed: "" }));
295296
});
297+
298+
test.each([0, -0])("should succeed when validation rules arguments are 0 (number)", (args) => {
299+
const validator = new Validator();
300+
validator.setRule("minimumValue", minimumValue);
301+
const result: ValidationResult = validator.validate("age", 0, { minimumValue: args });
302+
expect(result.isValid).toBe(true);
303+
expect(result.rules.minimumValue.severity).toBe("information");
304+
});
305+
306+
it.concurrent("should succeed when validation rules arguments are 0 (BigInt)", () => {
307+
const validator = new Validator();
308+
validator.setRule("minimumValue", minimumValue);
309+
const result: ValidationResult = validator.validate("age", 0, { minimumValue: BigInt(0) });
310+
expect(result.isValid).toBe(true);
311+
expect(result.rules.minimumValue.severity).toBe("information");
312+
});
296313
});

src/validator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ class Validator {
187187
}
188188

189189
const args: unknown = rules[key];
190-
if (!args) {
190+
if (typeof args === "undefined" || args === null || args === false || args === "" || (typeof args === "number" && isNaN(args))) {
191+
// NOTE(fpion): 0, -0 and 0n (BigInt) are considered valid arguments.
191192
continue;
192193
}
193194

0 commit comments

Comments
 (0)