-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make error message more context descriptive by allowing custom messages #3211
Make error message more context descriptive by allowing custom messages #3211
Comments
Proposed SolutionThe default error message can be overridden using a type User record {
@constraint:String {
minLength: 1,
maxLength: 10,
message: "User name should not exceed 10 characters"
}
string userName;
@constraint:Int {
minValue: 18,
message: "User should be atleast 18 years old"
}
int age;
}; The new error formatAfter this improvement, the constraint:Error {
// message is constructed from the messages associated with the constraint annotations
string message;
// details will be a map where the key denotes the failed element path and the value denotes
// the message(s) associated with that particular path constraints
map<string|string[]> details;
// Cause is an optional field which has the default message. This will be only populated
// if there is at least one user-defined message
error cause?;
} Construction of error messageThe error message is constructed by concatenating the error messages associated with the annotation. The following will be considered while constructing this message :
ExamplesScenario 1 :@constraint:String {
minLength: 2,
maxLength: 6,
message: "Short name should be 2 to 6 characters long"
}
type ShortName string;
ShortName name = "j";
error => {
message: "Short name should be 2 to 6 characters long",
cause: {
message: "Validation failed for '$:minLength' constraint(s)."
},
details: {
"$" : "Short name should be 2 to 6 characters long"
}
} Scenario 2 :type Person record {
ShortName name;
@constraint:Int {
minValue: 18,
message: "Person should be at least 18 years old"
}
int age;
};
Person person = {name: "j", age: 20};
error => {
message: "Short name should be 2 to 6 characters long",
cause: {
message: "Validation failed for '$.name:minLength' constraint(s)."
},
details: {
"$.name": "Short name should be 2 to 6 characters long"
}
}
Person person = {name: "j", age: 10};
error => {
message: "Person should be at least 18 years old and Short name should be 2 to 6 characters long",
cause: {
message: "Validation failed for '$.age:minValue','$.name:minLength' constraint(s)."
},
details: {
"$.name": "Short name should be 2 to 6 characters long",
"$.age": "Person should be at least 18 years old"
}
} Scenario 3 :type User record {
@constraint:String {
minLength: 2,
maxLength: 6,
message: "User's name should be 2 to 6 characters long"
}
string name;
@constraint:Int {
minValue: 18
}
int age;
};
User user = {name: "j", age: 20};
error => {
message: "User's name should be 2 to 6 characters long",
cause: {
message: "Validation failed for '$.name:minLength' constraint(s)."
},
details: {
"$.name": "User's name should be 2 to 6 characters long"
}
}
User user = {name: "j", age: 10};
error => {
message: "User's name should be 2 to 6 characters long and Validation failed for '$.age:minValue' constraint(s).",
cause: {
message: "Validation failed for '$.age:minValue','$.name:minLength' constraint(s)."
},
details: {
"$.name": "User's name should be 2 to 6 characters long",
"$.age": "Validation failed for '$.age:minValue' constraint(s)."
}
} Scenario 4 :type Employee record {
@constraint:String {
minLength: 2,
maxLength: 6,
message: "Employee's name should be 2 to 6 characters long"
}
string name;
@constraint:Int {
minValue: 18
}
int age;
@constraint:Array {
maxLength: 2,
message: "Employee should have at most 2 employees"
}
Employee[] employees;
};
Employee employee = {
name: "john",
age: 20,
employees: [
{name: "joe", age: 20, employees: []},
{name: "rey", age: 25, employees: []},
{name: "jay", age: 30, employees: []}
]
};
error => {
message: "Employee should have at most 2 employees",
cause: {
message: "Validation failed for '$.employees:maxLength' constraint(s)."
},
details: {
"$.employees": "Employee should have at most 2 employees"
}
}
Employee employee = {
name: "john",
age: 20,
employees: [
{name: "joe", age: 15, employees: []},
{name: "rey", age: 25, employees: []}
]
};
error => {
message: "Validation failed for '$.employees[0].age:minValue' constraint(s).",
details: {
"$.employees[0].age": "Validation failed for 'minValue' constraint(s)."
}
}
Employee employee = {
name: "john",
age: 20,
employees: [
{name: "j", age: 20, employees: []},
{name: "rey", age: 25, employees: []}
]
};
error => {
message: "Validation failed for '$.employees[0].name:minLength' constraint(s)."
details: {
"$.employees[0].name": "Employee's name should be 2 to 6 characters long"
}
} Further improvementsThe error message can be improved further by allowing expression in them which will be populated at runtime. For example: type User record {
@constraint:String {
minLength: 1,
maxLength: 10,
message: "User name: ${value} should not exceed ${maxLength} characters"
}
string userName;
@constraint:Int {
minValue: 18,
message: "User with age ${value} is not allowed, user should be atleast ${minValue} years old"
}
int age;
}; |
@shafreenAnfar and I had a discussion on the above proposal and decided on the following changes. In this issue, we are trying to answer this question:
|
Description:
Constraint
module should support custom messages when the validation fails. This can give more descriptive message wrt to the context.Describe your problem(s)
With the current implementation, the following is return as an error message when constraint validation fails.
Request 1 :
curl http://localhost:9090/users -d '{"userName": "tharmi", "age": 2}' -H "content-type: application/json"
Error Message :
payload validation failed: Validation failed for '$.age:minValue' constraint(s).
Request 2 :
curl http://localhost:9090/users -d '{"userName": "tharmigankrish", "age": 20}' -H "content-type: application/json"
Error Message :
payload validation failed: Validation failed for '$.userName:maxLength' constraint(s).
Describe your solution(s)
Having a
message
field in theconstraint
annotation to support customized error messagesExample :
Request 1 :
curl http://localhost:9090/users -d '{"userName": "tharmi", "age": 2}' -H "content-type: application/json"
New Error Message :
payload validation failed: User should be atleast 18 years old
Request 2 :
curl http://localhost:9090/users -d '{"userName": "tharmigankrish", "age": 20}' -H "content-type: application/json"
New Error Message :
payload validation failed: User name should not exceed 10 characters
The text was updated successfully, but these errors were encountered: