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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/graphql/constraint/directive/constraint.rb +33 -9
- 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
@@ -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
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
61
|
+
def owner_type
|
62
|
+
owner.type.non_null? ? owner.type.of_type : owner.type
|
39
63
|
end
|
40
64
|
end
|
41
65
|
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.
|
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
|