Added conversion
This commit is contained in:
@@ -5,6 +5,13 @@ export function isNumber(
|
||||
): Validator {
|
||||
return {
|
||||
type: "isNumber",
|
||||
convert: (value: any) => {
|
||||
if (typeof value === "string" || value instanceof String) {
|
||||
const num = Number(value);
|
||||
return Number.isNaN(num) ? value : num;
|
||||
}
|
||||
return value;
|
||||
},
|
||||
check: (value: any) => {
|
||||
if (value === null || value === undefined) return;
|
||||
if (allowNaN && Number.isNaN(value)) return;
|
||||
|
||||
@@ -129,10 +129,14 @@ Deno.test("fulfillsRegex (match)", async () => {
|
||||
[undefined, /[a-z]+/],
|
||||
["123abc", /[a-z]+/],
|
||||
["abc", /^[a-z]+$/],
|
||||
["^ab$", /^\^(ab)|(cd)\$$/]
|
||||
["^ab$", /^\^(ab)|(cd)\$$/],
|
||||
];
|
||||
for (const [value, regex] of values) {
|
||||
assertEquals(await validate(value, fulfillsRegex({ regex })), [], `${String(value)} - ${regex}`);
|
||||
assertEquals(
|
||||
await validate(value, fulfillsRegex({ regex })),
|
||||
[],
|
||||
`${String(value)} - ${regex}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -141,9 +145,13 @@ Deno.test("fulfillsRegex (no match)", async () => {
|
||||
[Symbol(), /[a-z]+/],
|
||||
["", /[a-z]+/],
|
||||
["abc123", /^[a-z]+$/],
|
||||
["^abcd$", /^\^(ab|cd)\$$/]
|
||||
["^abcd$", /^\^(ab|cd)\$$/],
|
||||
];
|
||||
for (const [value, regex] of values) {
|
||||
assertNotEquals(await validate(value, fulfillsRegex({ regex })), [], `${String(value)} - ${regex}`);
|
||||
assertNotEquals(
|
||||
await validate(value, fulfillsRegex({ regex })),
|
||||
[],
|
||||
`${String(value)} - ${regex}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -126,6 +126,15 @@ export function isEmail(): Validator {
|
||||
return {
|
||||
type: "isEmail",
|
||||
extends: [isString()],
|
||||
convert: (value: any) => {
|
||||
if (value === true) {
|
||||
return "true";
|
||||
} else if (value === false) {
|
||||
return "false";
|
||||
} else if (typeof value === "number") {
|
||||
return value.toString();
|
||||
}
|
||||
},
|
||||
check: (value: any) => {
|
||||
if (value === null || value === undefined) return;
|
||||
const regex =
|
||||
@@ -141,7 +150,7 @@ export function isEmail(): Validator {
|
||||
};
|
||||
}
|
||||
|
||||
export function fulfillsRegex({regex}: {regex: RegExp}): Validator {
|
||||
export function fulfillsRegex({ regex }: { regex: RegExp }): Validator {
|
||||
return {
|
||||
type: "fulfillsRegex",
|
||||
extends: [isString()],
|
||||
|
||||
Reference in New Issue
Block a user