Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Fix] Ember.String.htmlSafe throws exception #203

Merged
merged 4 commits into from
Jun 3, 2016
Merged
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
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');
});