MCPcopy Create free account
hub / github.com/axe-api/axe-api / RobustValidator

Class RobustValidator

packages/axe-api/src/Validators/RobustValidator.ts:6–57  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import { validate, setLocales, LanguageType, ILocale } from "robust-validator";
5
6class RobustValidator implements IValidator {
7 constructor(supportedLanguages: string[]) {
8 import("robust-validator").then((pkg) => {
9 for (const language of supportedLanguages) {
10 const languagePack = pkg[language];
11
12 if (!languagePack) {
13 throw new Error(
14 `The language is not supported by robust-validator: ${language}`,
15 );
16 }
17
18 setLocales(languagePack as ILocale);
19 LogService.debug(
20 `Robust-validator language pack has been imported: ${language}`,
21 );
22 }
23 });
24 }
25
26 async validate(req: AxeRequest, model: IModelService, formData: any) {
27 // Getting the validation rules
28 const requestMethod: HttpMethods = req.method as unknown as HttpMethods;
29 const validationRules = model.instance.getValidationRules(requestMethod);
30
31 // Validating the data if there is any
32 if (validationRules) {
33 const result = await validate(formData, validationRules, {
34 language: req.currentLanguage.language as LanguageType,
35 });
36 if (result.isInvalid) {
37 const errors: Record<string, string[]> = {};
38
39 Object.keys(result.errors).forEach((field) => {
40 if (!errors[field]) {
41 errors[field] = [];
42 }
43
44 errors[field].push(
45 ...result.errors[field].map((item) => item.message),
46 );
47 });
48
49 return {
50 errors,
51 };
52 }
53 }
54
55 return null;
56 }
57}
58
59export default RobustValidator;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected