Instantly share code, notes, and snippets.

Embed
What would you like to do?
Before refactoring
```javascript
import { errorize } from '../../../utils/errorize';
import { module, test } from 'qunit';
module('Unit | Utility | errorize');
test('errorize', function(assert) {
var errors = {
responseJSON: {
errors:[
{
code:100,
title:'has already been taken',
detail:'Email has already been taken',
source:{
pointer:'/data/attributes/email',
},
},
{
code:100,
title:'is too short (minimum is 6 characters)',
detail:'Password is too short (minimum is 6 characters)',
source:{
pointer:'/data/attributes/password',
},
},
],
},
};
var expected = {
email: [
{message: 'Email has already been taken'},
],
password: [
{message: 'Password is too short (minimum is 6 characters)'},
],
};
assert.deepEqual(errorize(errors), expected, 'errorize should equal errors');
});
```
```javascript
import Ember from 'ember';
export function errorize(errors) {
var data = {};
errors = errors.responseJSON.errors;
errors.forEach((error)=> {
if (error.source) {
let field = error.source.pointer.split('/').pop().camelize();
if (Ember.isNone(data[field])) {
data[field] = [];
}
data[field].push({message: error.detail});
}
});
return data;
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment