rails-param-validation 0.4.14 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3eae277f67629481d14b7c25d6a8d6883b194f0e8a28df8b1dab38854e60b82
4
- data.tar.gz: a56276914e1d304238a6de444ec80d8cdfbacb29d00c64746fd2ce88fcc665b3
3
+ metadata.gz: 6c8f555ee0aafbe724456b93c7eb9fd8cdc54f38d892e9551fe8961bfa40ffb1
4
+ data.tar.gz: ffca5a75b38b8d282b52af89afcee6d4204ad118286cfeb9f11417e3a7e9494e
5
5
  SHA512:
6
- metadata.gz: f3629891ba43c919f837b234d0d4133983a43eca0cc0ce8609747624920dcac443ebc9c14acc5c583468493eb189735b5e1fef8fefe4ba0f936e8d011c647246
7
- data.tar.gz: 832116a057c74a7259fb7aae6e00ae777f9525102be9de6ef4074957969ccd1834ebd5253763d19d528e7b3faffadfc14ba0c844e1fd55f4202d3ae0db1e987d
6
+ metadata.gz: e9c3c97ea17e7aa12bdc37932cfeb9233cc40931217a2ff9be2fa2dac4baafaf874a2335b2efad8471235d9c70c85a90f3a8b53fc4e9245fa057b249eefd6203
7
+ data.tar.gz: a0ad7c5f21b4ac0b4c63bc1b570a8b4082eac1ac6c79f203c425da848d1edf7db10ff8cc671f952470d45b5e8daf20a2d0e5d71a4f375c7e8fecd86f050601de
@@ -33,10 +33,11 @@ module RailsParamValidation
33
33
  end
34
34
 
35
35
  class Validator
36
- attr_reader :schema
36
+ attr_reader :schema, :collection
37
37
 
38
- def initialize(schema)
38
+ def initialize(schema, collection)
39
39
  @schema = schema
40
+ @collection = collection
40
41
  end
41
42
 
42
43
  # @return [MatchData]
@@ -3,27 +3,45 @@ require_relative "./validator"
3
3
 
4
4
  module RailsParamValidation
5
5
 
6
+ class FactoryCollection
7
+ # @return [Array<ValidatorFactory>]
8
+ attr_reader :factories
9
+
10
+ # @param [Array<ValidatorFactory>] factories
11
+ def initialize(factories = [])
12
+ @factories = factories
13
+ end
14
+
15
+ def register(factory)
16
+ factories.push factory
17
+ end
18
+
19
+ def clone
20
+ FactoryCollection.new(factories.clone)
21
+ end
22
+ end
23
+
6
24
  class ValidatorFactory
7
25
 
8
26
  # @param [ValidatorFactory] factory
9
27
  def self.register(factory)
10
- factories.push factory
28
+ collection.register factory
11
29
  end
12
30
 
13
- # @return [Array<ValidatorFactory>]
14
- def self.factories
15
- @@factories ||= []
31
+ # @return [FactoryCollection]
32
+ def self.collection
33
+ @factories ||= FactoryCollection.new
16
34
  end
17
35
 
18
36
  # @return [Validator]
19
- def self.create(schema)
20
- factory = factories.detect { |f| f.supports? schema }
37
+ def self.create(schema, collection = self.collection)
38
+ factory = collection.factories.detect { |f| f.supports? schema }
21
39
 
22
40
  if factory.nil?
23
41
  raise NoMatchingFactory.new(schema)
24
42
  end
25
43
 
26
- factory.create schema
44
+ factory.create schema, collection
27
45
  end
28
46
 
29
47
  # @return [Boolean]
@@ -31,7 +49,7 @@ module RailsParamValidation
31
49
  end
32
50
 
33
51
  # @return Validator
34
- def create(schema)
52
+ def create(schema, collection)
35
53
  end
36
54
  end
37
55
  end
@@ -2,10 +2,10 @@ module RailsParamValidation
2
2
 
3
3
  class AlternativesValidator < Validator
4
4
  # @param [Array] schema
