Is it possible to reuse a validator which includes it’s schema and rules? Take the following, for example:
#! /usr/bin/env ruby
# frozen_string_literal: true
# Save as `snippet`, then `chmod 755 snippet`, and run as `./snippet`.
require "bundler/inline"
gemfile true do
source "https://rubygems.org"
gem "amazing_print"
gem "debug"
gem "dry-validation"
end
class Inner < Dry::Validation::Contract
json do
required(:label).filled(:string)
end
rule :label do
key.failure("value can only be: Prometheus") unless value == "Prometheus"
end
end
class Outer < Dry::Validation::Contract
json do
required(:context).schema(Inner.schema)
end
end
result = Outer.new.call context: {label: "Hades"}
ap result.errors.to_h # {}
Notice that I include Inner
as a required schema within Outer
but I don’t see an error for :label
since only the schema is include but not the rules. Is there a way to fold in both the schema and the rules from another validator?
I was searching around and I see a lot of schema inclusion discussion but nothing with rules. I can’t see any support for this in the source code but wanted to ask anyway.