Created
November 26, 2015 18:35
-
-
Save ryrych/8c379894300a6ce539e5 to your computer and use it in GitHub Desktop.
Before refactoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```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'); | |
}); | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```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