5
- def initialize(schema)
6
- super schema
5
+ def initialize(schema, collection)
6
+ super schema, collection
7
7
 
8
- @inner_validators = schema.map { |value| ValidatorFactory.create(value) }
8
+ @inner_validators = schema.map { |value| ValidatorFactory.create(value, collection) }
9
9
  end
10
10
 
11
11
  def matches?(path, data)
@@ -34,8 +34,8 @@ class AlternativesValidator < Validator
34
34
  schema.is_a? Array
35
35
  end
36
36
 
37
- def create(schema)
38
- AlternativesValidator.new schema
37
+ def create(schema, collection)
38
+ AlternativesValidator.new schema, collection
39
39
  end
40
40
  end
41
41
 
@@ -2,10 +2,10 @@ module RailsParamValidation
2
2
 
3
3
  class ArrayValidator < Validator
4
4
  # @param [ArrayT] schema
5
- def initialize(schema)
6
- super schema
5
+ def initialize(schema, collection)
6
+ super schema, collection
7
7
 
8
- @inner_validator = ValidatorFactory.create schema.inner_type
8
+ @inner_validator = ValidatorFactory.create schema.inner_type, collection
9
9
  end
10
10
 
11
11
  def matches?(path, data)
@@ -41,8 +41,8 @@ module RailsParamValidation
41
41
  schema.is_a? AnnotationTypes::ArrayT
42
42
  end
43
43
 
44
- def create(schema)
45
- ArrayValidator.new schema
44
+ def create(schema, collection)
45
+ ArrayValidator.new schema, collection
46
46
  end
47
47
  end
48
48
 
@@ -1,8 +1,8 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class BooleanValidator < Validator
4
- def initialize(schema)
5
- super schema
4
+ def initialize(schema, collection)
5
+ super schema, collection
6
6
  end
7
7
 
8
8
  def matches?(path, data)
@@ -30,8 +30,8 @@ module RailsParamValidation
30
30
  schema == Boolean
31
31
  end
32
32
 
33
- def create(schema)
34
- BooleanValidator.new schema
33
+ def create(schema, collection)
34
+ BooleanValidator.new schema, collection
35
35
  end
36
36
  end
37
37
 
@@ -3,8 +3,8 @@ module RailsParamValidation
3
3
  class ConstantValidator < Validator
4
4
  CLASS_MAP = { TrueClass => Boolean, FalseClass => Boolean }
5
5
 
6
- def initialize(schema)
7
- super schema
6
+ def initialize(schema, collection)
7
+ super schema, collection
8
8
 
9
9
  @constant = schema
10
10
  end
@@ -23,7 +23,7 @@ module RailsParamValidation
23
23
  end
24
24
 
25
25
  def to_openapi
26
- ValidatorFactory.create(CLASS_MAP.fetch(schema.class, schema.class)).to_openapi.merge(enum: [schema])
26
+ ValidatorFactory.create(CLASS_MAP.fetch(schema.class, schema.class), collection).to_openapi.merge(enum: [schema])
27
27
  end
28
28
  end
29
29
 
@@ -32,8 +32,8 @@ module RailsParamValidation
32
32
  ![String, Symbol, Numeric, TrueClass, FalseClass].detect { |klass| schema.is_a? klass }.nil?
33
33
  end
34
34
 
35
- def create(schema)
36
- ConstantValidator.new schema
35
+ def create(schema, collection)
36
+ ConstantValidator.new schema, collection
37
37
  end
38
38
  end
39
39
 
@@ -1,12 +1,12 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class CustomTypeValidator < Validator
4
- def initialize(type)
5
- super type
4
+ def initialize(type, collection)
5
+ super type, collection
6
6
  end
7
7
 
8
8
  def matches?(path, data)
9
- ValidatorFactory.create(schema.schema).matches? path, data
9
+ ValidatorFactory.create(schema.schema, collection).matches? path, data
10
10
  end
11
11
 
12
12
  def to_openapi
@@ -19,8 +19,8 @@ module RailsParamValidation
19
19
  schema.is_a? AnnotationTypes::CustomT
20
20
  end
