graphql-constraint-directive 0.1.2 → 0.1.3
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/graphql/constraint/directive/constraint.rb +29 -10
- data/lib/graphql/constraint/directive/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fc00580a0d30e9c6b2e714f5f3f3265c316641f1a3ec8813279940f6f5ae2c68
|
|
4
|
+
data.tar.gz: c7abbd8060dda8b2d68a28647880dd96bf515a7e3b6c3c0cf82e7824386405ce
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e7e5c2687a55f5cec943f240fb9569bf798407f5771d71a128ab44f577676c04265ccb6e5ee9266bae8c859a8088b14ed0a366554e16a1e5934804a036045ffc
|
|
7
|
+
data.tar.gz: 97a8f96e18f95df10d69ed98ad33692b9aceae9d06c6a87fc47ff02d81ddfb42c08e68361d5a6e72df985a9efd43383875aa0c1d4c68d1625e2fd444c333b315
|
data/Gemfile.lock
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
34
|
-
|
|
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
|
-
|
|
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
|
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.
|
|
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-
|
|
11
|
+
date: 2023-01-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: graphql
|