-
Notifications
You must be signed in to change notification settings - Fork 23
Validating change sets
E. Lynette Rayle edited this page Oct 1, 2021
·
2 revisions
- 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.
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
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)
# 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"
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"]}
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
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.
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