Module: Fend::Plugins::ObjectValidation
- Defined in:
- lib/fend/plugins/object_validation.rb
Overview
‘object_validation` plugin adds support for validating object attributes and methods.
class UserModelValidation < Fend
plugin :object_validation
plugin :validation_options
validate do |user|
user.attrs(:username, :email) do |username, email|
username.validate(presence: true, max_length: 20, type: String)
email.validate(presence: true, format: EMAIL_REGEX, type: String)
end
end
end
user = User.new(username: "", email: "invalid@email")
validation = UserModelValidation.call(user)
validation.success? #=> false
validation. #=> { username: ["must be present"], email: ["is in invalid format"] }
As the example shows, the only change is that instread of the ‘#params` you should use `#attrs` method.
## Handling hash values
If attribute value should be a hash, you can still use the ‘#params` method:
# user.address #=> { city: "My city", street: "My street" }
user.attrs(:address) do |address|
address.params(:city, :street) do |city, street|
# ...
end
end
Defined Under Namespace
Modules: ParamMethods