Skip to content

Transform Validator into a Factory #74

@enchf

Description

@enchf

In general, a validator should behave as a tree with leaves of three types:

  • A root, generate by a static method in Validator, with no predicate.
  • An intermediate leaf, with a parent and children.
  • A leaf, or an ending path in the tree without children.

The broken up of predicates in slices and single arguments will help in conceptualize.

Validator should then be responsible of:

  • Receiving the full set of arguments and determine which arguments to take.
  • Validate its own predicate and pass the full set of arguments to each children.
  • Determine the validity of the whole subtree, having a way to reduce/fold its children.

The Arguments provider are build as a Factory (#68) and can determine a valid arity of the input.

A Validator is immutable, its predicate, arguments provider and parent set build in the constructor, and its children are set as through builder generator function. Example:

# Root
Validator.start.evaluate do
  # Custom trunk
  trunk.arguments(:all).predicate { |*args| args.all?(:numeric?) }.build do
    leaf.arguments(:first) { |value| value == 1 }
  end
end

Validators can be:

  • Pre-defined in factories.
  • Have a type/identifier.

Special cases can be found:

  • Logical operators: Intermediate leafs being AND (all) and OR (any), predicateless, will only determine the reductor.
  • Arguments: Predicate to validate size according to arity, each children will be bijectively related to the input arguments.
  • Varargs/Tail: Special configuration of arguments validator. When added:
    • No more validators could be added.
    • Its input arguments are the tail of the list from the full set of arguments.
    • Tail can be empty.
    • Each child is applied against each argument in the tail. To preserve generic functionality, n children are appended, having n equal to the tail size, and the children validators block builder is applied to each child.

After doing this, there are extra steps:

  • The validator can write values to a Context, which can be used later in action execution.

Originally posted by @enchf in #70 (comment)

Metadata

Metadata

Assignees

Labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions