Skip to content
Open
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
4 changes: 2 additions & 2 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ Watchers only fire on value changes, not on initial render. Multiple action bind
| `numeric` | Must be a number | — |
| `minLength` | Minimum string length | `{ min: number }` |
| `maxLength` | Maximum string length | `{ max: number }` |
| `min` | Minimum numeric value | `{ min: number }` |
| `max` | Maximum numeric value | `{ max: number }` |
| `min` | Minimum value for numbers and numeric strings | `{ min: number }` |
| `max` | Maximum value for numbers and numeric strings | `{ max: number }` |
| `pattern` | Must match regex | `{ pattern: string }` |
| `matches` | Must equal another field | `{ other: { $state: "/path" } }` |
| `equalTo` | Alias for matches | `{ other: { $state: "/path" } }` |
Expand Down
38 changes: 32 additions & 6 deletions packages/core/src/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,54 @@ describe("builtInValidationFunctions", () => {
});

describe("min", () => {
it("passes when number meets minimum", () => {
it("passes when a number or numeric string meets the minimum", () => {
expect(builtInValidationFunctions.min(5, { min: 3 })).toBe(true);
expect(builtInValidationFunctions.min(3, { min: 3 })).toBe(true);
expect(builtInValidationFunctions.min("5", { min: 3 })).toBe(true);
expect(builtInValidationFunctions.min("3", { min: 3 })).toBe(true);
});

it("fails when number is below minimum", () => {
it("fails when a number or numeric string is below the minimum", () => {
expect(builtInValidationFunctions.min(2, { min: 3 })).toBe(false);
expect(builtInValidationFunctions.min("2", { min: 3 })).toBe(false);
});

it("fails for non-numbers", () => {
expect(builtInValidationFunctions.min("5", { min: 3 })).toBe(false);
it.each(["", " ", "5px", null, false, NaN, Infinity])(
"fails for non-numeric value %j",
(value) => {
expect(builtInValidationFunctions.min(value, { min: 3 })).toBe(false);
},
);

it("fails when min is missing or not a number", () => {
expect(builtInValidationFunctions.min(5, { min: "3" })).toBe(false);
expect(builtInValidationFunctions.min(5, {})).toBe(false);
});
});

describe("max", () => {
it("passes when number meets maximum", () => {
it("passes when a number or numeric string meets the maximum", () => {
expect(builtInValidationFunctions.max(3, { max: 5 })).toBe(true);
expect(builtInValidationFunctions.max(5, { max: 5 })).toBe(true);
expect(builtInValidationFunctions.max("3", { max: 5 })).toBe(true);
expect(builtInValidationFunctions.max("5", { max: 5 })).toBe(true);
});

it("fails when number exceeds maximum", () => {
it("fails when a number or numeric string exceeds the maximum", () => {
expect(builtInValidationFunctions.max(6, { max: 5 })).toBe(false);
expect(builtInValidationFunctions.max("6", { max: 5 })).toBe(false);
});

it.each(["", " ", "5px", null, false, NaN, Infinity])(
"fails for non-numeric value %j",
(value) => {
expect(builtInValidationFunctions.max(value, { max: 5 })).toBe(false);
},
);

it("fails when max is missing or not a number", () => {
expect(builtInValidationFunctions.max(5, { max: "5" })).toBe(false);
expect(builtInValidationFunctions.max(5, {})).toBe(false);
});
});

Expand Down
19 changes: 15 additions & 4 deletions packages/core/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ const matchesImpl: ValidationFunction = (
return value === other;
};

const toFiniteNumber = (value: unknown): number | null => {
if (typeof value === "number") {
return Number.isFinite(value) ? value : null;
}
if (typeof value !== "string" || value.trim() === "") return null;
const number = Number(value);
return Number.isFinite(number) ? number : null;
};

/**
* Built-in validation functions
*/
Expand Down Expand Up @@ -132,20 +141,22 @@ export const builtInValidationFunctions: Record<string, ValidationFunction> = {
* Check minimum numeric value
*/
min: (value: unknown, args?: Record<string, unknown>) => {
if (typeof value !== "number") return false;
const number = toFiniteNumber(value);
if (number === null) return false;
const min = args?.min;
if (typeof min !== "number") return false;
return value >= min;
return number >= min;
},

/**
* Check maximum numeric value
*/
max: (value: unknown, args?: Record<string, unknown>) => {
if (typeof value !== "number") return false;
const number = toFiniteNumber(value);
if (number === null) return false;
const max = args?.max;
if (typeof max !== "number") return false;
return value <= max;
return number <= max;
},

/**
Expand Down