Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-22 09:20:29 +01:00
Apply consistent date validation on the server (using v8) (#124)
* Apply consistent date validation on the server (using v8) * Variable fix * added specs to verify date/time field validation * minor text change Co-authored-by: Faizaan Gagan <fzngagan@gmail.com>
Dieser Commit ist enthalten in:
Ursprung
7bfa0aff70
Commit
f80f40d6b3
2 geänderte Dateien mit 51 neuen und 8 gelöschten Zeilen
|
@ -52,7 +52,7 @@ class ::CustomWizard::UpdateValidator
|
|||
@updater.errors.add(field_id, I18n.t('wizard.field.invalid_file', label: label, types: file_types))
|
||||
end
|
||||
|
||||
if ['date', 'date_time'].include?(type) && value.present? && !validate_date(value)
|
||||
if ['date', 'date_time'].include?(type) && value.present? && !validate_date(value, format)
|
||||
@updater.errors.add(field_id, I18n.t('wizard.field.invalid_date'))
|
||||
end
|
||||
|
||||
|
@ -88,13 +88,8 @@ class ::CustomWizard::UpdateValidator
|
|||
.include?(File.extname(value['original_filename'])[1..-1])
|
||||
end
|
||||
|
||||
def validate_date(value)
|
||||
begin
|
||||
Date.parse(value)
|
||||
true
|
||||
rescue ArgumentError
|
||||
false
|
||||
end
|
||||
def validate_date(value, format)
|
||||
v8.eval("moment('#{value}', '#{format}', true).isValid()")
|
||||
end
|
||||
|
||||
def validate_time(value)
|
||||
|
@ -126,4 +121,12 @@ class ::CustomWizard::UpdateValidator
|
|||
def standardise_boolean(value)
|
||||
ActiveRecord::Type::Boolean.new.cast(value)
|
||||
end
|
||||
|
||||
def v8
|
||||
return @ctx if @ctx
|
||||
|
||||
@ctx = PrettyText.v8
|
||||
PrettyText.ctx_load(@ctx, "#{Rails.root}/vendor/assets/javascripts/moment.js")
|
||||
@ctx
|
||||
end
|
||||
end
|
||||
|
|
|
@ -132,4 +132,44 @@ describe CustomWizard::UpdateValidator do
|
|||
updater.errors.messages[:step_2_field_6].first
|
||||
).to eq(nil)
|
||||
end
|
||||
|
||||
it 'validates date fields' do
|
||||
@template[:steps][1][:fields][0][:format] = "DD-MM-YYYY"
|
||||
CustomWizard::Template.save(@template)
|
||||
|
||||
updater = perform_validation('step_2', step_2_field_1: '13-11-2021')
|
||||
expect(
|
||||
updater.errors.messages[:step_2_field_1].first
|
||||
).to eq(nil)
|
||||
end
|
||||
|
||||
it 'doesn\'t validate date field if the format is not respected' do
|
||||
@template[:steps][1][:fields][0][:format] = "MM-DD-YYYY"
|
||||
CustomWizard::Template.save(@template)
|
||||
|
||||
updater = perform_validation('step_2', step_2_field_1: '13-11-2021')
|
||||
expect(
|
||||
updater.errors.messages[:step_2_field_1].first
|
||||
).to eq(I18n.t('wizard.field.invalid_date'))
|
||||
end
|
||||
|
||||
it 'validates date time fields' do
|
||||
@template[:steps][1][:fields][2][:format] = "DD-MM-YYYY HH:mm:ss"
|
||||
CustomWizard::Template.save(@template)
|
||||
|
||||
updater = perform_validation('step_2', step_2_field_3: '13-11-2021 09:15:00')
|
||||
expect(
|
||||
updater.errors.messages[:step_2_field_3].first
|
||||
).to eq(nil)
|
||||
end
|
||||
|
||||
it 'doesn\'t validate date time field if the format is not respected' do
|
||||
@template[:steps][1][:fields][2][:format] = "MM-DD-YYYY HH:mm:ss"
|
||||
CustomWizard::Template.save(@template)
|
||||
|
||||
updater = perform_validation('step_2', step_2_field_3: '13-11-2021 09:15')
|
||||
expect(
|
||||
updater.errors.messages[:step_2_field_3].first
|
||||
).to eq(I18n.t('wizard.field.invalid_date'))
|
||||
end
|
||||
end
|
||||
|
|
Laden …
In neuem Issue referenzieren