All files / mod/joi validate.ts

0% Statements 0/16
0% Branches 0/6
0% Functions 0/1
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                                                                                 
import { InvalidValidationError } from "@mod/errors/versed-errors";
 
const validate = (ctx, schema, target) => {
  // const { translate } = ctx;
  // const translation = await getTranslationSource(languageId, ctx);
  // const language = (translation && translation.joi_errors) || {};
  // const convertedObj = convertObjToTrueObj(null, language);
  try {
    const { error, value } = schema.validate(target, {
      errors: {
        escapeHtml: true,
      },
      // messages: convertedObj,
      abortEarly: false,
      stripUnknown: true,
    });
    if (error) {
      throw error;
    }
    return value;
  } catch (joiError) {
    const oriDetail = joiError.details;
    const details = [];
    if (oriDetail && oriDetail.length) {
      for (const detail of oriDetail) {
        const { message } = detail;
        const customMsg = message;
        // for (const key of path) {
        //   customMsg = customMsg.replace(key, translate(key));
        // }
        details.push(customMsg);
      }
    }
    throw new InvalidValidationError({
      errors: details,
    });
  }
};
 
export default validate;