Skip to content

Commit

Permalink
[Bug Fix] Ember.String.htmlSafe throws exception (#203)
Browse files Browse the repository at this point in the history
* Failing test for SafeString.

* Unwrap string in case of htmlbars string
  • Loading branch information
offirgolan committed Jun 3, 2016
1 parent 64a361c commit ab0f10e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
10 changes: 10 additions & 0 deletions addon/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/

import Ember from 'ember';

export function hasEmberData() {
return typeof self.DS !== 'undefined';
}

export function unwrapString(input) {
if (input && input instanceof Ember.Handlebars.SafeString) {
return input.toString();
}

return input;
}
15 changes: 8 additions & 7 deletions addon/validators/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
*/

import Ember from 'ember';
import Messages from './messages';
import Messages from 'ember-cp-validations/validators/messages';
import getOwner from 'ember-getowner-polyfill';
import { unwrapString } from 'ember-cp-validations/utils/utils';

const {
get,
Expand Down Expand Up @@ -212,15 +213,15 @@ const Base = Ember.Object.extend({
*/
createErrorMessage(type, value, options = {}) {
const messages = this.get('errorMessages');
let message;
let message = unwrapString(options.message);

options.description = messages.getDescriptionFor(get(this, 'attribute'), options);

if (options.message) {
if (typeof options.message === 'string') {
message = messages.formatMessage(options.message, options);
} else if (typeof options.message === 'function') {
message = options.message.apply(this, arguments);
if (message) {
if (typeof message === 'string') {
message = messages.formatMessage(message, options);
} else if (typeof message === 'function') {
message = message.apply(this, arguments);
message = isNone(message) ? messages.getMessageFor(type, options) : messages.formatMessage(message, options);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Dummy Tests</title>
<title>Ember CP Validations - Tests</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">

Expand Down
13 changes: 12 additions & 1 deletion tests/unit/validators/base-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test('value - default gets model value', function(assert) {
});

validator.init();

assert.equal(validator.get('attribute'), 'foo');
assert.equal(validator.getValue(), 'bar');
});
Expand All @@ -115,3 +115,14 @@ test('value - overwrite value method via options', function(assert) {
assert.equal(validator.getValue(), 'baz');
assert.deepEqual(validator.get('options'), {});
});

test('message - handles SafeString', function(assert) {
assert.expect(1);

options = {
message: Ember.String.htmlSafe('should be more than &euro;15')
};

message = validator.createErrorMessage(undefined, undefined, options);
assert.equal(message, 'should be more than &euro;15');
});

0 comments on commit ab0f10e

Please sign in to comment.