swaf/src/db/Validator.ts

492 lines
15 KiB
TypeScript
Raw Normal View History

2020-04-22 15:52:17 +02:00
import {Connection} from "mysql";
import {ServerError} from "../HttpError.js";
import Model, {ModelType} from "./Model.js";
import ModelQuery, {WhereTest} from "./ModelQuery.js";
2020-04-22 15:52:17 +02:00
2020-09-06 10:21:47 +02:00
export const EMAIL_REGEX = /^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/;
export default class Validator<V> {
public static async validate(
validationMap: { [p: string]: Validator<unknown> },
body: { [p: string]: unknown },
): Promise<void> {
const bag = new ValidationBag();
for (const p of Object.keys(validationMap)) {
try {
await validationMap[p].execute(p, body[p], false);
} catch (e) {
if (e instanceof ValidationBag) {
bag.addBag(e);
} else throw e;
}
}
if (bag.hasMessages()) throw bag;
}
private readonly steps: ValidationStep<V>[] = [];
2020-04-22 15:52:17 +02:00
private readonly validationAttributes: string[] = [];
private readonly rawValueToHuman?: (val: V) => string;
2020-04-22 15:52:17 +02:00
private _min?: number;
private _max?: number;
public constructor(rawValueToHuman?: (val: V) => string) {
this.rawValueToHuman = rawValueToHuman;
}
2020-04-22 15:52:17 +02:00
/**
* @param thingName The name of the thing to validate.
* @param value The value to verify.
* @param onlyFormat {@code true} to only validate format properties, {@code false} otherwise.
* @param connection A connection to use in case of wrapped transactions.
*/
public async execute(
thingName: string,
value: V | undefined,
onlyFormat: boolean,
connection?: Connection,
): Promise<void> {
const bag = new ValidationBag<V>();
2020-04-22 15:52:17 +02:00
for (const step of this.steps) {
if (onlyFormat && !step.isFormat) continue;
let result;
try {
result = step.verifyStep(value, thingName, connection);
if (result instanceof Promise) {
result = await result;
}
2021-11-10 17:48:54 +01:00
} catch (e) {
if (e instanceof Error) {
throw new ServerError(`An error occurred while validating ${thingName} with value "${value}".`, e);
} else {
throw e;
}
}
if (result === false && step.throw) {
const error: ValidationError<V> = step.throw();
error.rawValueToHuman = this.rawValueToHuman;
2020-04-22 15:52:17 +02:00
error.thingName = thingName;
error.value = value;
bag.addMessage(error);
break;
2020-04-22 15:52:17 +02:00
} else if (step.interrupt !== undefined && step.interrupt(value)) {
break;
}
}
if (bag.hasMessages()) {
throw bag;
}
}
public defined(): Validator<V> {
2020-04-22 15:52:17 +02:00
this.validationAttributes.push('required');
this.addStep({
verifyStep: val => val !== undefined,
throw: () => new UndefinedValueValidationError(),
isFormat: true,
});
return this;
}
public acceptUndefined(alsoAcceptEmptyString: boolean = false): Validator<V> {
2020-04-22 15:52:17 +02:00
this.addStep({
verifyStep: () => true,
throw: null,
interrupt: val => {
return val === undefined ||
val === null ||
alsoAcceptEmptyString && typeof val === 'string' && val.length === 0;
},
2020-04-22 15:52:17 +02:00
isFormat: true,
});
return this;
}
public equals(other?: V): Validator<V> {
2020-04-22 15:52:17 +02:00
this.addStep({
verifyStep: val => val === other,
throw: () => new BadValueValidationError(other),
isFormat: true,
});
return this;
}
public sameAs(otherName?: string, other?: V): Validator<V> {
this.addStep({
verifyStep: val => val === other,
throw: () => new DifferentThanError(otherName),
isFormat: true,
});
return this;
}
public regexp(regexp: RegExp): Validator<V> {
2020-04-22 15:52:17 +02:00
this.validationAttributes.push(`pattern="${regexp}"`);
this.addStep({
verifyStep: val => regexp.test(<string><unknown>val),
throw: () => new InvalidFormatValidationError(),
isFormat: true,
});
return this;
}
public length(length: number): Validator<V> {
2020-04-22 15:52:17 +02:00
this.addStep({
verifyStep: val => isLenghtable(val) && val.length === length,
2020-04-22 15:52:17 +02:00
throw: () => new BadLengthValidationError(length),
isFormat: true,
});
return this;
}
/**
* @param minLength included
*/
public minLength(minLength: number): Validator<V> {
this.addStep({
verifyStep: val => isLenghtable(val) && val.length >= minLength,
throw: () => new TooShortError(minLength),
isFormat: true,
});
return this;
}
/**
* @param maxLength included
*/
public maxLength(maxLength: number): Validator<V> {
this.addStep({
verifyStep: val => isLenghtable(val) && val.length <= maxLength,
throw: () => new TooLongError(maxLength),
isFormat: true,
});
return this;
}
2020-04-22 15:52:17 +02:00
/**
* @param minLength included
* @param maxLength included
*/
public between(minLength: number, maxLength: number): Validator<V> {
2020-04-22 15:52:17 +02:00
this.addStep({
verifyStep: val => {
return isLenghtable(val) &&
val.length >= minLength && val.length <= maxLength;
2020-04-22 15:52:17 +02:00
},
throw: () => new BadLengthValidationError(minLength, maxLength),
isFormat: true,
});
return this;
}
/**
* @param min included
*/
public min(min: number): Validator<V> {
2020-04-22 15:52:17 +02:00
this.validationAttributes.push(`min="${min}"`);
this._min = min;
this.addStep({
verifyStep: val => {
return typeof val === 'number' && val >= min;
2020-04-22 15:52:17 +02:00
},
throw: () => new OutOfRangeValidationError(this._min, this._max),
isFormat: true,
});
return this;
}
/**
* @param max included
*/
public max(max: number): Validator<V> {
2020-04-22 15:52:17 +02:00
this.validationAttributes.push(`max="${max}"`);
this._max = max;
this.addStep({
verifyStep: val => {
return typeof val === 'number' && val <= max;
2020-04-22 15:52:17 +02:00
},
throw: () => new OutOfRangeValidationError(this._min, this._max),
isFormat: true,
});
return this;
}
public unique<M extends Model>(
model: M | ModelType<M>,
foreignKey?: string,
querySupplier?: () => ModelQuery<M>,
): Validator<V> {
2020-04-22 15:52:17 +02:00
this.addStep({
verifyStep: async (val, thingName, c) => {
if (!foreignKey) foreignKey = thingName;
let query: ModelQuery<M>;
2020-04-22 15:52:17 +02:00
if (querySupplier) {
query = querySupplier();
2020-04-22 15:52:17 +02:00
} else {
query = (model instanceof Model ? <ModelType<M>>model.constructor : model).select('');
2020-04-22 15:52:17 +02:00
}
query.where(foreignKey, val);
if (model instanceof Model && typeof model.id === 'number')
query = query.where('id', model.id, WhereTest.NE);
2020-04-22 15:52:17 +02:00
return (await query.execute(c)).results.length === 0;
},
throw: () => new AlreadyExistsValidationError(model.table),
2020-04-22 15:52:17 +02:00
isFormat: false,
});
return this;
}
public exists(modelType: ModelType<Model>, foreignKey?: string): Validator<V> {
2020-04-22 15:52:17 +02:00
this.addStep({
verifyStep: async (val, thingName, c) => {
const results = await modelType.select('')
.where(foreignKey !== undefined ? foreignKey : thingName, val)
.execute(c);
return results.results.length >= 1;
},
throw: () => new UnknownRelationValidationError(modelType.table, foreignKey),
2020-04-22 15:52:17 +02:00
isFormat: false,
});
return this;
}
private addStep(step: ValidationStep<V>) {
2020-04-22 15:52:17 +02:00
this.steps.push(step);
}
public getValidationAttributes(): string[] {
return this.validationAttributes;
}
public step(step: number): Validator<V> {
2020-04-22 15:52:17 +02:00
this.validationAttributes.push(`step="${step}"`);
return this;
}
}
interface ValidationStep<V> {
interrupt?: (val?: V) => boolean;
2020-04-22 15:52:17 +02:00
verifyStep(val: V | undefined, thingName: string, connection?: Connection): boolean | Promise<boolean>;
2020-04-22 15:52:17 +02:00
throw: ((val?: V) => ValidationError<V>) | null;
2020-04-22 15:52:17 +02:00
readonly isFormat: boolean;
}
export class ValidationBag<V> extends Error {
private readonly errors: ValidationError<V>[] = [];
2020-04-22 15:52:17 +02:00
public addMessage(err: ValidationError<V>): void {
if (!err.thingName) throw new Error('Null thing name');
this.errors.push(err);
}
2020-04-22 15:52:17 +02:00
public addBag(otherBag: ValidationBag<V>): void {
for (const error of otherBag.errors) {
this.errors.push(error);
}
2020-04-22 15:52:17 +02:00
}
public hasMessages(): boolean {
return this.errors.length > 0;
2020-04-22 15:52:17 +02:00
}
public getMessages(): { [p: string]: ValidationError<V> } {
const messages: { [p: string]: ValidationError<V> } = {};
for (const err of this.errors) {
messages[err.thingName || 'unknown'] = {
name: err.name,
message: err.message,
value: err.value,
};
}
return messages;
}
public getErrors(): ValidationError<V>[] {
return this.errors;
2020-04-22 15:52:17 +02:00
}
}
export class ValidationError<V> extends Error {
public rawValueToHuman?: (val: V) => string;
2020-04-22 15:52:17 +02:00
public thingName?: string;
public value?: V;
2020-04-22 15:52:17 +02:00
public get name(): string {
return this.constructor.name;
}
}
export class BadLengthValidationError<V> extends ValidationError<V> {
2020-04-22 15:52:17 +02:00
private readonly expectedLength: number;
private readonly maxLength?: number;
public constructor(expectedLength: number, maxLength?: number) {
2020-04-22 15:52:17 +02:00
super();
this.expectedLength = expectedLength;
this.maxLength = maxLength;
}
public get message(): string {
return `${this.thingName} expected length: ${this.expectedLength}${this.maxLength !== undefined ? ` to ${this.maxLength}` : ''}; ` +
`actual length: ${isLenghtable(this.value) && this.value.length}.`;
2020-04-22 15:52:17 +02:00
}
}
export class TooShortError<V> extends ValidationError<V> {
private readonly minLength: number;
public constructor(minLength: number) {
super();
this.minLength = minLength;
}
public get message(): string {
return `${this.thingName} must be at least ${this.minLength} characters.`;
}
}
export class TooLongError<V> extends ValidationError<V> {
private readonly maxLength: number;
public constructor(maxLength: number) {
super();
this.maxLength = maxLength;
}
public get message(): string {
return `${this.thingName} must be at most ${this.maxLength} characters.`;
}
}
export class BadValueValidationError<V> extends ValidationError<V> {
private readonly expectedValue: V;
2020-04-22 15:52:17 +02:00
public constructor(expectedValue: V) {
2020-04-22 15:52:17 +02:00
super();
this.expectedValue = expectedValue;
}
public get message(): string {
let expectedValue: string = String(this.expectedValue);
let actualValue: string = String(this.value);
if (this.rawValueToHuman && this.value) {
expectedValue = this.rawValueToHuman(this.expectedValue);
actualValue = this.rawValueToHuman(this.value);
}
return `Expected: ${expectedValue}; got: ${actualValue}.`;
2020-04-22 15:52:17 +02:00
}
}
export class DifferentThanError<V> extends ValidationError<V> {
private readonly otherName?: string;
public constructor(otherName?: string) {
super();
this.otherName = otherName;
}
public get message(): string {
return `This should be the same as ${this.otherName}.`;
}
}
export class OutOfRangeValidationError<V> extends ValidationError<V> {
2020-04-22 15:52:17 +02:00
private readonly min?: number;
private readonly max?: number;
public constructor(min?: number, max?: number) {
2020-04-22 15:52:17 +02:00
super();
this.min = min;
this.max = max;
}
public get message(): string {
if (this.min === undefined) {
return `${this.thingName} must be at most ${this.max}`;
} else if (this.max === undefined) {
return `${this.thingName} must be at least ${this.min}`;
}
let min: string = String(this.min);
let max: string = String(this.max);
if (this.rawValueToHuman) {
min = this.rawValueToHuman(this.min as unknown as V);
max = this.rawValueToHuman(this.max as unknown as V);
}
return `${this.thingName} must be between ${min} and ${max}.`;
2020-04-22 15:52:17 +02:00
}
}
export class InvalidFormatValidationError<V> extends ValidationError<V> {
2020-04-22 15:52:17 +02:00
public get message(): string {
return `"${this.value}" is not a valid ${this.thingName}.`;
}
}
export class UndefinedValueValidationError<V> extends ValidationError<V> {
2020-04-22 15:52:17 +02:00
public get message(): string {
return `${this.thingName} is required.`;
}
}
export class AlreadyExistsValidationError<V> extends ValidationError<V> {
2020-04-22 15:52:17 +02:00
private readonly table: string;
public constructor(table: string) {
2020-04-22 15:52:17 +02:00
super();
this.table = table;
}
public get message(): string {
return `${this.thingName} already exists in ${this.table}.`;
2020-04-22 15:52:17 +02:00
}
}
export class UnknownRelationValidationError<V> extends ValidationError<V> {
2020-04-22 15:52:17 +02:00
private readonly table: string;
private readonly foreignKey?: string;
public constructor(table: string, foreignKey?: string) {
2020-04-22 15:52:17 +02:00
super();
this.table = table;
this.foreignKey = foreignKey;
}
public get message(): string {
return `${this.thingName}=${this.value} relation was not found in ${this.table}${this.foreignKey !== undefined ? `.${this.foreignKey}` : ''}.`;
}
}
export class FileError<V> extends ValidationError<V> {
private readonly _message: string;
public constructor(message: string) {
super();
this._message = message;
}
public get message(): string {
return `${this._message}`;
}
}
export type Lengthable = {
length: number,
};
export function isLenghtable(value: unknown): value is Lengthable {
return value !== undefined && value !== null &&
typeof (value as Lengthable).length === 'number';
}