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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b6f336626eb409c9c4fcb45b2a3cb56c01bf5a08c8f82eafb57d21d3721e7ad1
4
- data.tar.gz: 54e0b4b15c6b99a3d8abca95f7e12892245c7c334551aba158b0797588f77904
3
+ metadata.gz: c6db30cea880bd70a7a4c6ceeae65ebb28806ab5b62e0271ef9a660fbc88f394
4
+ data.tar.gz: 965eaf3101f70f84c6aae336ade54700580c0b3a850cb3c9813d61a3f1f7c23d
5
5
  SHA512:
6
- metadata.gz: 57378c5f785cf126d5284c42334663b8a89e322097168d2776d63188481b227d1491168574c557b1e58def51e561dbc181f767073f74b3a510817921f1cca4fc
7
- data.tar.gz: 66e21d13ecf91c3a4b15f09ee7d054ad88bcc0d62fd51218fc460463ac32d9878d701bdd0cd08c511ba919cb6b63d743a8d9a444906ca9961a7eb537b4db83cc
6
+ metadata.gz: 0d119eb25721f1f31a094e2d0c303af91ff9643a93d14c935c2a46c94efac99c614f72361c7e1ad8760ab7c0d1412486ce460dbd33e3e12293cc85e74a679362
7
+ data.tar.gz: b77c97d4355ea181408702e5a240f4ce99edbf232c1bb7c924d70a1344945ede034fded66c87b6ce055e1bd579f2be8758dc97f1d2e2023611e003566906121d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0
4
+
5
+ - Add `:any` type to skip type validation for a given param.
6
+
3
7
  ## 1.2.7
4
8
 
5
9
  - Fix issue where the JSONAPI formatter did not ignore `meta` when `data` is omitted.
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)
@@ -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? = Types.scalar?(type)
284
+ def scalar? = type.scalar?
285
+ def any? = type.any?
285
286
  def formatter? = !!@formatter
286
287
 
287
288
  def inspect
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParams
4
+ module Types
5
+ register(:any,
6
+ abstract: true,
7
+ match: -> _ {},
8
+ )
9
+ end
10
+ end
@@ -41,6 +41,7 @@ module TypedParams
41
41
  def accepts_block? = !!@accepts_block
42
42
  def coercable? = !!@coerce
43
43
  def scalar? = !!@scalar
44
+ def any? = @type == :any
44
45
  def abstract? = !!@abstract
45
46
  def subtype? = !!@archetype
46
47
 
@@ -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?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TypedParams
4
- VERSION = '1.2.7'
4
+ VERSION = '1.3.0'
5
5
  end
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.2.7
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-28 00:00:00.000000000 Z
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