dry-struct
真正用于基本类型断言和强制。
如果您想要更复杂的验证,那么您可能也想要实现dry-validation
(如 推荐的那样dry-rb
)
请参阅Validating data with 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