我正在使用graphql-ruby gem,并且我有一个更新记录的突变。这些都可以正常工作,但是现在我想包含phony_rails gem来验证电话号码。
问题作为一个有更多FE经验的开发人员,我不太擅长rails或ruby,我正在用它来学习。也就是说,我有两个想要验证的电话号码——home_phone_number
和mobile_phone_number
。我的突变参数是这样的:
# frozen_string_literal: true
module Mutations
module Person
class UpdatePerson < BaseMutation
visibility_role :introspect_admin
visibility_pundit_class PersonPolicy
argument :id, ID, required: true
argument :email, String, required: false
argument :home_phone_number, String, required: false
argument :mobile_phone_number, String, required: false
field :person, Types::Person::Person, null: true
def call(input = {})
current_user = context[:current_user]
authorize!(current_user)
person = Person.find(input[:id])
person.email = input[:email]
person.home_phone_number = input[:home_phone_number]
person.mobile_phone_number = input[:mobile_phone_number]
person.save!
{ error: nil, person: person }
end
# @param [User|nil] current_user
def authorize!(current_user)
raise Pundit::NotAuthorizedError, 'Not allowed to update person.' unless
PersonPolicy.new(current_user, nil).update?
end
end
end
end
现在我想给home_phone_number
和mobile_phone_number
添加验证。我把我的测试写成这样:
context 'invalid number home phone number' do
let(:variables) do
{
'input' => {
'id' => person.id,
'homePhoneNumber' => '123'
}
}
end
it 'should return an error if home phone is invalid' do
expect(subject).not_to contain_graphql_errors
expect(data_dig('error')).not_to be_nil
expect(data_dig('error', 'error')).to eq('standard_error')
expect(data_dig('error', 'description')).to eq('Home phone number must be valid')
end
end
context 'invalid number mobile phone number' do
let(:variables) do
{
'input' => {
'id' => person.id,
'mobilePhoneNumber' => '123'
}
}
end
it 'should return an error if mobile phone is invalid' do
expect(subject).not_to contain_graphql_errors
expect(data_dig('error')).not_to be_nil
expect(data_dig('error', 'error')).to eq('standard_error')
expect(data_dig('error', 'description')).to eq('Mobile phone number must be valid')
end
end
我已经试过了
我能得到的是这个,但不一定通过我的测试:
def call(input = {})
current_user = context[:current_user]
authorize!(current_user)
validate_phone_numbers(input[:home_phone_number], input[:mobile_phone_number])
# ....
def validate_phone_numbers(home_phone_number, mobile_phone_number)
phone_numbers = [home_phone_number, mobile_phone_number]
phone_numbers.each do |contact|
raise StandardError, 'Phone Number must be valid' if !PhonyRails.plausible_number?(contact) #would this stop execution too?
end
end
如你所见,在这样做时,我将无法指定哪个是家庭电话号码,哪个是手机号码。
我也试着一个一个地做:
def validate_phone_numbers(home_phone_number, mobile_phone_number)
home_phone_number_valid = PhonyRails.plausible_number?(home_phone_number)
mobile_phone_number_valid = PhonyRails.plausible_number?(mobile_phone_number)
raise StandardError, 'Home phone number must be valid' if !home_phone_number_valid
raise StandardError, 'Mobile phone number must be valid' if !mobile_phone_number_valid
# Stop execution
return if !home_phone_number_valid || !mobile_phone_number_valid
end
上面的行也不能完全工作。
如果能给我一些指导,我将非常感激。谢谢你!不要在解析器中返回{ error: nil, person: person }
,而是尝试返回{ error: errors_array, person: person }
,其中errors_array包含来自phony的任何验证错误消息。如:
def call
...
errors_array = []
errors_array << 'Home Phone Number must be valid' if !PhonyRails.plausible_number?(input[:home_phone_number])
errors_array << 'Mobile Phone Number must be valid' if !PhonyRails.plausible_number?(input[:mobile_phone_number])
...
{ error: errors_array, person: person }
end
还要确保在你的说明书中包括手机和家庭电话号码无效的情况。
我希望这对你有帮助!