typed_params 1.2.7 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +40 -0
- data/lib/typed_params/schema.rb +2 -1
- data/lib/typed_params/types/any.rb +10 -0
- data/lib/typed_params/types/type.rb +1 -0
- data/lib/typed_params/validator.rb +1 -1
- data/lib/typed_params/version.rb +1 -1
- data/lib/typed_params.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6db30cea880bd70a7a4c6ceeae65ebb28806ab5b62e0271ef9a660fbc88f394
|
4
|
+
data.tar.gz: 965eaf3101f70f84c6aae336ade54700580c0b3a850cb3c9813d61a3f1f7c23d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d119eb25721f1f31a094e2d0c303af91ff9643a93d14c935c2a46c94efac99c614f72361c7e1ad8760ab7c0d1412486ce460dbd33e3e12293cc85e74a679362
|
7
|
+
data.tar.gz: b77c97d4355ea181408702e5a240f4ce99edbf232c1bb7c924d70a1344945ede034fded66c87b6ce055e1bd579f2be8758dc97f1d2e2023611e003566906121d
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -68,6 +68,7 @@ Links:
|
|
68
68
|
- [Non-scalar types](#non-scalar-types)
|
69
69
|
- [Custom types](#custom-types)
|
70
70
|
- [Formats](#formats)
|
71
|
+
- [Custom formats](#custom-formats)
|
71
72
|
- [Contributing](#contributing)
|
72
73
|
- [License](#license)
|
73
74
|
|
@@ -944,6 +945,45 @@ class UsersController < ApplicationController
|
|
944
945
|
end
|
945
946
|
```
|
946
947
|
|
948
|
+
### Custom formats
|
949
|
+
|
950
|
+
You may register custom formatters that can be utilized in your schemas.
|
951
|
+
|
952
|
+
Each formatter consists of, at minimum, a `transform:` lambda, accepting a
|
953
|
+
params hash as well as optional `controller:` and `schema:` keywords, and
|
954
|
+
returning the formatted params.
|
955
|
+
|
956
|
+
For more usage examples, see [the default formatters](https://github.com/keygen-sh/typed_params/tree/master/lib/typed_params/formatters).
|
957
|
+
|
958
|
+
```rb
|
959
|
+
TypedParams::Formatters.register(:strong_params,
|
960
|
+
transform: -> (params, controller:) {
|
961
|
+
wrapper = controller.controller_name.singularize.to_sym
|
962
|
+
unwrapped = params[wrapper]
|
963
|
+
|
964
|
+
ActionController::Parameters.new(unwrapped).permit!
|
965
|
+
},
|
966
|
+
)
|
967
|
+
```
|
968
|
+
|
969
|
+
```rb
|
970
|
+
typed_params {
|
971
|
+
format :strong_params
|
972
|
+
|
973
|
+
param :user, type: :hash do
|
974
|
+
param :email, type: :string, format: { with: /@/ }
|
975
|
+
param :password, type: :string
|
976
|
+
end
|
977
|
+
}
|
978
|
+
def create
|
979
|
+
puts user_params
|
980
|
+
# => #<ActionController::Parameters
|
981
|
+
# {"email"=>"json@smith.example","password"=>"7c84241a1102"}
|
982
|
+
# permitted: true
|
983
|
+
# >
|
984
|
+
end
|
985
|
+
```
|
986
|
+
|
947
987
|
## Is it any good?
|
948
988
|
|
949
989
|
[Yes.](https://news.ycombinator.com/item?id=3067434)
|
data/lib/typed_params/schema.rb
CHANGED
@@ -281,7 +281,8 @@ module TypedParams
|
|
281
281
|
def unless? = !@unless.nil?
|
282
282
|
def array? = Types.array?(type)
|
283
283
|
def hash? = Types.hash?(type)
|
284
|
-
def scalar? =
|
284
|
+
def scalar? = type.scalar?
|
285
|
+
def any? = type.any?
|
285
286
|
def formatter? = !!@formatter
|
286
287
|
|
287
288
|
def inspect
|
@@ -28,7 +28,7 @@ module TypedParams
|
|
28
28
|
|
29
29
|
# Assert type
|
30
30
|
raise InvalidParameterError.new("type mismatch (received #{type.humanize} expected #{schema.type.humanize})", path: param.path, source: schema.source) unless
|
31
|
-
type == schema.type || type.subtype? && type.archetype == schema.type
|
31
|
+
schema.any? || type == schema.type || type.subtype? && type.archetype == schema.type
|
32
32
|
|
33
33
|
# Assertions for params without children
|
34
34
|
if schema.children.nil?
|
data/lib/typed_params/version.rb
CHANGED
data/lib/typed_params.rb
CHANGED
@@ -26,6 +26,7 @@ require 'typed_params/transforms/noop'
|
|
26
26
|
require 'typed_params/transforms/transform'
|
27
27
|
require 'typed_params/transformer'
|
28
28
|
require 'typed_params/types'
|
29
|
+
require 'typed_params/types/any'
|
29
30
|
require 'typed_params/types/array'
|
30
31
|
require 'typed_params/types/boolean'
|
31
32
|
require 'typed_params/types/date'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typed_params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zeke Gabrielse
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-02
|
11
|
+
date: 2025-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- lib/typed_params/transforms/noop.rb
|
80
80
|
- lib/typed_params/transforms/transform.rb
|
81
81
|
- lib/typed_params/types.rb
|
82
|
+
- lib/typed_params/types/any.rb
|
82
83
|
- lib/typed_params/types/array.rb
|
83
84
|
- lib/typed_params/types/boolean.rb
|
84
85
|
- lib/typed_params/types/date.rb
|