Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- 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.
- 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.

## [1.0.2] - 2025-04-28
Expand Down
19 changes: 18 additions & 1 deletion src/__tests__/validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, test } from "vitest";

import Validator from "../validator";
import containsNonAlphanumeric from "../rules/containsNonAlphanumeric";
import email from "../rules/email";
import minimumValue from "../rules/minimumValue";
import required from "../rules/required";
import type {
RuleConfiguration,
Expand Down Expand Up @@ -293,4 +294,20 @@ describe("Validator", () => {
expect(result.rules.notEmpty.value).toBe("");
expect(JSON.stringify(result.rules.notEmpty.custom)).toBe(JSON.stringify({ value: " ", trimmed: "" }));
});

test.each([0, -0])("should succeed when validation rules arguments are 0 (number)", (args) => {
const validator = new Validator();
validator.setRule("minimumValue", minimumValue);
const result: ValidationResult = validator.validate("age", 0, { minimumValue: args });
expect(result.isValid).toBe(true);
expect(result.rules.minimumValue.severity).toBe("information");
});

it.concurrent("should succeed when validation rules arguments are 0 (BigInt)", () => {
const validator = new Validator();
validator.setRule("minimumValue", minimumValue);
const result: ValidationResult = validator.validate("age", 0, { minimumValue: BigInt(0) });
expect(result.isValid).toBe(true);
expect(result.rules.minimumValue.severity).toBe("information");
});
});
3 changes: 2 additions & 1 deletion src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ class Validator {
}

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

Expand Down