File: has_validators.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (57 lines) | stat: -rw-r--r-- 1,568 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true
module GraphQL
  class Schema
    class Member
      module HasValidators
        include GraphQL::EmptyObjects

        # Build {GraphQL::Schema::Validator}s based on the given configuration
        # and use them for this schema member
        # @param validation_config [Hash{Symbol => Hash}]
        # @return [void]
        def validates(validation_config)
          new_validators = GraphQL::Schema::Validator.from_config(self, validation_config)
          @own_validators ||= []
          @own_validators.concat(new_validators)
          nil
        end

        # @return [Array<GraphQL::Schema::Validator>]
        def validators
          @own_validators || EMPTY_ARRAY
        end

        module ClassConfigured
          def inherited(child_cls)
            super
            child_cls.extend(ClassValidators)
          end

          module ClassValidators
            include GraphQL::EmptyObjects

            def validators
              inherited_validators = superclass.validators
              if inherited_validators.any?
                if @own_validators.nil?
                  inherited_validators
                else
                  inherited_validators + @own_validators
                end
              elsif @own_validators.nil?
                EMPTY_ARRAY
              else
                @own_validators
              end
            end
          end
        end

        def self.extended(child_cls)
          super
          child_cls.extend(ClassConfigured)
        end
      end
    end
  end
end