Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-22 09:20:29 +01:00
Ensure url field validates properly and accepts empty inputs
Dieser Commit ist enthalten in:
Ursprung
2bca82a159
Commit
5794349244
3 geänderte Dateien mit 38 neuen und 9 gelöschten Zeilen
|
@ -1,3 +1,5 @@
|
||||||
|
require 'addressable/uri'
|
||||||
|
|
||||||
class ::CustomWizard::UpdateValidator
|
class ::CustomWizard::UpdateValidator
|
||||||
attr_reader :updater
|
attr_reader :updater
|
||||||
|
|
||||||
|
@ -37,7 +39,7 @@ class ::CustomWizard::UpdateValidator
|
||||||
@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
|
||||||
|
|
||||||
if is_url_type(field) && !check_if_url(value)
|
if is_url_type(field) && value.present? && !check_if_url(value)
|
||||||
@updater.errors.add(field_id, I18n.t('wizard.field.not_url', label: label))
|
@updater.errors.add(field_id, I18n.t('wizard.field.not_url', label: label))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -111,8 +113,13 @@ class ::CustomWizard::UpdateValidator
|
||||||
['url'].include? field.type
|
['url'].include? field.type
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_if_url(value)
|
SCHEMES ||= %w(http https)
|
||||||
value =~ URI::regexp
|
|
||||||
|
def check_if_url(url)
|
||||||
|
parsed = Addressable::URI.parse(url) or return false
|
||||||
|
SCHEMES.include?(parsed.scheme)
|
||||||
|
rescue Addressable::URI::InvalidURIError
|
||||||
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def standardise_boolean(value)
|
def standardise_boolean(value)
|
||||||
|
|
|
@ -10,11 +10,7 @@ describe CustomWizard::UpdateValidator do
|
||||||
}
|
}
|
||||||
|
|
||||||
before do
|
before do
|
||||||
CustomWizard::Template.save(
|
CustomWizard::Template.save(template, skip_jobs: true)
|
||||||
JSON.parse(File.open(
|
|
||||||
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
|
|
||||||
).read),
|
|
||||||
skip_jobs: true)
|
|
||||||
@template = CustomWizard::Template.find('super_mega_fun_wizard')
|
@template = CustomWizard::Template.find('super_mega_fun_wizard')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -114,4 +110,25 @@ describe CustomWizard::UpdateValidator do
|
||||||
updater.errors.messages[:step_1_field_2].first
|
updater.errors.messages[:step_1_field_2].first
|
||||||
).to eq(I18n.t('wizard.field.required', label: 'Textarea'))
|
).to eq(I18n.t('wizard.field.required', label: 'Textarea'))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'validates url fields' do
|
||||||
|
updater = perform_validation('step_2', step_2_field_6: 'https://discourse.com')
|
||||||
|
expect(
|
||||||
|
updater.errors.messages[:step_2_field_6].first
|
||||||
|
).to eq(nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not validate url fields with non-url inputs' do
|
||||||
|
updater = perform_validation('step_2', step_2_field_6: 'discourse')
|
||||||
|
expect(
|
||||||
|
updater.errors.messages[:step_2_field_6].first
|
||||||
|
).to eq(I18n.t('wizard.field.not_url', label: 'Url'))
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'validates empty url fields' do
|
||||||
|
updater = perform_validation('step_2', step_2_field_6: '')
|
||||||
|
expect(
|
||||||
|
updater.errors.messages[:step_2_field_6].first
|
||||||
|
).to eq(nil)
|
||||||
|
end
|
||||||
end
|
end
|
5
spec/fixtures/wizard.json
gevendort
5
spec/fixtures/wizard.json
gevendort
|
@ -81,6 +81,11 @@
|
||||||
"label": "Checkbox",
|
"label": "Checkbox",
|
||||||
"type": "checkbox"
|
"type": "checkbox"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "step_2_field_6",
|
||||||
|
"label": "Url",
|
||||||
|
"type": "url"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "step_2_field_7",
|
"id": "step_2_field_7",
|
||||||
"label": "Upload",
|
"label": "Upload",
|
||||||
|
|
Laden …
In neuem Issue referenzieren