graphql-constraint-directive 0.1.2 → 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: 46bdc345b3060ac6b9c965350a9c5f9928246bdeb1805975228f9f42f69f87e4
4
- data.tar.gz: af01415fd405937df061bd4dcc47a2f8a97c34f7d9b50de4e6446426940dc52b
3
+ metadata.gz: fc00580a0d30e9c6b2e714f5f3f3265c316641f1a3ec8813279940f6f5ae2c68
4
+ data.tar.gz: c7abbd8060dda8b2d68a28647880dd96bf515a7e3b6c3c0cf82e7824386405ce
5
5
  SHA512:
6
- metadata.gz: 6955147fb378004d1887725a8872b66b8a6fbbb94e7d45c0c4b948c2a3e58763299690b1367572b05f189b6a35f3dc6971e58b831337a2d65324ce91b0ecf690
7
- data.tar.gz: 453cf6b76b66e6b4fa2830f39cade3186737177b09102fa61a3ec235af8be098af4dfdd5a3baf6e25637afa7e84ae39999f2fb3cf3a43d2ff97ce9d19088c0ec
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.2)
4
+ graphql-constraint-directive (0.1.3)
5
5
  graphql (~> 2.0, < 3)
6
6
 
7
7
  GEM
@@ -1,17 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "graphql"
4
- require "byebug"
5
4
 
6
5
  module GraphQL
7
6
  module Constraint
8
7
  module Directive
9
8
  class Constraint < GraphQL::Schema::Directive
10
9
  LENGTH = { min_length: :minimum, max_length: :maximum }.freeze
10
+ NUMERICALITY = { min: :greater_than_or_equal_to, max: :less_than_or_equal_to }.freeze
11
11
 
12
12
  description "validate GraphQL fields"
13
13
  argument :max_length, Int, required: false, description: "validate max length for String"
14
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"
15
17
  locations INPUT_FIELD_DEFINITION
16
18
 
17
19
  def initialize(owner, **arguments)
@@ -22,21 +24,38 @@ module GraphQL
22
24
  private
23
25
 
24
26
  def validation_config
25
- {
26
- length: length_config
27
- }
27
+ {}.then { length_config.nil? ? _1 : _1.merge(length_config) }
28
+ .then { numericality_config.nil? ? _1 : _1.merge(numericality_config) }
28
29
  end
29
30
 
30
31
  def length_config
31
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?
45
+
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
32
52
 
33
- if owner_type != GraphQL::Types::String
34
- raise ArgumentError, <<~MD
35
- #{filtered_args.keys.join(", ")} in @constraint can't be attached to #{owner.graphql_name} because it has to be a String type.
36
- MD
37
- end
53
+ def assert_valid_owner_type_and_args(type, args)
54
+ return if owner_type == type
38
55
 
39
- filtered_args.transform_keys { |key| LENGTH[key] }
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
40
59
  end
41
60
 
42
61
  def owner_type
@@ -3,7 +3,7 @@
3
3
  module GraphQL
4
4
  module Constraint
5
5
  module Directive
6
- VERSION = "0.1.2"
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.2
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