Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/jsonapi-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,27 @@ class JsonApiDataStore {
};
}

formatErrors(payload) {
return payload.errors.map(function(error) {
return {
status: error.status,
attribute: error.source.pointer.split('/')[3],
detail: error.detail
};
})
};

/**
* Sync a JSONAPI-compliant payload with the store.
* @method sync
* @param {object} data The JSONAPI payload
* @return {object} The model/array of models corresponding to the payload's primary resource(s).
*/
sync(payload) {
if (payload.errors) {
return { errors: this.formatErrors(payload) };
}

return this.syncWithMeta(payload).data;
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,28 @@ describe('JsonApiDataStore', () => {
expect(articles[1].related_article.id).to.eq(1337);
});
});

context('when given a payload with errors', () => {
var store = new JsonApiDataStore(),
payload = {
errors: [
{
status: 422,
source: {
pointer: '/data/attributes/title'
},
detail: 'is too short (minimum is 3 characters)'
}
]
};

it('should return error object', () => {
var articleErrors = store.sync(payload).errors;
expect(articleErrors[0].status).to.eq(422);
expect(articleErrors[0].attribute).to.eq('title');
expect(articleErrors[0].detail).to.eq('is too short (minimum is 3 characters)');
});
});
});

describe('.syncWithMeta()', () => {
Expand Down