mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 14:37:13 +01:00
move CRUD operations to helpers
This commit is contained in:
39
helpers/validationError.ts
Normal file
39
helpers/validationError.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { VALIDATION_ERRORS } from '../db/enums'
|
||||
|
||||
interface 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(',')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user