This commit is contained in:
Sebastian Seedorf
2020-05-26 00:20:44 +02:00
parent cd6ed23d47
commit 218e596ede
5 changed files with 72 additions and 39 deletions

View File

@@ -1,24 +1,32 @@
export type Args = {[_: string]: any};
export type Args = { [_: string]: any };
export interface Validator {
type: string;
check: (value: any) => Promise<Args|undefined>|Args|undefined;
message: (value: any, args?: Args) => Promise<string|undefined>|string|undefined;
check: (value: any) => Promise<Args | undefined> | Args | undefined;
message: (
value: any,
args?: Args,
) => Promise<string | undefined> | string | undefined;
}
export interface ValidationError {
type: string;
param?: string[];
message?: string|null;
message?: string | null;
args?: Args;
}
export type Validatable = Schema|Validator|Validator[];
export type Validatable = Schema | Validator | Validator[];
export const ArraySymbol: unique symbol = Symbol("ArraySymbol");
export type Schema = {[key: string]: Validatable}|{[ArraySymbol]: Validatable};
export type Schema = { [key: string]: Validatable } | {
[ArraySymbol]: Validatable;
};
export async function validate(value: any, validators: Validatable): Promise<ValidationError[]|undefined> {
export async function validate(
value: any,
validators: Validatable,
): Promise<ValidationError[] | undefined> {
if (instanceofValidatorArray(validators) || instanceofValidator(validators)) {
return validateValue(value, validators);
} else {
@@ -34,10 +42,13 @@ function instanceofValidator(value: any): value is Validator {
}
function instanceofValidatorArray(value: any): value is Validator[] {
return Array.isArray(value) && value.every(x => instanceofValidator(x));
return Array.isArray(value) && value.every((x) => instanceofValidator(x));
}
async function validateValue(value: any, validators: Validator|Validator[]): Promise<ValidationError[]> {
async function validateValue(
value: any,
validators: Validator | Validator[],
): Promise<ValidationError[]> {
if (!Array.isArray(validators)) {
validators = [validators];
}
@@ -49,33 +60,40 @@ async function validateValue(value: any, validators: Validator|Validator[]): Pro
result.push({
type: validator.type,
args,
message
message,
});
}
}
return result;
}
async function validateSchema(value: any, validators: Schema): Promise<ValidationError[]> {
async function validateSchema(
value: any,
validators: Schema,
): Promise<ValidationError[]> {
if (validators.hasOwnProperty(ArraySymbol)) {
const v = validators as {[ArraySymbol]: Validatable};
const v = validators as { [ArraySymbol]: Validatable };
if (Array.isArray(value)) {
const arr = await Promise.all(value.map(val => validate(val, v[ArraySymbol])));
const errors = arr.flatMap((val, idx) => (val ?? []).map(error => ({
...error,
param: [`[${idx}]`, ...(error.param || [])]
})));
const arr = await Promise.all(
value.map((val) => validate(val, v[ArraySymbol])),
);
const errors = arr.flatMap((val, idx) =>
(val ?? []).map((error) => ({
...error,
param: [`[${idx}]`, ...(error.param || [])],
}))
);
return errors;
} else {
return [{
type: "array",
param: ["[]"],
message: "Array expected!",
args: {}
args: {},
}];
}
}
const v = validators as {[key: string]: Validatable};
const v = validators as { [key: string]: Validatable };
const valErrors: ValidationError[] = [];
for (const prop in validators) {
if (!validators.hasOwnProperty(prop)) {
@@ -89,9 +107,9 @@ async function validateSchema(value: any, validators: Schema): Promise<Validatio
type: "property",
param: [prop],
message: `Property '${prop}' expected but not found!`,
args: {property: prop}
args: { property: prop },
});
}
}
return valErrors;
}
}