Skip to content

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

Open
jerradpatch opened this issue May 19, 2017 · 4 comments
Open

Validation Error Format #82

jerradpatch opened this issue May 19, 2017 · 4 comments
Labels
type: discussion Issues discussing any topic.

Comments

@jerradpatch
Copy link
Contributor

jerradpatch commented May 19, 2017

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.

validationErrors: {
   <fieldName>: [{constraint: <constraintName>, message: <actualMessage>}],
   <anotherFieldName: [{...},{...}],
    ...
}
Where in the template I could bind to this result with angular4
<div *ngFor="let err of resultModel.email">
    {{err.constraint}}{{err.email}}
</div>
<input type="email" [(ngModel)]="model.email">
@jerradpatch
Copy link
Contributor Author

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);
}

@st-clair-clarke
Copy link

I support your suggestion. This might be ideal for angular usage.

@NoNameProvided NoNameProvided added the type: discussion Issues discussing any topic. label Jan 10, 2018
@NoNameProvided
Copy link
Member

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.'}],
  } 
}

@HonoluluHenk
Copy link
Contributor

Much easier to handle!

Please do not forget that messages need translation and also need to include constraint parameters.
Maybe see #238 for a possible solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: discussion Issues discussing any topic.
Development

No branches or pull requests

4 participants