I am using AJV library https://github.com/ajv-validator/ajv to validate the input of my nodejs express api. However, im having trouble extracting the property name in question for each error object in the returned array.
[{
instancePath: '',
schemaPath: '#/required',
keyword: 'required',
params: { missingProperty: 'start_date' },
message: "must have required property 'start_date'"
}
{
instancePath: '/top',
schemaPath: '#/properties/top/type',
keyword: 'type',
params: { type: 'number' },
message: 'must be number'
}]
As you can see from the above output extracting the property name (start_date, top
) for each is a little different, so im hoping there is an easy way to do that without having to parse depending on the error type (keyword).
Expectation
i expect to be able to create a error like below mapping the original array. To do that i need the message which is available in the above original output and the property name which is not available.
[
{ property: "start_date", message: "must have required property 'start_date"}
{ property: "top", message: "must be number" },
]
Code
export interface ILeaderboardQuery {
rank: string;
entity_types: string[];
country?: string | undefined;
region?: string | undefined;
start_date: string;
end_date: string;
top?: number | undefined;
}
export const LeaderboardQuerySchema: JSONSchemaType<ILeaderboardQuery> = {
type: "object",
properties: {
rank: { type: "string" },
entity_types: {
type: "array",
items: {
type: "string",
},
},
country: { type: "string", nullable: true },
region: { type: "string", nullable: true },
start_date: { type: "string" },
end_date: { type: "string" },
top: { type: "number", nullable: true },
},
required: ["rank", "start_date", "end_date"],
additionalProperties: false,
};
const ajv = new Ajv({ allErrors: true });
export const GetLeaderboardValidator = (req: Request, res: Response, next: NextFunction) => {
const validate = ajv.compile<ILeaderboardQuery>(LeaderboardQuerySchema);
for (const err of validate.errors as DefinedError[]) {
console.log(err);
}
};
ajv: ^8.6.2"