graphql-constraint-directive 0.1.1 → 0.1.3

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: ba3f526ee2124471f6dfc59db75677018871fb2166a056def33c4436ac77bf56
4
- data.tar.gz: bcf163565e80cbc79b26ca429d540c7b0949281fe7fe2b86b5af3136b0b71932
3
+ metadata.gz: fc00580a0d30e9c6b2e714f5f3f3265c316641f1a3ec8813279940f6f5ae2c68
4
+ data.tar.gz: c7abbd8060dda8b2d68a28647880dd96bf515a7e3b6c3c0cf82e7824386405ce
5
5
  SHA512:
6
- metadata.gz: 917131726cb5381b676bd09c4c8e30a87a64b6bbc7674304396fd7da14979a7c6ee5243516d2a757f2bbd16809925c73eb083f027e9764f607f8666ab448826d
7
- data.tar.gz: 4f91c444e0013e31250d3cb431f745b9fc3e947063c706739ab86915a19009fcd3b459e417bf4ba4df6de3f7a5a3ba439a81b64f58354cfacd4c981530089a39
6
+ metadata.gz: e7e5c2687a55f5cec943f240fb9569bf798407f5771d71a128ab44f577676c04265ccb6e5ee9266bae8c859a8088b14ed0a366554e16a1e5934804a036045ffc
7
+ data.tar.gz: 97a8f96e18f95df10d69ed98ad33692b9aceae9d06c6a87fc47ff02d81ddfb42c08e68361d5a6e72df985a9efd43383875aa0c1d4c68d1625e2fd444c333b315
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphql-constraint-directive (0.1.1)
4
+ graphql-constraint-directive (0.1.3)
5
5
  graphql (~> 2.0, < 3)
6
6
 
7
7
  GEM
@@ -7,10 +7,13 @@ module GraphQL
7
7
  module Directive
8
8
  class Constraint < GraphQL::Schema::Directive
9
9
  LENGTH = { min_length: :minimum, max_length: :maximum }.freeze
10
+ NUMERICALITY = { min: :greater_than_or_equal_to, max: :less_than_or_equal_to }.freeze
10
11
 
11
12
  description "validate GraphQL fields"
12
13
  argument :max_length, Int, required: false, description: "validate max length for String"
13
14
  argument :min_length, Int, required: false, description: "validate min length for String"
15
+ argument :max, Int, required: false, description: "validate max length for Int"
16
+ argument :min, Int, required: false, description: "validate min length for Int"
14
17
  locations INPUT_FIELD_DEFINITION
15
18
 
16
19
  def initialize(owner, **arguments)
@@ -21,21 +24,42 @@ module GraphQL
21
24
  private
22
25
 
23
26
  def validation_config
24
- {
25
- length: length_config
26
- }
27
+ {}.then { length_config.nil? ? _1 : _1.merge(length_config) }
28
+ .then { numericality_config.nil? ? _1 : _1.merge(numericality_config) }
27
29
  end
28
30
 
29
31
  def length_config
30
32
  filtered_args = arguments.keyword_arguments.filter { |key, _| LENGTH.key?(key) }
33
+ return if filtered_args.empty?
34
+
35
+ assert_valid_owner_type_and_args(GraphQL::Types::String, filtered_args)
36
+
37
+ {
38
+ length: filtered_args.transform_keys { |key| LENGTH[key] }
39
+ }
40
+ end
41
+
42
+ def numericality_config
43
+ filtered_args = arguments.keyword_arguments.filter { |key, _| NUMERICALITY.key?(key) }
44
+ return if filtered_args.empty?
31
45
 
32
- if owner.type != GraphQL::Types::String
33
- raise ArgumentError, <<~MD
34
- #{filtered_args.keys.join(", ")} in @constraint can't be attached to #{owner.graphql_name} because it has to be a String type.
35
- MD
36
- end
46
+ assert_valid_owner_type_and_args(GraphQL::Types::Int, filtered_args)
47
+
48
+ {
49
+ numericality: filtered_args.transform_keys { |key| NUMERICALITY[key] }
50
+ }
51
+ end
52
+
53
+ def assert_valid_owner_type_and_args(type, args)
54
+ return if owner_type == type
55
+
56
+ raise ArgumentError, <<~MD
57
+ #{args.keys.join(", ")} in @constraint can't be attached to #{owner.graphql_name} because it has to be a #{owner_type} type.
58
+ MD
59
+ end
37
60
 
38
- filtered_args.transform_keys { |key| LENGTH[key] }
61
+ def owner_type
62
+ owner.type.non_null? ? owner.type.of_type : owner.type
39
63
  end
40
64
  end
41
65
  end
@@ -3,7 +3,7 @@
3
3
  module GraphQL
4
4
  module Constraint
5
5
  module Directive
6
- VERSION = "0.1.1"
6
+ VERSION = "0.1.3"
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-constraint-directive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - MH4GF
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-03 00:00:00.000000000 Z
11
+ date: 2023-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql