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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Validasaur is Deno validation library slightly inspired by Laravel Validation.
- [`minNumber(minValue: number)`](#minnumberminvalue-number)
- [`notIn(disallowedValues: PrimitiveTypes[])`](#notindisallowedvalues-primitivetypes)
- [`notNull`](#notnull)
- [`notNaN`](#notnan)
- [`nullable`](#nullable)
- [`numberBetween(minValue: number, maxValue: number)`](#numberbetweenminvalue-number-maxvalue-number)
- [`requiredIf(field: string, fieldValue: any)`](#requirediffield-string-fieldvalue-any)
Expand Down Expand Up @@ -462,6 +463,9 @@ Value under this field must be valid email address.

#### `isFloat`

> **Warning**
> This does not check for `NaN`. Please use `notNaN` as well.

Value under this field must be a float number.

* Invalid values: `"0.1"`, `[]`, `0`, `1`, `123`, etc.
Expand All @@ -487,6 +491,9 @@ const [ passes, errors ] = await validate({

#### `isInt`

> **Warning**
> This does not check for `NaN`. Please use `notNaN` as well.

Value under this field must be an integer.

* Invalid values: `0.5`, `"123"`, etc.
Expand All @@ -508,13 +515,19 @@ Value under this field must be valid IPv6.

#### `isNumber`

> **Warning**
> This does not check for `NaN`. Please use `notNaN` as well.

Value under this field must be a float or an integer.

* Invalid values: `"1"`, `"1.5"`, etc.
* Valid values: `1`, `1.5`, etc.

#### `isNumeric`

> **Warning**
> This does not check for `NaN`. Please use `notNaN` as well.

Same as `asNumber`, but it allows numeric string.

* Invalid values: `"1.0abc"`, `"x.1"`, etc.
Expand Down Expand Up @@ -587,6 +600,9 @@ const [ passes, errors ] = await validate({

#### `maxNumber(maxValue: number)`

> **Warning**
> This does not check for `NaN`. Please use `notNaN` as well.

Value under this field should be a number that is not higher than `maxValue`.

Example:
Expand Down Expand Up @@ -623,6 +639,9 @@ const [ passes, errors ] = await validate({

#### `minNumber(minValue: number)`

> **Warning**
> This does not check for `NaN`. Please use `notNaN` as well.

Value under this field should be a number that is not lower than `minValue`.

Example:
Expand Down Expand Up @@ -663,6 +682,11 @@ const [ passes, errors ] = await validate({

Value under this field must not be `null`.

#### `notNaN`

Value under this field must not be `NaN`.


#### `nullable`

In case you need a `required` field that accept `null` value,
Expand Down
1 change: 1 addition & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const defaultMessages: ValidationMessages = {
minNumber: ":attr cannot be lower than :minValue",
notIn: ":value is not allowed",
notNull: ":value cannot be null",
notNaN: ":attr cannot be NaN",
numberBetween: ":value must be between :minValue - :maxValue",
required: ":attr is required",
default: ":attr is invalid",
Expand Down
1 change: 1 addition & 0 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export * from "./rules/min_length.ts";
export * from "./rules/min_number.ts";
export * from "./rules/not_in.ts";
export * from "./rules/not_null.ts";
export * from "./rules/not_nan.ts";
export * from "./rules/nullable.ts";
export * from "./rules/number_between.ts";
export * from "./rules/match.ts";
Expand Down
6 changes: 6 additions & 0 deletions src/rules/not_nan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Validity } from "../types.ts";
import { invalid } from "../utils.ts";

export function notNaN(value: any): Validity {
return isNaN(value) ? invalid("notNaN", { value }, true) : undefined;
}
11 changes: 11 additions & 0 deletions tests/rules/not_nan.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { notNaN } from "../../src/rules/not_nan.ts";
import { invalid } from "../../src/utils.ts";
import { assertInvalid, assertValid } from "../utils.ts";

Deno.test("rules.notNaN(NaN) should be invalid", () => {
assertInvalid(notNaN(NaN), invalid("notNaN", { value: NaN }, true));
});

Deno.test("rules.notNaN(0) should be valid", () => {
assertValid(notNaN(0));
});