Method: GraphQL::Schema::Validator::RequiredValidator#validate

Defined in:
lib/graphql/schema/validator/required_validator.rb

#validate(_object, context, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/graphql/schema/validator/required_validator.rb', line 62

def validate(_object, context, value)
  fully_matched_conditions = 0
  partially_matched_conditions = 0

  visible_keywords = context.types.arguments(@validated).map(&:keyword)
  no_visible_conditions = true

  if !value.nil?
    @one_of.each do |one_of_condition|
      case one_of_condition
      when Symbol
        if no_visible_conditions && visible_keywords.include?(one_of_condition)
          no_visible_conditions = false
        end

        if value.key?(one_of_condition)
          fully_matched_conditions += 1
        end
      when Array
        any_match = false
        full_match = true

        one_of_condition.each do |k|
          if no_visible_conditions && visible_keywords.include?(k)
            no_visible_conditions = false
          end
          if value.key?(k)
            any_match = true
          else
            full_match = false
          end
        end

        partial_match = !full_match && any_match

        if full_match
          fully_matched_conditions += 1
        end

        if partial_match
          partially_matched_conditions += 1
        end
      else
        raise ArgumentError, "Unknown one_of condition: #{one_of_condition.inspect}"
      end
    end
  end

  if no_visible_conditions
    if @allow_all_hidden
      return nil
    else
      raise GraphQL::Error, "        \#{@validated.path} validates `required: ...` but all required arguments were hidden.\n\n        Update your schema definition to allow the client to see some fields or skip validation by adding `required: { ..., allow_all_hidden: true }`\n      ERR\n    end\n  end\n\n  if fully_matched_conditions == 1 && partially_matched_conditions == 0\n    nil # OK\n  else\n    @message || build_message(context)\n  end\nend\n"