Added logical or
This commit is contained in:
@@ -26,7 +26,7 @@ export type Schema = { [key: string]: Validatable } | {
|
|||||||
export async function validate(
|
export async function validate(
|
||||||
value: any,
|
value: any,
|
||||||
validators: Validatable,
|
validators: Validatable,
|
||||||
): Promise<ValidationError[] | undefined> {
|
): Promise<ValidationError[]> {
|
||||||
if (instanceofValidatorArray(validators) || instanceofValidator(validators)) {
|
if (instanceofValidatorArray(validators) || instanceofValidator(validators)) {
|
||||||
return validateValue(value, validators);
|
return validateValue(value, validators);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
5
mod.ts
5
mod.ts
@@ -6,5 +6,6 @@ export {
|
|||||||
Validatable,
|
Validatable,
|
||||||
ArraySymbol,
|
ArraySymbol,
|
||||||
} from "./Validator.ts";
|
} from "./Validator.ts";
|
||||||
export * from "./validators/string.ts";
|
export { isString } from "./validators/string.ts";
|
||||||
export * from "./validators/number.ts";
|
export { isNumber, isInteger } from "./validators/number.ts";
|
||||||
|
export { or } from "./validators/logic.ts";
|
||||||
|
|||||||
27
validators/logic.test.ts
Normal file
27
validators/logic.test.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { or } from "./logic.ts";
|
||||||
|
import { validate, Validator, isString, isInteger } from "../mod.ts";
|
||||||
|
import {
|
||||||
|
assertEquals,
|
||||||
|
assertNotEquals,
|
||||||
|
} from "https://deno.land/std@0.53.0/testing/asserts.ts";
|
||||||
|
|
||||||
|
Deno.test("or (match)", async () => {
|
||||||
|
const values: [any, Validator[]][] = [
|
||||||
|
["", [isString, isInteger]],
|
||||||
|
[1, [isString, isInteger]],
|
||||||
|
];
|
||||||
|
for (const [value, validators] of values) {
|
||||||
|
assertEquals(await validate(value, or(...validators)), []);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("or (no match)", async () => {
|
||||||
|
const values: [any, Validator[]][] = [
|
||||||
|
[true, [isString, isInteger]],
|
||||||
|
[3.1415, [isString, isInteger]],
|
||||||
|
[1, [isString]],
|
||||||
|
];
|
||||||
|
for (const [value, validators] of values) {
|
||||||
|
assertNotEquals(await validate(value, or(...validators)), []);
|
||||||
|
}
|
||||||
|
});
|
||||||
18
validators/logic.ts
Normal file
18
validators/logic.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { Validator, Args, Validatable, validate } from "../mod.ts";
|
||||||
|
|
||||||
|
export function or(...funcs: Validatable[]): Validator {
|
||||||
|
return {
|
||||||
|
type: "or",
|
||||||
|
check: async (value: any) => {
|
||||||
|
const errors = await Promise.all(funcs.map((x) => validate(value, x)));
|
||||||
|
if (errors.every((x) => x.length > 0)) {
|
||||||
|
return {
|
||||||
|
errors,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
message: (value: any, args?: Args) => {
|
||||||
|
return "At least one of the rules has to be successful.";
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user