-
I want to return detailed error messages from my custom validation rules. {
"errors": [
{
"instancePath": "",
"schemaPath": "#/required",
"keyword": "required",
"params": {
"missingProperty": "email"
},
"message": "must have required property 'email'",
"modelName": "MyModel",
"dataPath": ".email",
"requestPath": "body"
}
]
} Now I want to validate that the email is unique. Since I need to call the database I can't use AJV and let it build the error object (as far as I understand). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @ilyutov You can use this code example: import {nameOf} from "@tsed/core";
import {JsonParameterStore} from "@tsed/schema";
import {ValidationError} from "./ValidationError.js";
export class RequiredValidationError extends ValidationError {
public name: string = "REQUIRED_VALIDATION_ERROR";
static from(metadata: JsonParameterStore) {
const name = nameOf(metadata.paramType);
const expression = metadata.expression;
const type = name.toLowerCase().replace(/parse|params|filter/gi, "");
const message = `It should have required parameter '${expression}'`;
const errors = [
{
dataPath: "",
keyword: "required",
message,
modelName: type,
params: {
missingProperty: expression
},
schemaPath: "#/required"
}
];
return new RequiredValidationError(message, errors);
}
} Note: Ajv Support asynchronous validation. Look this section: https://ajv.js.org/guide/async-validation.html, Ts.ED await validate result, so it could be possible to explore this way! See you ;) |
Beta Was this translation helpful? Give feedback.
Hello @ilyutov
You can use this code example: