Skip to content

Do not add key errors to the entity when there is no validation error. #71

@vitorgamer58

Description

@vitorgamer58

Is your feature request related to a problem? Please describe.
When I call the isValid function on my entity instance, it returns true or false, indicating whether or not my entity is valid.
Additionally, this function includes in my entity an errors key containing the errors that were detected in the validation, according to the rules defined in the entity.
However, even when there is no error, it still creates the errors key in the entity, containing an empty object.

This can lead to problems, for example if I pass the already validated entity to another usecase, or if I pass it to some function to insert into the database, which will insert the errors key containing an empty object.

Describe the solution you'd like
I believe we can make a conditional to only insert the errors key if there really is a validation error.
In the baseEntity.js file in the line that follows:

this.errors = errors

Describe alternatives you've considered
I have not considered alternatives to this feature.

Additional context
I noticed on two points:

  1. By adapting my project that uses herbs2mongo to save nested entities in the database.
  2. When using a usecase that passes the request it received (entity) to another usecase.

As a temporary fix, after verifying that the entity is valid, I deleted the errors field before calling the other usecase.

delete client.errors;

And in the database part, I changed my DataMapper instance to delete keys that I might configure when calling my deletekeys function, as already mentioned in this issue: herbsjs/herbs2mongo#35

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions