Created the validateSchema method

This commit is contained in:
Mathias Wagner 2023-01-15 03:26:22 +01:00
parent b147d46d05
commit 66377f191a
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,4 +1,18 @@
import {Response} from "express";
import { Response } from 'express';
import { ObjectSchema } from 'joi';
const codeMappings: Record<string, number> = {
"any.required": 1, // TODO
"any": 0
};
export const sendError = (res: Response, httpCode: number, errorCode: number, message: string, fieldName?: string) =>
res.status(httpCode).json({code: errorCode, message, fieldName});
res.status(httpCode).json({ code: errorCode, message, fieldName });
export const validateSchema = (res: Response, schema: ObjectSchema, object: Record<string, any>) => {
const { error } = schema.validate(object, { errors: { wrap: { label: '' } } });
const errorCode: number = codeMappings[error?.details[0].type || 'any'] || 0;
const message: string = error?.details[0].message || 'No message provided';
sendError(res, 400, errorCode, message);
};