2020-04-22 15:52:17 +02:00
|
|
|
import {Connection} from "mysql";
|
2021-05-03 19:29:22 +02:00
|
|
|
|
|
|
|
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])?)+$/;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export default class Validator<V> {
|
2020-11-11 18:37:39 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
private readonly steps: ValidationStep<V>[] = [];
|
2020-04-22 15:52:17 +02:00
|
|
|
private readonly validationAttributes: string[] = [];
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
private readonly rawValueToHuman?: (val: V) => string;
|
2020-04-22 15:52:17 +02:00
|
|
|
private _min?: number;
|
|
|
|
private _max?: number;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public constructor(rawValueToHuman?: (val: V) => string) {
|
2020-05-04 22:06:20 +02:00
|
|
|
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.
|
|
|
|
*/
|
2020-09-25 23:42:15 +02:00
|
|
|
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;
|
|
|
|
|
2020-08-26 14:56:34 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-08-26 14:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result === false && step.throw) {
|
2020-09-25 23:42:15 +02:00
|
|
|
const error: ValidationError<V> = step.throw();
|
2020-05-04 22:06:20 +02:00
|
|
|
error.rawValueToHuman = this.rawValueToHuman;
|
2020-04-22 15:52:17 +02:00
|
|
|
error.thingName = thingName;
|
|
|
|
error.value = value;
|
|
|
|
bag.addMessage(error);
|
2020-08-26 15:02:09 +02:00
|
|
|
break;
|
2020-04-22 15:52:17 +02:00
|
|
|
} else if (step.interrupt !== undefined && step.interrupt(value)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bag.hasMessages()) {
|
|
|
|
throw bag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public acceptUndefined(alsoAcceptEmptyString: boolean = false): Validator<V> {
|
2020-04-22 15:52:17 +02:00
|
|
|
this.addStep({
|
|
|
|
verifyStep: () => true,
|
|
|
|
throw: null,
|
2020-09-25 23:42:15 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public sameAs(otherName?: string, other?: V): Validator<V> {
|
2020-04-25 16:08:53 +02:00
|
|
|
this.addStep({
|
|
|
|
verifyStep: val => val === other,
|
|
|
|
throw: () => new DifferentThanError(otherName),
|
|
|
|
isFormat: true,
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public length(length: number): Validator<V> {
|
2020-04-22 15:52:17 +02:00
|
|
|
this.addStep({
|
2020-09-25 23:42:15 +02:00
|
|
|
verifyStep: val => isLenghtable(val) && val.length === length,
|
2020-04-22 15:52:17 +02:00
|
|
|
throw: () => new BadLengthValidationError(length),
|
|
|
|
isFormat: true,
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-04-25 09:33:15 +02:00
|
|
|
/**
|
|
|
|
* @param minLength included
|
|
|
|
*/
|
2020-09-25 23:42:15 +02:00
|
|
|
public minLength(minLength: number): Validator<V> {
|
2020-04-25 09:33:15 +02:00
|
|
|
this.addStep({
|
2020-09-25 23:42:15 +02:00
|
|
|
verifyStep: val => isLenghtable(val) && val.length >= minLength,
|
2020-04-25 16:08:53 +02:00
|
|
|
throw: () => new TooShortError(minLength),
|
2020-04-25 09:33:15 +02:00
|
|
|
isFormat: true,
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param maxLength included
|
|
|
|
*/
|
2020-09-25 23:42:15 +02:00
|
|
|
public maxLength(maxLength: number): Validator<V> {
|
2020-04-25 09:33:15 +02:00
|
|
|
this.addStep({
|
2020-09-25 23:42:15 +02:00
|
|
|
verifyStep: val => isLenghtable(val) && val.length <= maxLength,
|
2020-04-25 16:08:53 +02:00
|
|
|
throw: () => new TooLongError(maxLength),
|
2020-04-25 09:33:15 +02:00
|
|
|
isFormat: true,
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-04-22 15:52:17 +02:00
|
|
|
/**
|
|
|
|
* @param minLength included
|
|
|
|
* @param maxLength included
|
|
|
|
*/
|
2020-09-25 23:42:15 +02:00
|
|
|
public between(minLength: number, maxLength: number): Validator<V> {
|
2020-04-22 15:52:17 +02:00
|
|
|
this.addStep({
|
|
|
|
verifyStep: val => {
|
2020-09-25 23:42:15 +02:00
|
|
|
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
|
|
|
|
*/
|
2020-09-25 23:42:15 +02:00
|
|
|
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 => {
|
2020-09-25 23:42:15 +02:00
|
|
|
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
|
|
|
|
*/
|
2020-09-25 23:42:15 +02:00
|
|
|
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 => {
|
2020-09-25 23:42:15 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
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) => {
|
2020-04-25 16:08:53 +02:00
|
|
|
if (!foreignKey) foreignKey = thingName;
|
2020-06-27 14:36:50 +02:00
|
|
|
let query: ModelQuery<M>;
|
2020-04-22 15:52:17 +02:00
|
|
|
if (querySupplier) {
|
2020-09-25 23:42:15 +02:00
|
|
|
query = querySupplier();
|
2020-04-22 15:52:17 +02:00
|
|
|
} else {
|
2020-09-25 23:42:15 +02:00
|
|
|
query = (model instanceof Model ? <ModelType<M>>model.constructor : model).select('');
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
2020-09-25 23:42:15 +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;
|
|
|
|
},
|
2020-09-25 23:42:15 +02:00
|
|
|
throw: () => new AlreadyExistsValidationError(model.table),
|
2020-04-22 15:52:17 +02:00
|
|
|
isFormat: false,
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public exists(modelType: ModelType<Model>, foreignKey?: string): Validator<V> {
|
2020-04-22 15:52:17 +02:00
|
|
|
this.addStep({
|
2020-09-25 23:42:15 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
private addStep(step: ValidationStep<V>) {
|
2020-04-22 15:52:17 +02:00
|
|
|
this.steps.push(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getValidationAttributes(): string[] {
|
|
|
|
return this.validationAttributes;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public step(step: number): Validator<V> {
|
2020-04-22 15:52:17 +02:00
|
|
|
this.validationAttributes.push(`step="${step}"`);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
interface ValidationStep<V> {
|
|
|
|
interrupt?: (val?: V) => boolean;
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
verifyStep(val: V | undefined, thingName: string, connection?: Connection): boolean | Promise<boolean>;
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
throw: ((val?: V) => ValidationError<V>) | null;
|
2020-04-22 15:52:17 +02:00
|
|
|
|
|
|
|
readonly isFormat: boolean;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export class ValidationBag<V> extends Error {
|
|
|
|
private readonly errors: ValidationError<V>[] = [];
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public addMessage(err: ValidationError<V>): void {
|
2020-04-25 16:08:53 +02:00
|
|
|
if (!err.thingName) throw new Error('Null thing name');
|
|
|
|
this.errors.push(err);
|
|
|
|
}
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public addBag(otherBag: ValidationBag<V>): void {
|
2020-04-25 16:08:53 +02:00
|
|
|
for (const error of otherBag.errors) {
|
|
|
|
this.errors.push(error);
|
|
|
|
}
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public hasMessages(): boolean {
|
2020-04-25 16:08:53 +02:00
|
|
|
return this.errors.length > 0;
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public getMessages(): { [p: string]: ValidationError<V> } {
|
|
|
|
const messages: { [p: string]: ValidationError<V> } = {};
|
2020-04-25 16:08:53 +02:00
|
|
|
for (const err of this.errors) {
|
2020-09-25 23:42:15 +02:00
|
|
|
messages[err.thingName || 'unknown'] = {
|
2020-04-25 16:08:53 +02:00
|
|
|
name: err.name,
|
|
|
|
message: err.message,
|
|
|
|
value: err.value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return messages;
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public getErrors(): ValidationError<V>[] {
|
2020-04-25 16:08:53 +02:00
|
|
|
return this.errors;
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 16:24:00 +01:00
|
|
|
export class ValidationError<V> extends Error {
|
2020-09-25 23:42:15 +02:00
|
|
|
public rawValueToHuman?: (val: V) => string;
|
2020-04-22 15:52:17 +02:00
|
|
|
public thingName?: string;
|
2020-09-25 23:42:15 +02:00
|
|
|
public value?: V;
|
2020-04-22 15:52:17 +02:00
|
|
|
|
|
|
|
public get name(): string {
|
|
|
|
return this.constructor.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export class BadLengthValidationError<V> extends ValidationError<V> {
|
2020-04-22 15:52:17 +02:00
|
|
|
private readonly expectedLength: number;
|
|
|
|
private readonly maxLength?: number;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
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}` : ''}; ` +
|
2020-09-25 23:42:15 +02:00
|
|
|
`actual length: ${isLenghtable(this.value) && this.value.length}.`;
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export class TooShortError<V> extends ValidationError<V> {
|
2020-04-25 16:08:53 +02:00
|
|
|
private readonly minLength: number;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public constructor(minLength: number) {
|
2020-04-25 16:08:53 +02:00
|
|
|
super();
|
|
|
|
this.minLength = minLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get message(): string {
|
|
|
|
return `${this.thingName} must be at least ${this.minLength} characters.`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export class TooLongError<V> extends ValidationError<V> {
|
2020-04-25 16:08:53 +02:00
|
|
|
private readonly maxLength: number;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public constructor(maxLength: number) {
|
2020-04-25 16:08:53 +02:00
|
|
|
super();
|
|
|
|
this.maxLength = maxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get message(): string {
|
|
|
|
return `${this.thingName} must be at most ${this.maxLength} characters.`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export class BadValueValidationError<V> extends ValidationError<V> {
|
|
|
|
private readonly expectedValue: V;
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public constructor(expectedValue: V) {
|
2020-04-22 15:52:17 +02:00
|
|
|
super();
|
|
|
|
this.expectedValue = expectedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get message(): string {
|
2020-09-25 23:42:15 +02:00
|
|
|
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);
|
2020-05-04 22:06:20 +02:00
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
return `Expected: ${expectedValue}; got: ${actualValue}.`;
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export class DifferentThanError<V> extends ValidationError<V> {
|
|
|
|
private readonly otherName?: string;
|
2020-04-25 16:08:53 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public constructor(otherName?: string) {
|
2020-04-25 16:08:53 +02:00
|
|
|
super();
|
|
|
|
this.otherName = otherName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get message(): string {
|
2020-09-25 23:42:15 +02:00
|
|
|
return `This should be the same as ${this.otherName}.`;
|
2020-04-25 16:08:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export class OutOfRangeValidationError<V> extends ValidationError<V> {
|
2020-04-22 15:52:17 +02:00
|
|
|
private readonly min?: number;
|
|
|
|
private readonly max?: number;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
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}`;
|
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
let min: string = String(this.min);
|
|
|
|
let max: string = String(this.max);
|
2020-05-04 22:06:20 +02:00
|
|
|
if (this.rawValueToHuman) {
|
2020-09-25 23:42:15 +02:00
|
|
|
min = this.rawValueToHuman(this.min as unknown as V);
|
|
|
|
max = this.rawValueToHuman(this.max as unknown as V);
|
2020-05-04 22:06:20 +02:00
|
|
|
}
|
|
|
|
return `${this.thingName} must be between ${min} and ${max}.`;
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +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}.`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export class UndefinedValueValidationError<V> extends ValidationError<V> {
|
2020-04-22 15:52:17 +02:00
|
|
|
public get message(): string {
|
|
|
|
return `${this.thingName} is required.`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export class AlreadyExistsValidationError<V> extends ValidationError<V> {
|
2020-04-22 15:52:17 +02:00
|
|
|
private readonly table: string;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public constructor(table: string) {
|
2020-04-22 15:52:17 +02:00
|
|
|
super();
|
|
|
|
this.table = table;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get message(): string {
|
2020-04-25 16:08:53 +02:00
|
|
|
return `${this.thingName} already exists in ${this.table}.`;
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +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;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
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}` : ''}.`;
|
|
|
|
}
|
2020-06-14 16:26:36 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
export class FileError<V> extends ValidationError<V> {
|
|
|
|
private readonly _message: string;
|
2020-06-14 16:26:36 +02:00
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public constructor(message: string) {
|
2020-06-14 16:26:36 +02:00
|
|
|
super();
|
2020-09-25 23:42:15 +02:00
|
|
|
this._message = message;
|
2020-06-14 16:26:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get message(): string {
|
2020-09-25 23:42:15 +02:00
|
|
|
return `${this._message}`;
|
2020-06-14 16:26:36 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
|
|
|
|
export type Lengthable = {
|
|
|
|
length: number,
|
|
|
|
};
|
|
|
|
|
|
|
|
export function isLenghtable(value: unknown): value is Lengthable {
|
|
|
|
return value !== undefined && value !== null &&
|
|
|
|
typeof (value as Lengthable).length === 'number';
|
|
|
|
}
|