diff --git a/Validator.test.ts b/Validator.test.ts index 2ca3f69..c962f85 100644 --- a/Validator.test.ts +++ b/Validator.test.ts @@ -7,6 +7,8 @@ import { Validatable, ArraySymbol, isString, + isNumber, + isInteger, } from "./mod.ts"; Deno.test("validate schema (match)", async () => { @@ -14,7 +16,7 @@ Deno.test("validate schema (match)", async () => { ["string", isString], ["string", [isString]], [["arr", "ay"], { [ArraySymbol]: isString }], - [{ foo: "bar", lorem: "ipsum" }, { foo: isString, lorem: [isString] }], + [{ foo: 3.1415, lorem: "ipsum" }, { foo: isNumber, lorem: [isString] }], ]; for (const [value, constraints] of values) { assertEquals([], await validate(value, constraints)); @@ -26,7 +28,7 @@ Deno.test("validate schema (no match)", async () => { [6, isString], [false, [isString]], [["arr", ["ay"]], { [ArraySymbol]: isString }], - [{ foo: {}, lorem: "ipsum" }, { foo: isString, lorem: [isString] }], + [{ foo: 3.1415, lorem: "ipsum" }, { foo: isInteger, lorem: [isString] }], ]; for (const [value, constraints] of values) { assertNotEquals([], await validate(value, constraints)); diff --git a/mod.ts b/mod.ts index a11d6b1..3bcac77 100644 --- a/mod.ts +++ b/mod.ts @@ -7,3 +7,4 @@ export { ArraySymbol, } from "./Validator.ts"; export * from "./validators/string.ts"; +export * from "./validators/number.ts"; diff --git a/validators/number.test.ts b/validators/number.test.ts new file mode 100644 index 0000000..e5ba370 --- /dev/null +++ b/validators/number.test.ts @@ -0,0 +1,69 @@ +import { isNumber, isInteger } from "./number.ts"; +import { validate } from "../mod.ts"; +import { + assertEquals, + assertNotEquals, +} from "https://deno.land/std@0.53.0/testing/asserts.ts"; + +Deno.test("isNumber (match)", async () => { + const values = [ + 0, + 1, + 2e64, + -1, + 0.1, + Math.PI, + ]; + for (const value of values) { + assertEquals(await validate(value, isNumber), []); + } +}); + +Deno.test("isNumber (no match)", async () => { + const values = [ + undefined, + null, + NaN, + "0", + true, + false, + () => {}, + function named() {}, + new Object(), + Symbol(), + ]; + for (const value of values) { + assertNotEquals(await validate(value, isNumber), []); + } +}); + +Deno.test("isInteger (match)", async () => { + const values = [ + 0, + 1, + 2e64, + -1, + ]; + for (const value of values) { + assertEquals(await validate(value, isInteger), []); + } +}); + +Deno.test("isInteger (no match)", async () => { + const values = [ + undefined, + null, + NaN, + "0", + true, + false, + () => {}, + function named() {}, + new Object(), + Symbol(), + 0.1, + ]; + for (const value of values) { + assertNotEquals(await validate(value, isInteger), []); + } +}); diff --git a/validators/number.ts b/validators/number.ts new file mode 100644 index 0000000..9fd57e8 --- /dev/null +++ b/validators/number.ts @@ -0,0 +1,25 @@ +import { Validator, Args } from "../mod.ts"; + +export const isNumber: Validator = { + type: "isNumber", + check: (value: any) => { + if (!Number.isFinite(value)) { + return {}; + } + }, + message: (value: any, args?: Args) => { + return `The value '${value && value.toString()}' has to be a number.`; + }, +}; + +export const isInteger: Validator = { + type: "isInteger", + check: (value: any) => { + if (!Number.isInteger(value)) { + return {}; + } + }, + message: (value: any, args?: Args) => { + return `The value '${value && value.toString()}' has to be an integer.`; + }, +}; diff --git a/validators/string.test.ts b/validators/string.test.ts index 79c8d3d..0776460 100644 --- a/validators/string.test.ts +++ b/validators/string.test.ts @@ -13,7 +13,7 @@ Deno.test("isString (match)", async () => { new String("bar"), ]; for (const value of values) { - assertEquals([], await validate(value, isString)); + assertEquals(await validate(value, isString), []); } }); @@ -31,6 +31,6 @@ Deno.test("isString (no match)", async () => { Symbol(), ]; for (const value of values) { - assertNotEquals([], await validate(value, isString)); + assertNotEquals(await validate(value, isString), []); } });