lib/dry/validation/schema/value.rb in dry-validation-0.8.0 vs lib/dry/validation/schema/value.rb in dry-validation-0.9.0

- old
+ new

@@ -16,12 +16,12 @@ def predicates(mod) @registry = options[:registry] = schema_class.predicates(mod) end - def input(type) - schema_class.config.input = type + def input(*predicates) + schema_class.config.input = predicates self end def key(name, &block) warn 'key is deprecated - use required instead.' @@ -113,13 +113,12 @@ def check(name, options = {}) Check[name, options.merge(type: type)] end def configure(&block) - klass = ::Class.new(schema_class, &block) - @schema_class = klass - @registry = klass.registry + schema_class.class_eval(&block) + @registry = schema_class.registry self end def root? name.nil? @@ -144,11 +143,11 @@ def key?(name) create_rule([:val, registry[:key?].curry(name).to_ast]) end def predicate(name, *args) - registry.ensure_valid_predicate(name, args) + registry.ensure_valid_predicate(name, args, schema_class) registry[name].curry(*args) end def node(input, *args) if input.is_a?(::Symbol) @@ -162,13 +161,17 @@ else [type, [name, input.to_ast]] end end + def dyn_arg?(name) + schema_class.instance_methods.include?(name) + end + private - def infer_predicates(predicates, infer_on) + def infer_predicates(predicates, infer_on = self) predicates.map { |predicate| name, *args = ::Kernel.Array(predicate).first if name.is_a?(Schema) infer_on.schema(name) @@ -177,9 +180,11 @@ end }.reduce(:and) end def method_missing(meth, *args, &block) + return schema_class.instance_method(meth) if dyn_arg?(meth) + val_rule = create_rule([:val, predicate(meth, *args).to_ast]) if block val = new.instance_eval(&block)