Skip to content

Commit

Permalink
feat(asset): validation checks byte length of name and description
Browse files Browse the repository at this point in the history
Before this fix asset name and description validation was based on length in chars. This didn't work well, because node checks byte length of strings.
  • Loading branch information
beregovoy68 committed Dec 1, 2016
1 parent 14b77de commit 427e69d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
26 changes: 23 additions & 3 deletions src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ function AngularApplicationConfig($provide, $validatorProvider, networkConstants
}
}
});
$validatorProvider.addMethod('address', function(value, element) {
$validatorProvider.addMethod('address', function (value, element) {
return this.optional(element) || __mockValidateAddress(value);
}, 'Account number must be a sequence of 35 alphanumeric characters with no spaces, ' +
'optionally starting with \'1W\'');
$validatorProvider.addMethod('decimal', function(value, element, params) {
$validatorProvider.addMethod('decimal', function (value, element, params) {
var maxdigits = angular.isNumber(params) ? params : Currency.WAV.precision;

var regex = new RegExp('^(?:-?\\d+)?(?:\\.\\d{0,' + maxdigits + '})?$');
return this.optional(element) || regex.test(value);
}, 'Amount is expected with a dot (.) as a decimal separator with no more than {0} fraction digits');
$validatorProvider.addMethod('password', function(value, element) {
$validatorProvider.addMethod('password', function (value, element) {
if (this.optional(element))
return true;

Expand All @@ -80,6 +80,26 @@ function AngularApplicationConfig($provide, $validatorProvider, networkConstants
return containsDigits && containsUppercase && containsLowercase;
}, 'The password is too weak. A good password must contain at least one digit, ' +
'one uppercase and one lowercase letter');
$validatorProvider.addMethod('minbytelength', function (value, element, params) {
if (this.optional(element))
return true;

if (!angular.isNumber(params))
throw new Error('minbytelength parameter must be a number. Got ' + params);

var minLength = params;
return converters.stringToByteArray(value).length >= minLength;
}, 'String is too short. Please add more characters.');
$validatorProvider.addMethod('maxbytelength', function (value, element, params) {
if (this.optional(element))
return true;

if (!angular.isNumber(params))
throw new Error('maxbytelength parameter must be a number. Got ' + params);

var maxLength = params;
return converters.stringToByteArray(value).length <= maxLength;
}, 'String is too long. Please remove some characters.');
}

AngularApplicationConfig.$inject = ['$provide', '$validatorProvider', 'constants.network', 'constants.application'];
Expand Down
13 changes: 6 additions & 7 deletions src/js/tokens/token.create.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
rules: {
assetName: {
required: true,
minlength: ASSET_NAME_MIN,
maxlength: ASSET_NAME_MAX
minbytelength: ASSET_NAME_MIN,
maxbytelength: ASSET_NAME_MAX
},
assetDescription: {
maxlength: ASSET_DESCRIPTION_MAX
maxbytelength: ASSET_DESCRIPTION_MAX
},
assetTotalTokens: {
required: true,
Expand All @@ -46,12 +46,11 @@
messages: {
assetName: {
required: 'Asset name is required',
minlength: 'Asset name minimum length must be ' + ASSET_NAME_MIN + ' characters',
maxlength: 'Asset name maximum length must be ' + ASSET_NAME_MAX + ' characters'
minbytelength: 'Asset name is too short. Please give your asset a longer name',
maxbytelength: 'Asset name is too long. Please give your asset a shorter name'
},
assetDescription: {
maxlength: 'Maximum length of asset description must be less than ' + ASSET_DESCRIPTION_MAX +
' characters'
maxbytelength: 'Maximum length of asset description exceeded. Please make a shorter description'
},
assetTotalTokens: {
required: 'Total amount of issued tokens in required',
Expand Down

0 comments on commit 427e69d

Please sign in to comment.