Hello guys,
Thank you all for your work. Your gems are awesome.
I want to migrate all of my JSON schemas to the dry-validation and the dry-schema but I found out that dry-schema doesn’t have some functionality I need.
Could you check my question on StackOverflow, please?
Validating that exactly one of each schema are present would require a Dry::Validation::Contract
ByValue = Dry::Schema.JSON do
required(:type).value(:string, eql?: "type_1")
required(:value).value(:integer)
end
ByKind = Dry::Schema.JSON do
required(:type).value(:string, eql?: "type_2")
required(:kind).filled
end
class MyContract < Dry::Validation::Contract
json do
required(:array).array do
schema ByValue | ByKind
end
end
rule :array do
unless value.count { |v| ByValue.valid?(v) } == 1
key.failure("must have exactly one ByValue schema")
end
end
rule :array do
unless value.count { |v| ByKind.valid?(v) } == 1
key.failure("must have exactly one ByKind schema")
end
end
end