Validations
Validation errors are handled automatically for any models adhering to the ActiveModel::Validations API.
After we’ve run the persistence logic - but before we close the
transaction - we check model.errors
. If errors are present anywhere in
the graph, we rollback the transaction and return a JSONAPI-compliant Error response:
[
{
code: "unprocessable_entity",
detail: "Name can't be blank",
meta: {
attribute: "name",
message: "can't be blank"
},
source: {
pointer: "/data/attributes/name"
},
status: "422",
title: "Validation Error"
}
]
This is true for nested write operations as well. Let’s say we were
saving an Employee
and their Position
s in a single request, but one
of the positions had a validation error on a missing title
:
[
{
code: 'unprocessable_entity',
status: '422',
title: 'Validation Error',
detail: "Title can't be blank",
source: { pointer: '/data/attributes/title' },
meta: {
relationship: {
attribute: :title,
message: "can't be blank",
code: :blank,
name: :positions,
id: '123',
type: 'positions'
}
}
}
]
This is enough information for a client to apply errors to the relevant objects. In JSORM’s case, you’d see:
let success = await employee.save({ with: "positions" })
console.log(employee.errors) // # {}
let position = employee.positions[0]
console.log(position.errors.title.message) // # "Can't be blank"