dry-struct Как условно проверить один атрибут?
Я использую dry-types и dry-struct, и я хотел бы получить условную проверку.
для класса:
class Tax < Dry::Struct
attribute :tax_type, Types::String.constrained(min_size: 2, max_size: 3, included_in: %w[IVA IS NS])
attribute :tax_country_region, Types::String.constrained(max_size: 5)
attribute :tax_code, Types::String.constrained(max_size: 10)
attribute :description, Types::String.constrained(max_size: 255)
attribute :tax_percentage, Types::Integer
attribute :tax_ammount, Types::Integer.optional
end
Я хочу проверить tax_ammount
как целое число и обязательно, если `tax_type == 'IS'.
2 ответа
dry-struct
действительно для базового утверждения типа и принуждения.
Если вы хотите более сложную проверку, вы, вероятно, хотите реализовать dry-validation
а также (как рекомендовано dry-rb
)
См. Проверка данных с помощью dry-struct, которая сообщает
Пожалуйста, не надо. Структуры предназначены для работы с допустимым вводом, он не может генерировать сообщения об ошибках, достаточно хорошие для отображения их для пользователя и т. Д. Используйте сухую проверку для проверки входящих данных, а затем передавать их выходные данные в структуры.
Условная проверка с использованием dry-validation
было бы что-то вроде
TaxValidation = Dry::Validation.Schema do
# Could be:
# required(:tax_type).filled(:str?,
# size?: 2..3,
# included_in?: %w(IVA IS NS))
# but since we are validating against a list of Strings I figured the rest was implied
required(:tax_type).filled(included_in?: %w(IVA IS NS))
optional(:tax_amount).maybe(:int?)
# rule name is of your choosing and will be used
# as the errors key (i just chose `tax_amount` for consistency)
rule(tax_amount:[:tax_type, :tax_amount]) do |tax_type, tax_amount|
tax_type.eql?('IS').then(tax_amount.filled?)
end
end
- Это требует
tax_type
быть в%w(IVA IS NS)
список; - Позволяет
tax_amount
быть необязательным, но если оно заполнено, оно должно бытьInteger
(int?
) а также; - Если
tax_type == 'IS'
(eql?('IS')
) затемtax_amount
должен быть заполнен (это означает, что это должно бытьInteger
на основании правила выше).
Очевидно, что вы можете проверить и другие ваши входные данные, но я оставил их для краткости.
Примеры:
TaxValidation.({}).success?
#=> false
TaxValidation.({}).errors
# => {:tax_type=>["is missing"]}
TaxValidation.({tax_type: 'NO'}).errors
#=> {:tax_type=>["must be one of: IVA, IS, NS"]}
TaxValidation.({tax_type: 'NS'}).errors
#=> {}
TaxValidation.({tax_type: 'IS'}).errors
#=> {:tax_amount=>["must be filled"]}
TaxValidation.({tax_type: 'IS',tax_amount:'NO'}).errors
#=> {:tax_amount=>["must be an integer"]}
TaxValidation.({tax_type: 'NS',tax_amount:12}).errors
#=> {}
TaxValidation.({tax_type: 'NS',tax_amount:12}).success?
#=> true
Есть альтернативное решение — без правил проверки, которые по сути дублируют атрибуты структуры.
module TaxChore
class BaseTax < Dry::Struct
attribute :type, Types::String.enum('IVA','NS')
# ...
attribute? :amount, Types::Integer.default(0)
end
class MandatoryTax < BaseTax
attribute :type, Types::String.enum('IS')
attribute :amount, Types::Integer
end
Tax = BaseTax | MandatoryTax
def self.run
tax = Tax.(type: 'IVA')
p tax
tax = Tax.(type: 'IVA', amount: 21)
p tax
tax = Tax.(type: 'IS', amount: 42)
p tax
begin
tax = Tax.(type: 'IS')
p tax
rescue Dry::Struct::Error => e
puts e
end
end
run
end
Выходы:
#<TaxChore::BaseTax type="IVA" amount=0>
#<TaxChore::BaseTax type="NS" amount=21>
#<TaxChore::MandatoryTax type="IS" amount=42>
[TaxChore::MandatoryTax.new] :amount is missing in Hash input
(NB: я удалил лишнее
tax_
префикс, так как
tax.type
так же ясно, но короче.)