diff --git a/app/graphql/types/request_password_recovery_input.rb b/app/graphql/types/request_password_recovery_input.rb index 822183e..08b8246 100644 --- a/app/graphql/types/request_password_recovery_input.rb +++ b/app/graphql/types/request_password_recovery_input.rb @@ -1,5 +1,5 @@ module Types class RequestPasswordRecoveryInput < Types::BaseInputObject - argument :email, String, required: true + argument :email, SquishedString, required: true end end diff --git a/app/graphql/types/sign_in_input.rb b/app/graphql/types/sign_in_input.rb index 2bdcdc2..d066f8f 100644 --- a/app/graphql/types/sign_in_input.rb +++ b/app/graphql/types/sign_in_input.rb @@ -1,6 +1,6 @@ module Types class SignInInput < Types::BaseInputObject - argument :email, String, required: true + argument :email, SquishedString, required: true argument :password, String, required: true end end diff --git a/app/graphql/types/sign_up_input.rb b/app/graphql/types/sign_up_input.rb index ebe512d..26168e2 100644 --- a/app/graphql/types/sign_up_input.rb +++ b/app/graphql/types/sign_up_input.rb @@ -1,10 +1,10 @@ module Types class SignUpInput < Types::BaseInputObject - argument :email, String, required: true + argument :email, SquishedString, required: true argument :password, String, required: true - argument :first_name, String, required: false - argument :last_name, String, required: false + argument :first_name, SquishedString, required: false + argument :last_name, SquishedString, required: false argument :avatar, Types::ImageUploaderType, required: false end diff --git a/app/graphql/types/squished_string.rb b/app/graphql/types/squished_string.rb new file mode 100644 index 0000000..061a676 --- /dev/null +++ b/app/graphql/types/squished_string.rb @@ -0,0 +1,14 @@ +module Types + class SquishedString < BaseScalar + def self.coerce_input(input_value, _context) + squished = input_value.try(:squish) + raise GraphQL::CoercionError, "#{input_value.inspect} is not a valid string" unless squished.is_a? String + + squished + end + + def self.coerce_result(ruby_value, _context) + ruby_value.to_s + end + end +end diff --git a/app/graphql/types/update_user_input.rb b/app/graphql/types/update_user_input.rb index 977f932..8a11753 100644 --- a/app/graphql/types/update_user_input.rb +++ b/app/graphql/types/update_user_input.rb @@ -1,8 +1,8 @@ module Types class UpdateUserInput < Types::BaseInputObject - argument :email, String, required: false - argument :first_name, String, required: false - argument :last_name, String, required: false + argument :email, SquishedString, required: false + argument :first_name, SquishedString, required: false + argument :last_name, SquishedString, required: false argument :current_password, String, required: false argument :password, String, required: false argument :avatar, Types::ImageUploaderType, required: false diff --git a/spec/graphql/types/squished_string_spec.rb b/spec/graphql/types/squished_string_spec.rb new file mode 100644 index 0000000..51e2bf7 --- /dev/null +++ b/spec/graphql/types/squished_string_spec.rb @@ -0,0 +1,27 @@ +require "rails_helper" + +describe Types::SquishedString do + describe "#coerce_input" do + subject(:method) { described_class.coerce_input(input_value, {}) } + + context "with regular string" do + let(:input_value) { "String without extra whitespaces" } + + it { is_expected.to eq("String without extra whitespaces") } + end + + context "with overspaced string" do + let(:input_value) { " String with \t extra \n whitespaces " } + + it { is_expected.to eq("String with extra whitespaces") } + end + + context "with not string provided" do + let(:input_value) { 42 } + + it "raises graphql error" do + expect { method }.to raise_error(::GraphQL::CoercionError, "42 is not a valid string") + end + end + end +end