@@ -40,42 +40,32 @@ class RequiredValidator < Validator
4040 # @param message [String]
4141 def initialize ( one_of : nil , argument : nil , allow_all_hidden : nil , message : nil , **default_options )
4242 @one_of = if one_of
43- @single_argument = false
4443 one_of
4544 elsif argument
46- @single_argument = true
4745 [ argument ]
4846 else
4947 raise ArgumentError , "`one_of:` or `argument:` must be given in `validates required: {...}`"
5048 end
51- @allow_all_hidden = allow_all_hidden . nil? ? @single_argument : allow_all_hidden
49+ @allow_all_hidden = allow_all_hidden . nil? ? !! argument : allow_all_hidden
5250 @message = message
5351 super ( **default_options )
5452 end
5553
5654 def validate ( _object , context , value )
5755 fully_matched_conditions = 0
5856 partially_matched_conditions = 0
59- if @one_of
60- visible_keywords = context . types . arguments ( @validated ) . map ( &:keyword )
61- visible_one_ofs = @one_of & visible_keywords
62- if visible_one_ofs . empty?
63- if @allow_all_hidden
64- return nil
65- else
66- raise GraphQL ::Error , <<~ERR
67- #{ @validated . path } validates `required: ...` but all required arguments were hidden.
6857
69- Update your schema definition to allow the client to see some fields or skip validation by adding `required: { ..., allow_all_hidden: true }`
70- ERR
71- end
72- end
73- end
58+ visible_keywords = context . types . arguments ( @validated ) . map ( &:keyword )
59+ no_visible_conditions = true
7460
7561 if !value . nil?
7662 @one_of . each do |one_of_condition |
7763 case one_of_condition
7864 when Symbol
65+ if no_visible_conditions && visible_keywords . include? ( one_of_condition )
66+ no_visible_conditions = false
67+ end
68+
7969 if value . key? ( one_of_condition )
8070 fully_matched_conditions += 1
8171 end
@@ -84,6 +74,9 @@ def validate(_object, context, value)
8474 full_match = true
8575
8676 one_of_condition . each do |k |
77+ if no_visible_conditions && visible_keywords . include? ( k )
78+ no_visible_conditions = false
79+ end
8780 if value . key? ( k )
8881 any_match = true
8982 else
@@ -106,6 +99,18 @@ def validate(_object, context, value)
10699 end
107100 end
108101
102+ if no_visible_conditions
103+ if @allow_all_hidden
104+ return nil
105+ else
106+ raise GraphQL ::Error , <<~ERR
107+ #{ @validated . path } validates `required: ...` but all required arguments were hidden.
108+
109+ Update your schema definition to allow the client to see some fields or skip validation by adding `required: { ..., allow_all_hidden: true }`
110+ ERR
111+ end
112+ end
113+
109114 if fully_matched_conditions == 1 && partially_matched_conditions == 0
110115 nil # OK
111116 else
0 commit comments