rails-param-validation 0.6.1 → 0.7.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: 8d5f88225db33a9b096591e45a663a2eea47e6e23ce8f57183e1cf9348d77a63
4
- data.tar.gz: 0b8de32953399fd870133a26d79d913cb0bfd4aeb820a3d33d6d76e84bd67c79
3
+ metadata.gz: 4d62012d6b37182ecd5ca7cd0e35a6fe7aaa8742395b0421e5125119e45d0bba
4
+ data.tar.gz: 4d4051c2ba5edcc773233c4b1c841344f46c75957b00dc62c53b45d7d4c2c07a
5
5
  SHA512:
6
- metadata.gz: 6c737d8ec89c6b01d85f3f1f3eb23b5964063d015fe11961046e5e1aea26762b143a78138e8f412b34fa8cc3f9086d3f7df296d4ca475a659a49f40be753274d
7
- data.tar.gz: edce82d4eeb6d5832726ec4cf9b8dd60ae6aa3195fcc02b616960867cc306d8154ec8b64b0bfefb344ba964da5fd0b299f10a20ae5486966a524d002d57e58bc
6
+ metadata.gz: e12db0d05b2d308e9dcfc7a07b622428ed777541b3a385d1bcc5b69816c774731ff98f37e895063d199ce0056b3e3e4baed8df994a86423e6f1639ec613f347a
7
+ data.tar.gz: a4d2e8cdce7df2d08b12df85dcccc1cc9fa1a8eaa182a6b1314b04110be9cce0a753e1ff3b72687ce15b88c60999ac2368a80db0152c68acab3b1f013df4e223
data/bin/.keep CHANGED
File without changes
@@ -1,14 +1,38 @@
1
1
  module RailsParamValidation
2
-
3
2
  class AlternativesValidator < Validator
4
3
  # @param [Array] schema
5
4
  def initialize(schema, collection)
5
+ schema = AlternativesValidator.simplify schema
6
6
  super schema, collection
7
7
 
8
- @inner_validators = schema.map { |value| ValidatorFactory.create(value, collection) }
9
- if @inner_validators.all? { |v| v.is_a?(ConstantValidator) }
10
- @inner_validators.sort_by! { |v| v.constant.to_s }
8
+ @inner_validators = []
9
+
10
+ previous_enum_values = nil
11
+ schema.each do |value|
12
+ validator = ValidatorFactory.create(value, collection)
13
+ if validator.is_a?(ConstantValidator)
14
+ if previous_enum_values.nil?
15
+ previous_enum_values = [value]
16
+ else
17
+ if validator.constant.class == previous_enum_values.first.class
18
+ previous_enum_values << value
19
+ else
20
+ @inner_validators << ConstantEnumValidator.new(previous_enum_values, collection)
21
+ previous_enum_values = [value]
22
+ end
23
+ end
24
+
25
+ next
26
+ end
27
+
28
+ if previous_enum_values
29
+ @inner_validators << ConstantEnumValidator.new(previous_enum_values, collection)
30
+ previous_enum_values = nil
31
+ end
32
+ @inner_validators << validator
11
33
  end
34
+
35
+ @inner_validators << ConstantEnumValidator.new(previous_enum_values, collection) if previous_enum_values
12
36
  end
13
37
 
14
38
  def matches?(path, data)
@@ -28,8 +52,28 @@ class AlternativesValidator < Validator
28
52
  end
29
53
 
30
54
  def to_openapi
55
+ if @inner_validators.size == 1
56
+ return @inner_validators.first.to_openapi
57
+ end
58
+
31
59
  { oneOf: @inner_validators.map(&:to_openapi) }
32
60
  end
61
+
62
+ protected
63
+
64
+ def self.simplify(schema)
65
+ alternatives = []
66
+
67
+ schema.each do |sub_schema|
68
+ if sub_schema.is_a?(Array)
69
+ alternatives += sub_schema
70
+ else
71
+ alternatives << sub_schema
72
+ end
73
+ end
74
+
75
+ alternatives
76
+ end
33
77
  end
34
78
 
35
79
  class AlternativesValidatorFactory < ValidatorFactory
@@ -0,0 +1,28 @@
1
+ module RailsParamValidation
2
+ class ConstantEnumValidator < Validator
3
+ def initialize(schema, collection)
4
+ super schema, collection
5
+ @constants = schema
6
+ end
7
+
8
+ def matches?(path, data)
9
+ detected_value = @constants.any? { |c| c.to_s == data.to_s }
10
+ unless detected_value
11
+ return MatchResult.new(nil, path, "The value is not one of the allowed constants")
12
+ end
13
+
14
+ MatchResult.new detected_value, path
15
+ end
16
+
17
+ def to_openapi
18
+ ValidatorFactory.create(
19
+ ConstantValidator::CLASS_MAP.fetch(
20
+ @constants.first.class,
21
+ @constants.first.class
22
+ ),
23
+ collection
24
+ ).to_openapi.merge(enum: @constants)
25
+ end
26
+ end
27
+
28
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsParamValidation
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -19,6 +19,7 @@ require_relative './rails-param-validation/validators/object'
19
19
  require_relative './rails-param-validation/validators/optional'
20
20
  require_relative './rails-param-validation/validators/hash'
21
21
  require_relative './rails-param-validation/validators/alternatives'
22
+ require_relative './rails-param-validation/validators/constant_enum'
22
23
 
23
24
  require_relative './rails-param-validation/rails/extensions/annotation_extension' unless defined?(Rails)
24
25
  require_relative './rails-param-validation/rails/rails' if defined?(Rails)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-param-validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oskar Kirmis
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-11 00:00:00.000000000 Z
11
+ date: 2024-05-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Declarative parameter definition and validation for Rails
14
14
  email:
@@ -57,6 +57,7 @@ files:
57
57
  - lib/rails-param-validation/validators/array.rb
58
58
  - lib/rails-param-validation/validators/boolean.rb
59
59
  - lib/rails-param-validation/validators/constant.rb
60
+ - lib/rails-param-validation/validators/constant_enum.rb
60
61
  - lib/rails-param-validation/validators/custom_type.rb
61
62
  - lib/rails-param-validation/validators/date.rb
62
63
  - lib/rails-param-validation/validators/datetime.rb
@@ -78,7 +79,7 @@ metadata:
78
79
  homepage_uri: https://okirmis.github.io/rails-param-validation
79
80
  source_code_uri: https://github.com/okirmis/rails-param-validation
80
81
  changelog_uri: https://github.com/okirmis/rails-param-validation
81
- post_install_message:
82
+ post_install_message:
82
83
  rdoc_options: []
83
84
  require_paths:
84
85
  - lib
@@ -94,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  version: '0'
95
96
  requirements: []
96
97
  rubygems_version: 3.3.26
97
- signing_key:
98
+ signing_key:
98
99
  specification_version: 4
99
100
  summary: Declarative parameter definition and validation for Rails
100
101
  test_files: []