0

We are looking at use the reform gem for validating input.

One of the issues we are facing is that we accept input in this format:

params = {
  records: {
    "record-id-23423424": {
      name:       'Joe Smith'
    }
    "record-id-43234233": {
      name:       'Jane Doe'
    }
    "record-id-345234555": {
      name:       'Fox trot'
    }
    "record-id-34234234": {
      name:       'Alex'
    }
  }
}

so if we were to create reform class

class RecordForm < Reform::Form
  property :records
  validates :records, presence: true

  # ?????????
end

How do we validate the contents of the records to make sure each one has a name? The record-id-values are not known ahead of time.

Daniel
  • 7,006
  • 7
  • 43
  • 49
  • 1
    Are you stuck with the present schema? It would be easier to create the validation if the schema would be something like `{ records: [{ id: "record-id-23423424", name: 'Joe Smith'}, ...] }` – Laura Paakkinen Dec 21 '17 at 06:28

1 Answers1

0

Reform currently doesn't allow dynamic properties, and actually, it's not planned since Reform is supposed to be a UI-specific form object.

The solution would be to pre-parse your input into something what Laura suggests. You could then have nested properties for each field.

collection :records do
  property :id # manually parsed
  property :name
end
apotonick
  • 520
  • 2
  • 9