Skip to content

Validating change sets

E. Lynette Rayle edited this page Oct 1, 2021 · 2 revisions

back to Introduction

Goals

  • Test validation by creating a new resource and saving with the Change Set validators.
  • Test validation by editing an existing resource and saving with Change Set validators.

Validations through a change set

Basic process...

  • create a change set for a resource
  • make changes to change set
  • sync change set to resource if change set is valid?
  • persist resource

SPOTCHECK

For a new resource

In rails console...

book_cs = BookChangeSet.new(Book.new)
book_cs.valid? # false - with no data set, several validations fail 
book_cs.title = "Lullaby Town"
book_cs.author = "Robert Crais"
book_cs.series = "Elvis Cole"
book_cs.valid? # true - all data entered correctly
book = book_cs.sync

book.title  # ["Lullaby Town"]
book.author # ["Robert Crais"]
book.series # "Elvis Cole"

book = ValkyriePgDemo.pg_persister.save(resource: book)

For an existing resource

# Create and persist a valid book using the example above.
book_cs = BookChangeSet.new(book)
book_cs.valid? # true - values copied from the saved book are all valid
book_cs.title = "The Watchman"
book_cs.series = "Joe Pike"
book_cs.valid? # true - all data entered correctly
book = book_cs.sync

book.title  # ["The Watchman"]
book.author # ["Robert Crais"]
book.series # "Joe Pike"

Listing what failed

book_cs = BookChangeSet.new(Book.new)
book_cs.valid? # false - with no data set, several validations fail 
book_cs.errors.messages # {:title=>["can't be blank"], :author=>["can't be blank"], :series=>[" is not a valid series"]}

Common GOTCHAS

Error messages do not show current errors

errors.messages only updates when valid? is called even if recent changes would have different errors

book_cs = BookChangeSet.new(Book.new)
book_cs.valid? # false - with no data set, several validations fail 
book_cs.errors.messages # {:title=>["can't be blank"], :author=>["can't be blank"], :series=>[" is not a valid series"]}

book_cs.title = "The Last Detective"
book_cs.series = "Elvis Cole"
book_cs.errors.messages # {:title=>["can't be blank"], :author=>["can't be blank"], :series=>[" is not a valid series"]}
# These error are incorrect now that title and series have been set.  The author error is still correct.

book_cs.valid? # false - still false because of author error
book_cs.errors.messages {:author=>["can't be blank"]} # updated when valid? was called and shows only current errors

valid? passes, but sync fails

As mentioned in Persisting-changes-through-synchronization - Common Gotchas, sychronization will fail if the value in the change set is incompatible with the resource attribute's type. Validation does not check types. So the validation can pass and the sync still fails.

resource attribute with required: true passes validation when missing

Defining required on the property does not impact validations.

property :title, required: true
changeset.title = nil
changeset.required? :title # true
changeset.valid? # true, but would expect it to be false

To validate that a required...

property :title, required: true
validates :title, presence: true
changeset.title = nil
changeset.required? :title # true
changeset.valid? # false as expected

Previous | Next

Clone this wiki locally