Skip to content

Commit

Permalink
add alias and polymorphic options to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Jan 5, 2024
1 parent d92940e commit cd58129
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ Parameters can have validations, transforms, and more.
- [`:optional`](#optional-parameter)
- [`:if` and `:unless`](#conditional-parameter)
- [`:as`](#rename-parameter)
- [`:alias`](#alias-parameter)
- [`:noop`](#noop-parameter)
- [`:coerce`](#coerce-parameter)
- [`:allow_blank`](#allow-blank)
Expand All @@ -448,6 +449,7 @@ Parameters can have validations, transforms, and more.
- [`:length`](#length-validation)
- [`:transform`](#transform-parameter)
- [`:validate`](#validate-parameter)
- [`:polymorphic`](#polymorphic-parameter)

#### Parameter key

Expand Down Expand Up @@ -520,6 +522,17 @@ typed_params # => { user_id: 1 }
In this example, the parameter would be accepted as `:user`, but renamed
to `:user_id` for use inside of the controller.

#### Alias parameter

Allow a parameter to be provided via an alias.

```ruby
param :owner_id, type: :integer, alias: :user_id
```

In this example, the parameter would be accepted as both `:owner_id` and
`:user_id`, but accessible as `:owner_id` inside the controller.

#### Noop parameter

The parameter is accepted but immediately thrown out.
Expand Down Expand Up @@ -674,6 +687,33 @@ param :invalid, type: :string, validate: -> v {
}
```

#### Polymorphic parameter

***Currently, this option is only utilized by the JSONAPI formatter. Define
a polymorphic parameter, used when formatting JSONAPI relationships.***

```ruby
format :jsonapi

param :data, type: :hash do
param :relationships, type: :hash do
param :owner, type: :hash, polymorphic: true do
param :data, type: :hash do
param :type, type: :string, inclusion: { in: %w[users user] }
param :id, type: :integer
end
end
end
end

typed_params # => { owner_type: 'User', owner_id: 1 }
```

In this example, a polymorphic `:owner` relationship is defined. When run
through the JSONAPI formatter, instead of formatting the relationship
into the `:owner_id` key, it also includes the `:owner_type` key
for a polymorphic association.

### Shared options

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

0 comments on commit cd58129

Please sign in to comment.