From 00682fb4c2dffbd3ead55ee91c46a3562fa508ee Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 7 Dec 2021 14:20:34 +0530 Subject: [PATCH] FIX: field validation was not working in backend (#165) * FIX: field validation was not working in backend * added tests --- lib/custom_wizard/validators/template.rb | 4 +-- plugin.rb | 2 +- .../custom_wizard/template_validator_spec.rb | 33 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/custom_wizard/validators/template.rb b/lib/custom_wizard/validators/template.rb index c90944e9..d4a8367f 100644 --- a/lib/custom_wizard/validators/template.rb +++ b/lib/custom_wizard/validators/template.rb @@ -18,8 +18,8 @@ class CustomWizard::TemplateValidator data[:steps].each do |step| check_required(step, :step) - if data[:fields].present? - data[:fields].each do |field| + if step[:fields].present? + step[:fields].each do |field| check_required(field, :field) end end diff --git a/plugin.rb b/plugin.rb index 6f68ae3b..9768412c 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Create custom wizards -# version: 1.15.5 +# version: 1.15.6 # authors: Angus McLeod # url: https://github.com/paviliondev/discourse-custom-wizard # contact emails: angus@thepavilion.io diff --git a/spec/components/custom_wizard/template_validator_spec.rb b/spec/components/custom_wizard/template_validator_spec.rb index c8ce915a..015228a3 100644 --- a/spec/components/custom_wizard/template_validator_spec.rb +++ b/spec/components/custom_wizard/template_validator_spec.rb @@ -45,4 +45,37 @@ describe CustomWizard::TemplateValidator do CustomWizard::TemplateValidator.new(template).perform ).to eq(false) end + + context "steps" do + CustomWizard::TemplateValidator.required[:step].each do |attribute| + it "invalidates if \"#{attribute.to_s}\" is not present" do + template[:steps][0][attribute] = nil + expect( + CustomWizard::TemplateValidator.new(template).perform + ).to eq(false) + end + end + end + + context "fields" do + CustomWizard::TemplateValidator.required[:field].each do |attribute| + it "invalidates if \"#{attribute.to_s}\" is not present" do + template[:steps][0][:fields][0][attribute] = nil + expect( + CustomWizard::TemplateValidator.new(template).perform + ).to eq(false) + end + end + end + + context "actions" do + CustomWizard::TemplateValidator.required[:action].each do |attribute| + it "invalidates if \"#{attribute.to_s}\" is not present" do + template[:actions][0][attribute] = nil + expect( + CustomWizard::TemplateValidator.new(template).perform + ).to eq(false) + end + end + end end