graphiti-api/spraypaint.js

Multiple errors per attribute are not supported

Opened this issue · 1 comments

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>(

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