Add validation for unique and non-empty field questions.

Add validation for unique and non-empty field questions.

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 99e72cd..0bac2dd 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -8,3 +8,5 @@ en:
     user_already_responded: "You have already responded to this survey."
     max_one_survey_per_post: "Only one survey is allowed in a post."
     cannot_edit: "Survey cannot be edited because it has a response."
+    survey_must_have_different_questions: "Survey field questions should be different."
+    survey_must_not_have_any_empty_questions: "Survey field questions cannot be blank."
diff --git a/lib/discourse-surveys/survey_validator.rb b/lib/discourse-surveys/survey_validator.rb
index 9fb1fb4..0e5338d 100644
--- a/lib/discourse-surveys/survey_validator.rb
+++ b/lib/discourse-surveys/survey_validator.rb
@@ -14,10 +14,8 @@ module DiscourseSurveys
       survey_count = 0
 
       DiscourseSurveys::Helper::extract(@post.raw, @post.topic_id, @post.user_id).each do |survey|
-        # return false unless valid_arguments?(survey)
-        # return false unless unique_options?(survey)
-        # return false unless any_blank_options?(survey)
-        # return false unless at_least_one_option?(survey)
+        return false unless unique_questions?(survey)
+        return false unless any_blank_questions?(survey)
         surveys["survey"] = survey
         survey_count += 1
       end
@@ -32,65 +30,22 @@ module DiscourseSurveys
 
     private
 
-    def valid_arguments?(survey)
-      valid = true
-
-      survey["fields"].each do |field|
-        if field["response_type"].present? && !::SurveyField.response_type.has_key?(field["response_type"])
-          @post.errors.add(:base, I18n.t("survey.invalid_argument", argument: "response_type", value: field["response_type"]))
-          valid = false
-        end
-
-        unless field["question"].present?
-          @post.errors.add(:base, I18n.t("survey.blank_question"))
-          valid = false
-        end
-      end
-
-      valid
-    end
-
-    def unique_options?(survey)
-      if survey["options"].map { |o| o["id"] }.uniq.size != survey["options"].size
-        if survey["name"] == ::DiscourseSurveys::DEFAULT_SURVEY_NAME
-          @post.errors.add(:base, I18n.t("survey.default_survey_must_have_different_options"))
-        else
-          @post.errors.add(:base, I18n.t("survey.named_survey_must_have_different_options", name: survey["name"]))
-        end
-
-        return false
-      end
-
-      true
-    end
-
-    def any_blank_options?(survey)
-      if survey["options"].any? { |o| o["html"].blank? }
-        if survey["name"] == ::DiscourseSurveys::DEFAULT_SURVEY_NAME
-          @post.errors.add(:base, I18n.t("survey.default_survey_must_not_have_any_empty_options"))
-        else
-          @post.errors.add(:base, I18n.t("survey.named_survey_must_not_have_any_empty_options", name: survey["name"]))
-        end
-
+    def unique_questions?(survey)
+      if survey["fields"].map { |f| f["question"] }.uniq.size != survey["fields"].size
+        @post.errors.add(:base, I18n.t("survey.survey_must_have_different_questions"))
         return false
       end
 
       true
     end
 
-    def at_least_one_option?(survey)
-      if survey["options"].size < 1
-        if survey["name"] == ::DiscourseSurveys::DEFAULT_SURVEY_NAME
-          @post.errors.add(:base, I18n.t("survey.default_survey_must_have_at_least_1_option"))
-        else
-          @post.errors.add(:base, I18n.t("survey.named_survey_must_have_at_least_1_option", name: survey["name"]))
-        end
-
+    def any_blank_questions?(survey)
+      if survey["fields"].any? { |f| f["question"].blank? }
+        @post.errors.add(:base, I18n.t("survey.survey_must_not_have_any_empty_questions"))
         return false
       end
 
       true
     end
-
   end
 end

GitHub sha: 437c5574