wag-managment-frontend/src/(apicalls)/validations/validationProcesser.ts

77 lines
1.6 KiB
TypeScript

import { array, boolean, number, object, string } from "zod";
interface ValidationInterface {
required: string[];
properties: Object;
title: string;
type: string;
}
interface HeadersAndValidationsInterface {
headers: Object;
validation: ValidationInterface;
language: string;
properties: Object;
}
class HeadersAndValidations {
headers: Object;
validation: ValidationInterface;
language: string;
properties: Object;
validated: any = {};
constructor({
headers,
validation,
language,
}: HeadersAndValidationsInterface) {
this.headers = headers;
this.validation = validation;
this.language = language;
this.properties = this.validation?.properties;
this.parseProcesser();
}
parseProcesser() {
Object.entries(this.properties).map(([key, value]) => {
const multipleTypes: Object[] = value?.anyOf || [];
const isRequired: boolean = Object.keys(value).includes("anyOf");
if (!isRequired) {
multipleTypes.map((row: any) => {
if (row.type !== "null") {
this.validated[key] = {
required: false,
fieldType: row.type,
};
}
});
} else {
this.validated[key] = {
required: true,
fieldType: value,
};
}
});
}
parseType({ type }: any) {
switch (type) {
case "string":
return string;
case "number":
return number;
case "boolean":
return boolean;
case "array":
return array;
case "object":
return object;
default:
return string;
}
}
}
export { HeadersAndValidations };