mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
40 lines
1002 B
TypeScript
40 lines
1002 B
TypeScript
import { VALIDATION_ERRORS } from '../db/enums'
|
|
|
|
type ValidationErrors = {
|
|
[key: string]: { properties: { message: string }; kind: string }
|
|
}
|
|
|
|
function getDefinedValidationMessage(kind: string) {
|
|
switch (kind) {
|
|
case VALIDATION_ERRORS.AT_LEAST_ONE_DAY_BOOKED:
|
|
return 'Mindestens ein angefragter Tag ist nicht mehr verfügbar'
|
|
default:
|
|
return null
|
|
}
|
|
}
|
|
|
|
export class ValidationError extends Error {
|
|
private errors: ValidationErrors
|
|
|
|
constructor(errors: ValidationErrors) {
|
|
super()
|
|
|
|
if (Error.captureStackTrace) {
|
|
Error.captureStackTrace(this, ValidationError)
|
|
}
|
|
this.name = this.constructor.name
|
|
this.errors = errors
|
|
}
|
|
|
|
get message() {
|
|
return Object.entries<{ properties: { message: string }; kind: string }>(
|
|
this.errors
|
|
)
|
|
.map(([_key, value]) => {
|
|
const validationMessage = getDefinedValidationMessage(value.kind)
|
|
return validationMessage || `${value?.properties?.message}`
|
|
})
|
|
.join(',')
|
|
}
|
|
}
|