Hey there! I've been working on creating an interface in TypeScript to achieve the desired return as shown below:
{
"field": "departament_name",
"errors": [
"constraint": "O nome do departamento precisa ser do tipo String",
] },
Unfortunately, I'm facing an issue when trying to add the object to my array, resulting in the following error message:
Type 'string []' is not assignable to type 'constraints []'. Type 'string' is not assignable to type 'constraints'.ts (2322)
This is how my interfaces and function are structured:
export interface constraints {
constraint: string
}
export interface errorFormater {
field: string;
errors: constraints[]
}
And here's the function:
export const formatErrors = (validationErrors: ValidationError[]): errorFormater[] => {
let response: errorFormater[] = [];
for (let error of validationErrors) {
let field: string = error.property;
let constraints: string[] = [];
for (let constraint in error.constraints) {
if (!error.constraints.hasOwnProperty(constraint)) {
continue;
}
constraints.push(error.constraints[constraint]);
};
// console.log(property, errorMessage)
response.push({ field, errors: constraints });
}
return response;
}