Skip to content
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

Multiple errors per attribute are not supported #82

Open
tlconnor opened this issue Sep 29, 2020 · 1 comment
Open

Multiple errors per attribute are not supported #82

tlconnor opened this issue Sep 29, 2020 · 1 comment

Comments

@tlconnor
Copy link
Contributor

Spraypaint maps errors in a JSON:API response to model attributes by looking at the attribute metadata.

{
  "errors": [
    {
      "code": "bad_request",
      "status": "400",
      "title": "Request Error",
      "detail": "...",
      "meta": {
        "attribute": "name",
        "message": "..."
      },
      "source": {
        "pointer": "/data/attributes/name"
      }
    }  
  ]
}

Spraypaint models currently only support one error per attribute, so if a response includes multiple errors for a single attribute only the last one will be exposed in the errors object.

https://github.com/graphiti-api/spraypaint.js/blob/master/src/util/validation-error-builder.ts#L56

To allow for multiple errors per attribute we could do something like below, however it would be a breaking change:

diff --git a/src/util/validation-error-builder.ts b/src/util/validation-error-builder.ts
index 2e4fac5..24db9bd 100644
--- a/src/util/validation-error-builder.ts
+++ b/src/util/validation-error-builder.ts
@@ -53,14 +53,15 @@ export class ValidationErrorBuilder<T extends SpraypaintBase> {
     error: JsonapiError
   ) {
     let attribute = this.model.klass.deserializeKey(meta.attribute)
-    errorsAccumulator[attribute] = {
+    errorsAccumulator[attribute] = errorsAccumulator[attribute] || [];
+    errorsAccumulator[attribute].push({
       title: error.title as string,
       code: error.code as string,
       attribute: meta.attribute,
       message: meta.message,
       fullMessage: attribute === "base" ? meta.message : error.detail,
       rawPayload: error
-    }
+    })
   }
 
   private _processRelationship<R extends SpraypaintBase>(
@benlieb
Copy link

benlieb commented Jan 5, 2023

Happening to me as well. Why isn't this merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants