0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 18:50:27 +01:00

FIX: apply text length validations for non-empty fields only (#137)

* apply text length validations for non-empty fields only

* consolidated logic

* fixed formatting

* fix formatting

* calculate length only for string type values

* fix assignment

* added specs
Dieser Commit ist enthalten in:
Faizaan Gagan 2021-07-30 16:03:48 +05:30 committet von GitHub
Ursprung ee9e9a1d94
Commit 998757f857
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
2 geänderte Dateien mit 32 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -32,12 +32,14 @@ class ::CustomWizard::UpdateValidator
@updater.errors.add(field_id, I18n.t('wizard.field.required', label: label)) @updater.errors.add(field_id, I18n.t('wizard.field.required', label: label))
end end
if min_length.present? && value.is_a?(String) && value.strip.length < min_length.to_i if value.is_a?(String) && (stripped_length = value.strip.length) > 0
@updater.errors.add(field_id, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i)) if min_length.present? && stripped_length < min_length.to_i
end @updater.errors.add(field_id, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
end
if max_length.present? && value.is_a?(String) && value.strip.length > max_length.to_i if max_length.present? && stripped_length > max_length.to_i
@updater.errors.add(field_id, I18n.t('wizard.field.too_long', label: label, max: max_length.to_i)) @updater.errors.add(field_id, I18n.t('wizard.field.too_long', label: label, max: max_length.to_i))
end
end end
if is_url_type(field) && value.present? && !check_if_url(value) if is_url_type(field) && value.present? && !check_if_url(value)

Datei anzeigen

@ -97,6 +97,31 @@ describe CustomWizard::UpdateValidator do
).to eq(nil) ).to eq(nil)
end end
it "applies min length only if the input is non-empty" do
min_length = 3
@template[:steps][0][:fields][0][:min_length] = min_length
CustomWizard::Template.save(@template)
updater = perform_validation('step_1', step_1_field_1: '')
expect(
updater.errors.messages[:step_1_field_1].first
).to eq(nil)
end
it "applies max length only if the input is non-empty" do
max_length = 100
@template[:steps][0][:fields][0][:max_length] = max_length
CustomWizard::Template.save(@template)
updater = perform_validation('step_1', step_1_field_1: "")
expect(
updater.errors.messages[:step_1_field_1].first
).to eq(nil)
end
it 'standardises boolean entries' do it 'standardises boolean entries' do
updater = perform_validation('step_2', step_2_field_5: 'false') updater = perform_validation('step_2', step_2_field_5: 'false')
expect(updater.submission['step_2_field_5']).to eq(false) expect(updater.submission['step_2_field_5']).to eq(false)