added number
This commit is contained in:
@@ -7,6 +7,8 @@ import {
|
|||||||
Validatable,
|
Validatable,
|
||||||
ArraySymbol,
|
ArraySymbol,
|
||||||
isString,
|
isString,
|
||||||
|
isNumber,
|
||||||
|
isInteger,
|
||||||
} from "./mod.ts";
|
} from "./mod.ts";
|
||||||
|
|
||||||
Deno.test("validate schema (match)", async () => {
|
Deno.test("validate schema (match)", async () => {
|
||||||
@@ -14,7 +16,7 @@ Deno.test("validate schema (match)", async () => {
|
|||||||
["string", isString],
|
["string", isString],
|
||||||
["string", [isString]],
|
["string", [isString]],
|
||||||
[["arr", "ay"], { [ArraySymbol]: 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) {
|
for (const [value, constraints] of values) {
|
||||||
assertEquals([], await validate(value, constraints));
|
assertEquals([], await validate(value, constraints));
|
||||||
@@ -26,7 +28,7 @@ Deno.test("validate schema (no match)", async () => {
|
|||||||
[6, isString],
|
[6, isString],
|
||||||
[false, [isString]],
|
[false, [isString]],
|
||||||
[["arr", ["ay"]], { [ArraySymbol]: 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) {
|
for (const [value, constraints] of values) {
|
||||||
assertNotEquals([], await validate(value, constraints));
|
assertNotEquals([], await validate(value, constraints));
|
||||||
|
|||||||
1
mod.ts
1
mod.ts
@@ -7,3 +7,4 @@ export {
|
|||||||
ArraySymbol,
|
ArraySymbol,
|
||||||
} from "./Validator.ts";
|
} from "./Validator.ts";
|
||||||
export * from "./validators/string.ts";
|
export * from "./validators/string.ts";
|
||||||
|
export * from "./validators/number.ts";
|
||||||
|
|||||||
69
validators/number.test.ts
Normal file
69
validators/number.test.ts
Normal file
@@ -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), []);
|
||||||
|
}
|
||||||
|
});
|
||||||
25
validators/number.ts
Normal file
25
validators/number.ts
Normal file
@@ -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.`;
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -13,7 +13,7 @@ Deno.test("isString (match)", async () => {
|
|||||||
new String("bar"),
|
new String("bar"),
|
||||||
];
|
];
|
||||||
for (const value of values) {
|
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(),
|
Symbol(),
|
||||||
];
|
];
|
||||||
for (const value of values) {
|
for (const value of values) {
|
||||||
assertNotEquals([], await validate(value, isString));
|
assertNotEquals(await validate(value, isString), []);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user