21
21
 
22
- def create(schema)
23
- CustomTypeValidator.new schema
22
+ def create(schema, collection)
23
+ CustomTypeValidator.new schema, collection
24
24
  end
25
25
  end
26
26
 
@@ -1,8 +1,8 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class DateValidator < Validator
4
- def initialize(schema)
5
- super schema
4
+ def initialize(schema, collection)
5
+ super schema, collection
6
6
  end
7
7
 
8
8
  def matches?(path, data)
@@ -31,8 +31,8 @@ module RailsParamValidation
31
31
  schema == Date
32
32
  end
33
33
 
34
- def create(schema)
35
- DateValidator.new schema
34
+ def create(schema, collection)
35
+ DateValidator.new schema, collection
36
36
  end
37
37
  end
38
38
 
@@ -1,8 +1,8 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class DateTimeValidator < Validator
4
- def initialize(schema)
5
- super schema
4
+ def initialize(schema, collection)
5
+ super schema, collection
6
6
  end
7
7
 
8
8
  def matches?(path, data)
@@ -32,7 +32,7 @@ module RailsParamValidation
32
32
  end
33
33
 
34
34
  def create(schema)
35
- DateTimeValidator.new schema
35
+ DateTimeValidator.new schema, collection
36
36
  end
37
37
  end
38
38
 
@@ -1,8 +1,8 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class FloatValidator < Validator
4
- def initialize(schema)
5
- super schema
4
+ def initialize(schema, collection)
5
+ super schema, collection
6
6
  end
7
7
 
8
8
  def matches?(path, data)
@@ -31,8 +31,8 @@ module RailsParamValidation
31
31
  schema == Float
32
32
  end
33
33
 
34
- def create(schema)
35
- FloatValidator.new schema
34
+ def create(schema, collection)
35
+ FloatValidator.new schema, collection
36
36
  end
37
37
  end
38
38
 
@@ -2,11 +2,11 @@ module RailsParamValidation
2
2
 
3
3
  class HashValidator < Validator
4
4
  # @param [HashT] schema
5
- def initialize(schema)
6
- super schema
5
+ def initialize(schema, collection)
6
+ super schema, collection
7
7
 
8
- @value_validator = ValidatorFactory.create schema.inner_type
9
- @key_validator = ValidatorFactory.create schema.key_type
8
+ @value_validator = ValidatorFactory.create schema.inner_type, collection
9
+ @key_validator = ValidatorFactory.create schema.key_type, collection
10
10
  end
11
11
 
12
12
  def matches?(path, data)
@@ -44,8 +44,8 @@ module RailsParamValidation
44
44
  schema.is_a? AnnotationTypes::HashT
45
45
  end
46
46
 
47
- def create(schema)
48
- HashValidator.new schema
47
+ def create(schema, collection)
48
+ HashValidator.new schema, collection
49
49
  end
50
50
  end
51
51
 
@@ -1,8 +1,8 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class IntegerValidator < Validator
4
- def initialize(schema)
5
- super schema
4
+ def initialize(schema, collection)
5
+ super schema, collection
6
6
  end
7
7
 
8
8
  def matches?(path, data)
@@ -31,8 +31,8 @@ module RailsParamValidation
31
31
  schema == Integer
32
32
  end
33
33
 
34
- def create(schema)
35
- IntegerValidator.new schema
34
+ def create(schema, collection)
35
+ IntegerValidator.new schema, collection
36
36
  end
37
37
  end
38
38
 
@@ -2,10 +2,10 @@ module RailsParamValidation
2
2
 
3
3
  class ObjectValidator < Validator
4
4
  # @param [Hash] schema
5
- def initialize(schema)
6
- super schema
5
+ def initialize(schema, collection)
6
+ super schema, collection
7
7
 
8
- @inner_validators = schema.map { |key, value| [key, ValidatorFactory.create(value)] }.to_h
8
+ @inner_validators = schema.map { |key, value| [key, ValidatorFactory.create(value, collection)] }.to_h
9
9
  end
10
10
 
11
11
  def matches?(path, data)
@@ -55,8 +55,8 @@ module RailsParamValidation
55
55
  schema.is_a? Hash
56
56
  end
57
57
 
58
- def create(schema)
59
- ObjectValidator.new schema
58
+ def create(schema, collection)
59
+ ObjectValidator.new schema, collection
60
60
  end
61
61
  end
62
62
 
@@ -1,10 +1,10 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class OptionalValidator < Validator
4
- def initialize(schema)
5
- super schema
4
+ def initialize(schema, collection)
5
+ super schema, collection
6
6
 
7
- @inner_validator = ValidatorFactory.create schema.inner_type
7
+ @inner_validator = ValidatorFactory.create schema.inner_type, collection
8
8
  @default = schema.default
9
9
  end
10
10
 
@@ -36,8 +36,8 @@ module RailsParamValidation
36
36
  schema.is_a? AnnotationTypes::OptionalT
37
37
  end
38
38
 
39
- def create(schema)
40
- OptionalValidator.new schema
39
+ def create(schema, collection)
40
+ OptionalValidator.new schema, collection
41
41
  end
42
42
  end
43
43
 
@@ -1,8 +1,8 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class RegexValidator < Validator
4
- def initialize(schema)
5
- super schema
4
+ def initialize(schema, collection)
5
+ super schema, collection
6
6
  end
7
7
 
8
8
  def matches?(path, data)
@@ -29,8 +29,8 @@ module RailsParamValidation
29
29
  schema.is_a? Regexp
30
30
  end
31
31
 
32
- def create(schema)
33
- RegexValidator.new schema
32
+ def create(schema, collection)
33
+ RegexValidator.new schema, collection
34
34
  end
35
35
  end
36
36
 
@@ -1,8 +1,8 @@
1
1
  module RailsParamValidation
2
2
 
3
3
  class StringValidator < Validator
4
- def initialize(schema)
5
- super schema
4
+ def initialize(schema, collection)
5
+ super schema, collection
6
6
  end
7
7
 
8
8
  def matches?(path, data)
@@ -23,8 +23,8 @@ module RailsParamValidation
23
23
  schema == String || schema == Symbol
24
24
  end
25
25
 
26
- def create(schema)
27
- StringValidator.new schema
26
+ def create(schema, collection)
27
+ StringValidator.new schema, collection
28
28
  end
29
29
  end
30
30
 
@@ -3,8 +3,8 @@ module RailsParamValidation
3
3
  class UuidValidator < Validator
4
4
  REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
5
5
 
6
- def initialize(schema)
7
- super schema
6
+ def initialize(schema, collection)
7
+ super schema, collection
8
8
  end
9
9
 
10
10
  def matches?(path, data)
@@ -31,8 +31,8 @@ module RailsParamValidation
31
31
  schema == Uuid
32
32
  end
33
33
 
34
- def create(schema)
35
- UuidValidator.new schema
34
+ def create(schema, collection)
35
+ UuidValidator.new schema, collection
36
36
  end
37
37
  end
38
38
 
@@ -1,3 +1,3 @@
1
1
  module RailsParamValidation
2
- VERSION = "0.4.14"
2
+ VERSION = "0.5.0"
3
3
  end
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.4.14
4
+ version: 0.5.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-05-25 00:00:00.000000000 Z
11
+ date: 2023-10-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Declarative parameter definition and validation for Rails
14
14
  email:
@@ -78,7 +78,7 @@ metadata:
78
78
  homepage_uri: https://okirmis.github.io/rails-param-validation
79
79
  source_code_uri: https://github.com/okirmis/rails-param-validation
80
80
  changelog_uri: https://github.com/okirmis/rails-param-validation
81
- post_install_message:
81
+ post_install_message:
82
82
  rdoc_options: []
83
83
  require_paths:
84
84
  - lib
@@ -93,8 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubygems_version: 3.4.12
97
- signing_key:
96
+ rubygems_version: 3.3.26
97
+ signing_key:
98
98
  specification_version: 4
99
99
  summary: Declarative parameter definition and validation for Rails
100
100
  test_files: []