lundi 9 janvier 2017

Ember-Data validation response with EmbeddedRecordsMixin

How should the error response from the server look when doing a POST to create new records or a PATCH to update existing records but the validation fails server-side. Using the default JsonApiSerializer and Adapter but combined with the DS.EmbeddedRecordsMixin, to include a hasMany relationship in the request.

Consider the following request:

{
    "data": {
        "type": "sales-invoices",
        "attributes": {
            "name": null,
            "net-price": 29,
            "vat-amount": 6.09,
            "gross-price": 35.09
        },
        "relationships": {
            "customer": {
                "data": {
                    "type": "companies",
                    "id": "131"
                }
            },
            "organization": {
                "data": {
                    "type": "organizations",
                    "id": "3"
                }
            }
        },
        "sales-invoice-lines": [
            {
                "data": {
                    "attributes": {
                        "name": "Basic Linux Hosting",
                        "sort": 0,
                        "unit-price": 9,
                        "quantity": 1,
                        "total": 9,
                        "net-price": 9,
                        "vat-amount": 1.89,
                        "gross-price": 10.89,
                        "created": null,
                        "changed": null
                    },
                    "relationships": {
                        "sales-invoice": {
                            "data": {
                                "type": "sales-invoices",
                                "id": null
                            }
                        },
                        "vat-rate": {
                            "data": {
                                "type": "vat-rates",
                                "id": "1"
                            }
                        }
                    },
                    "type": "sales-invoice-lines"
                }
            },
            {
                "data": {
                    "attributes": {
                        "name": "Extra .com domain",
                        "sort": 1,
                        "unit-price": 20,
                        "quantity": 1,
                        "total": 20,
                        "net-price": 20,
                        "vat-amount": 4.2,
                        "gross-price": 24.2,
                        "created": null,
                        "changed": null
                    },
                    "relationships": {
                        "sales-invoice": {
                            "data": {
                                "type": "sales-invoices",
                                "id": null
                            }
                        },
                        "vat-rate": {
                            "data": {
                                "type": "vat-rates",
                                "id": "1"
                            }
                        }
                    },
                    "type": "sales-invoice-lines"
                }
            }
        ]
    }
}

Ember data uses json pointers (json api spec and ietf RFC), so should an error-response from the server look like this:

{
  "errors": [
    {
      "detail": "This value should not be null.",
      "source": {
        "pointer": "/data/attributes/name"
      }
    },
    {
      "detail": "This value should not be 0",
      "source": {
        "pointer": "/data/sales-invoice-lines/0/data/attributes/sort"
      }
    }
  ]
}

Will Ember data assign the error to the correct attribute even when embedded in an EmbeddedRecordsMixin hash? Is the order respected of the json array? so in this case the first record in the sales-invoice-lines hash would have /data/sales-invoice-lines/0/data as a pointer, and the second /data/sales-invoice-lines/1/data




Aucun commentaire:

Enregistrer un commentaire