Search code examples
ruby-on-railstrailblazerreform

Trailblazer Reform Contract - dry-validate - Virtual Field Validation


I am trying to create a registration form with Trailblazer and validation setup using Dry-Validation. I hit a snag which seems to be related to virtual fields. My contract looks like this.

module User::Contract
  class Register < Reform::Form
    model :user

    property :first_name
    property :last_name
    property :password, virtual: true
    property :password_confirmation, virtual: true
    property :email
    property :phone
    property :bio

    validation do
      option :form

      params do
        required(:first_name).filled(:string)
        required(:last_name).filled(:string)
        required(:email).filled(:string)
        required(:phone).filled(:string)
      end

      rule(:email) do
        unless /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
          key.failure('has invalid format')
        end
      end

      rule(:password) do
        key.failure('does not match') if value != form.password_confirmation
        key.failure('too short') if value.length < 7
      end
    end
  end
end

When I run it in the terminal i get this:

contract = User::Contract::Register.new(User.new)
contract.valid?
Dry::Validation::InvalidKeysError (.rule specifies keys that are not defined by the schema: [:password])

Question

Looks like the issue I am getting is because its a virtual field and because using Dry-Validation. I tried commenting the internal code and still had the issue. How do I validate a virtual field?

Any advice highly appreciated.


Solution

  • Yogesh Khater from Trailblazer Zulip Chat was kind enough to point out the error for me.

    When we need to define a rule for a virtual field, we need to make sure that the property is required. This is a requirement in the dry-validation library.

    The above can be fixed by requiring the property.

    class Register < Reform::Form
      model :user
    
      property :password, virtual: true
      property :password_confirmation, virtual: true
      # ....
    
      validation do
        option :form
    
        params do
          # ...
          required(:password).filled(:string)
        end
    
        rule(:password) do
          # ...
        end
      end
    end