Skip to content

Cross-property validations #302

@joeldrapper

Description

@joeldrapper

I’ve been thinking about ways to support cross-property validations. Here’s one idea I had for this interface.

class Foo
  prop :name, String
  prop :min, Integer
  prop :max, Integer
  prop :pattern, Regexp

  validate("Min must be less than max") { |min, max| min < max }
end

The way this would work is we would look at the names of the parameters of the block given to validate and based on those names, send the corresponding values. Calling block.parameters on the above block would return this, so the names are available.

[[:opt, :min], [:opt, :max]]

I believe this could be done at load time so runtime execution is extremely fast. Perhaps block could be defined as a private method with a generated name, and the generated initialiser would just generate the code to call that method with the relevant parameters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions