Skip to content

Commit a383875

Browse files
committed
add docs on custom formats
1 parent 0b0734d commit a383875

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Links:
6868
- [Non-scalar types](#non-scalar-types)
6969
- [Custom types](#custom-types)
7070
- [Formats](#formats)
71+
- [Custom formats](#custom-formats)
7172
- [Contributing](#contributing)
7273
- [License](#license)
7374

@@ -944,6 +945,39 @@ class UsersController < ApplicationController
944945
end
945946
```
946947

948+
### Custom formats
949+
950+
You may register custom formatters that can be utilized in your schemas.
951+
952+
```rb
953+
TypedParams::Formatters.register(:strong_params,
954+
transform: -> (params, controller:) {
955+
wrapper = controller.controller_name.singularize.to_sym
956+
unwrapped = params[wrapper]
957+
958+
ActionController::Parameters.new(unwrapped).permit!
959+
},
960+
)
961+
```
962+
963+
```rb
964+
typed_params {
965+
format :strong_params
966+
967+
param :user, type: :hash do
968+
param :email, type: :string, format: { with: /@/ }
969+
param :password, type: :string
970+
end
971+
}
972+
def create
973+
puts user_params
974+
# => #<ActionController::Parameters
975+
# {"email"=>"[email protected]","password"=>"7c84241a1102"}
976+
# permitted: true
977+
# >
978+
end
979+
```
980+
947981
## Is it any good?
948982

949983
[Yes.](https://news.ycombinator.com/item?id=3067434)

spec/typed_params/formatters_spec.rb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
require 'spec_helper'
44

55
RSpec.describe TypedParams::Formatters do
6-
describe '.register' do
7-
after { TypedParams::Formatters.unregister(:test) }
6+
after { TypedParams::Formatters.unregister(:test) }
87

8+
describe '.register' do
99
it 'should register format' do
1010
format = TypedParams::Formatters.register(:test,
11-
transform: -> k, v { [k, v] },
11+
transform: -> params { params },
1212
)
1313

1414
expect(TypedParams::Formatters.formats[:test]).to eq format
1515
end
1616

1717
it 'should not register a duplicate format' do
1818
format = TypedParams::Formatters.register(:test,
19-
transform: -> k, v { [k, v] },
19+
transform: -> params { params },
2020
)
2121

22-
expect { TypedParams::Formatters.register(:test, transform: -> k, v { [k, v] }) }
22+
expect { TypedParams::Formatters.register(:test, transform: -> params { params }) }
2323
.to raise_error ArgumentError
2424
end
2525
end
2626

2727
describe '.unregister' do
2828
it 'should unregister format' do
29-
TypedParams::Formatters.register(:test, transform: -> k, v { [k, v] },)
29+
TypedParams::Formatters.register(:test, transform: -> params { params })
3030
TypedParams::Formatters.unregister(:test)
3131

3232
expect(TypedParams::Formatters.formats[:test]).to be_nil
@@ -40,4 +40,22 @@
4040
expect(format.format).to eq :jsonapi
4141
end
4242
end
43-
end
43+
44+
it 'should use format' do
45+
TypedParams::Formatters.register(:test,
46+
transform: -> params { params.deep_transform_keys { _1.to_s.camelize(:lower).to_sym } },
47+
)
48+
49+
schema = TypedParams::Schema.new(type: :hash) do
50+
format :test
51+
52+
param :foo_bar, type: :string
53+
end
54+
55+
params = TypedParams::Parameterizer.new(schema:).call(
56+
value: { foo_bar: 'baz' },
57+
)
58+
59+
expect(params.unwrap).to eq(fooBar: 'baz')
60+
end
61+
end

0 commit comments

Comments
 (0)