Hi there,
I am currently in the process of evalating using dry-schema
for some input validation before hitting our ActiveRecord classes in a Rails application. Everything is working nice and smoothely. But now I wanted to add some custom predicates to match some of our application inputs. For example, we use prefixed-uuids for all of our objects and I want to validate with a Regex that only ids with a certain prefix can be passed in:
module CustomPredicates
include Dry::Logic::Predicates
def uuid_with_prefix?(prefix, input)
input =~ /\A#{prefix}_[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\Z/
end
end
class ApplicationSchema < Dry::Schema::JSON
# here I would like to specify where the predicates are loaded from
end
The documentation says at one point that inheriting like this is possible, but that seems not to be correct. I have opened a GH issue about this. I am not too fixated on this, but it would be the nicest way to reuse logic.
class PostSchema < ApplicationSchema
required(:text).filled(:string?)
required(:author).filled(:string?, uuid_with_prefix?: 'user')
end
I have tried all different method of how to specify predicates that are documented in the other gems like confgure { config.predicates = CustomPredicates }
but I suspect that in dry-schema
I somehow need access to the PredicateRegistry
but it is not exposed anywhere.
Is there maybe something still missing, that this cannot be done yet? Should I better stick with the old version of dry-validation
until the extraction process is done? Or am I completely missing something?
If somebody points me into the right direction, I would be totally happy to contribute a section of the readme that describes how to do this.