-
Notifications
You must be signed in to change notification settings - Fork 20
Defining change set validations
E. Lynette Rayle edited this page Jan 22, 2020
·
10 revisions
Reference: ActiveRecord validations
Supports validations through ActiveModel validations. Not all ActiveRecord validations are supported (e.g. uniqueness is not supported). Some common examples are...
- validates property_name, property_name, ..., presence - passes if the property or list of properties all have values present
- validates_with ClassName[, property: property_name] - calls #validate method in the identified class to determine if the property's value is valid; if no property_name is identified, it is used to validate the change set as a whole
example book change set with validations in app/change_sets/book_change_set.rb
# frozen_string_literal: true
class BookChangeSet < Valkyrie::ChangeSet
property :title, multiple: true, required: true
property :author, multiple: true, required: true
property :series, multiple: false, required: false
property :member_ids, multiple: true, required: false
validates :title, :author, presence: true
validates_with SeriesValidator
end
example custom validator in app/validators/series_validator.rb
# frozen_string_literal: true
class SeriesValidator < ActiveModel::Validator
# ensure the property exists and is in the controlled vocabulary
def validate(record)
return ['Joe Pike', 'Elvis Cole', 'Elvis Cole/Joe Pike'].include? record.series)
record.errors.add :series, "#{record.series} is not a valid series"
end
end