Skip to content

New validation: 3bytes characters filter (4 bytes characters cannot be stored using UTF8) #12253

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

Merged
merged 2 commits into from
Nov 28, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ $class = ($_option->getIsRequire()) ? ' required' : '';
if ($_option->getMaxCharacters()) {
$_textValidate['maxlength'] = $_option->getMaxCharacters();
}
$_textValidate['validate-no-utf8mb4-characters'] = true;
?>
<input type="text"
id="options_<?= /* @escapeNotVerified */ $_option->getId() ?>_text"
Expand All @@ -47,6 +48,7 @@ $class = ($_option->getIsRequire()) ? ' required' : '';
if ($_option->getMaxCharacters()) {
$_textAreaValidate['maxlength'] = $_option->getMaxCharacters();
}
$_textAreaValidate['validate-no-utf8mb4-characters'] = true;
?>
<textarea id="options_<?= /* @escapeNotVerified */ $_option->getId() ?>_text"
class="product-custom-option"
Expand Down
72 changes: 72 additions & 0 deletions dev/tests/js/jasmine/tests/lib/mage/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,76 @@ define([
)).toEqual(true);
});
});

describe('Testing 3 bytes characters only policy (UTF-8)', function () {
it('rejects data, if any of the characters cannot be stored using UTF-8 collation', function () {
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, '😅😂', null
)).toEqual(false);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, '😅 test 😂', null
)).toEqual(false);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, '💩 👻 💀', null
)).toEqual(false);
});

it('approves data, if all the characters can be stored using UTF-8 collation', function () {
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, '', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, '!$-_%ç&#?!', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, '1234567890', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, ' ', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'test', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'испытание', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'тест', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'փորձարկում', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'परीक्षण', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'テスト', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, '테스트', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, '测试', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, '測試', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'ทดสอบ', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'δοκιμή', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'اختبار', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'تست', null
)).toEqual(true);
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
$.validator.prototype, 'מִבְחָן', null
)).toEqual(true);
});
});

});
1 change: 1 addition & 0 deletions lib/web/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Submit,Submit
"Password cannot be the same as email address.","Password cannot be the same as email address."
"Please fix this field.","Please fix this field."
"Please enter a valid email address.","Please enter a valid email address."
"Please remove invalid characters: {0}.", "Please remove invalid characters: {0}."
"Please enter a valid URL.","Please enter a valid URL."
"Please enter a valid date (ISO).","Please enter a valid date (ISO)."
"Please enter only digits.","Please enter only digits."
Expand Down
18 changes: 18 additions & 0 deletions lib/web/mage/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,24 @@
$.mage.__('Please enter at least {0} characters')
],

/* detect chars that would require more than 3 bytes */
'validate-no-utf8mb4-characters': [
function (value) {
var validator = this,
message = $.mage.__('Please remove invalid characters: {0}.'),
matches = value.match(/(?:[\uD800-\uDBFF][\uDC00-\uDFFF])/g),
result = matches === null;

if (!result) {
validator.charErrorMessage = message.replace('{0}', matches.join());
}

return result;
}, function () {
return this.charErrorMessage;
}
],

/* eslint-disable max-len */
'email2': [
function (value, element) {
Expand Down