-
Notifications
You must be signed in to change notification settings - Fork 822
Validation Error Format #82
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
Labels
type: discussion
Issues discussing any topic.
Comments
something like this is what I was imagining. export function validationErrorToObject(ve: ValidationError[]): ValidationObject {
return ve.reduce((p, c:ValidationError) : ValidationObject => {
if(!c.children || !c.children.length) {
p[c.property] = {
value: c.value,
constraints: Object.keys(c.constraints)
.map(key=>{
return capitalize(c.constraints[key])+".\u00a0";
})
}
} else {
p[c.property] = validationErrorToObject(c.children);
}
return p;
}, {} as ValidationObject);
}
export interface ValidationObject {
[key: string]:
{value: string, constraints: string[] } | ValidationObject
}
export function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
} |
I support your suggestion. This might be ideal for angular usage. |
Just to be clear, so you want a structure like this: // class definition
class Post {
@IsString()
title: string;
@IsString()
body: string;
@ValidateNested()
author: User
} // validation result
{
title: [{ constraint: 'isString', message: 'title must be a string.'}],
body: [{ constraint: 'isString', message: 'body must be a string.'}],
author: {
name: [{ constraint: 'isString', message: 'name must be a string.'}],
age: [{ constraint: 'isInit', message: 'age must be an integer.'}],
}
} |
Much easier to handle! Please do not forget that messages need translation and also need to include constraint parameters. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am wanting to bind the validation error to my template with NG4, however given the current output of ValidationError[] I have to iterate to find the offender and then pull the message. I then have to bind the message to an object that is bound to the error for that field. I would be nice to get an object output instead of an array that could be bound to.
The text was updated successfully, but these errors were encountered: