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

Max chars input validation #375

Merged
merged 2 commits into from
Dec 19, 2020
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
1 change: 1 addition & 0 deletions src/model/external_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def parameter_to_external(parameter):
'type': parameter.type,
'min': parameter.min,
'max': parameter.max,
'max_length': parameter.max_length,
'values': parameter.values,
'secure': parameter.secure,
'fileRecursive': parameter.file_recursive,
Expand Down
7 changes: 6 additions & 1 deletion src/model/parameter_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'type',
'min',
'max',
'max_length',
'constant',
'_values_provider',
'values',
Expand Down Expand Up @@ -71,6 +72,7 @@ def _reload(self):
self.required = read_bool_from_config('required', config, default=False)
self.min = config.get('min')
self.max = config.get('max')
self.max_length = config.get('max_length')
self.secure = read_bool_from_config('secure', config, default=False)
self.separator = config.get('separator', ',')
self.multiple_arguments = read_bool_from_config('multiple_arguments', config, default=False)
Expand Down Expand Up @@ -277,6 +279,9 @@ def validate_value(self, value, *, ignore_required=False):
return None

if self.type == 'text':
if (not is_empty(self.max_length)) and (len(value) > int(self.max_length)):
return 'is longer than allowed char length (' \
+ str(len(value)) + ' > ' + str(self.max_length) + ')'
return None

if self.type == 'file_upload':
Expand All @@ -291,7 +296,7 @@ def validate_value(self, value, *, ignore_required=False):
int_value = int(value)

if (not is_empty(self.max)) and (int_value > int(self.max)):
return 'is greater than allowed value (' \
return 'is longer than allowed value (' \
+ value_string + ' > ' + str(self.max) + ')'

if (not is_empty(self.min)) and (int_value < int(self.min)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
secure: 'secure',
min: 'min',
max: 'max',
max_length: 'max_length',
multipleArguments: 'multiple_arguments',
sameArgParam: 'same_arg_param',
separator: 'separator',
Expand Down Expand Up @@ -169,6 +170,7 @@
description: null,
min: null,
max: null,
max_length: null,
allowedValues: null,
allowedValuesScript: null,
allowedValuesFromScript: null,
Expand All @@ -193,6 +195,7 @@
descriptionField,
minField,
maxField: Object.assign({}, maxField),
maxLengthField,
allowedValuesScriptField,
allowedValuesFromScriptField,
defaultValueField: Object.assign({}, defaultValueField),
Expand Down Expand Up @@ -221,6 +224,7 @@
this.required = get(config, 'required', false);
this.min = config['min'];
this.max = config['max'];
this.max_length = config['max_length'];
this.constant = !!get(config, 'constant', false);
this.secure = !!get(config, 'secure', false);
this.multipleArguments = !!get(config, 'multiple_arguments', false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export const allowedValuesScriptField = {
required: true
};

export const maxLengthField = {
name: 'Max Characters',
type: 'int'
};

export const allowedValuesFromScriptField = {
name: 'Load from script',
withoutValue: true,
Expand Down
12 changes: 10 additions & 2 deletions web-src/src/common/components/textfield.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
}

if (!empty) {
var typeError = getValidByTypeError(value, this.config.type, this.config.min, this.config.max);
var typeError = getValidByTypeError(value, this.config.type, this.config.min, this.config.max, this.config.max_length);
if (!isEmptyString(typeError)) {
return typeError;
}
Expand Down Expand Up @@ -149,7 +149,15 @@
}
}

function getValidByTypeError(value, type, min, max) {
function getValidByTypeError(value, type, min, max, max_length) {
if (type === 'text') {
if (max_length) {
if (value.length > max_length) {
return 'Max chars allowed: ' + max_length
}
}
}

if (type === 'int') {
const isInteger = /^(((-?[1-9])(\d*))|0)$/.test(value);
if (!isInteger) {
Expand Down