Skip to content

Commit

Permalink
update validation examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Jul 5, 2023
1 parent 8d180cf commit b973d63
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,18 @@ The lambda must return a tuple with the new key and value.
#### Validate parameter

Define a custom validation for the parameter, outside of the default
validations.
validations. The can be useful for defining mutually exclusive params,
or even validating that an ID exists before proceeding.

```ruby
# Mutually exclusive params
param :login, type: :hash, validate: -> v { v.key?(:username) ^ v.key?(:email) } do
param :username, type: :string, optional: true
param :email, type: :string, optional: true
param :password, type: :string
end

# Assert user exists
param :user, type: :integer, validate: -> id {
User.exists?(id)
}
Expand All @@ -655,6 +664,15 @@ The lambda should accept a value and return a boolean. When the boolean
evaluates to `false`, a `TypedParams::InvalidParameterError` will
be raised.

To customize the error message, the lambda can raise a `TypedParams::ValidationError`
error:

```ruby
param :invalid, type: :string, validate: -> v {
raise TypedParams::ValidationError, 'is always invalid'
}
```

### Shared options

You can define a set of options that will be applied to immediate
Expand Down

0 comments on commit b973d63

Please sign in to comment.