File: extensions_gallery_validator.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (70 lines) | stat: -rw-r--r-- 2,161 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
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

module WebIde
  module Settings
    class ExtensionsGalleryValidator
      include Messages

      # @param [Hash] context
      # @return [Gitlab::Fp::Result]
      def self.validate(context)
        unless context.fetch(:requested_setting_names).intersect?([:vscode_extensions_gallery])
          return Gitlab::Fp::Result.ok(context)
        end

        context => { settings: Hash => settings }
        settings => { vscode_extensions_gallery: Hash => vscode_extensions_gallery }

        # NOTE: We deep_stringify_keys here, so we can still pass keys as symbols during tests.
        #       This is the only place where keys need to be strings, because of the JSON schema
        #       validation, all other places we convert and work with the keys as symbols.
        errors = validate_against_schema(vscode_extensions_gallery.deep_stringify_keys)

        if errors.none?
          Gitlab::Fp::Result.ok(context)
        else
          Gitlab::Fp::Result.err(SettingsVscodeExtensionsGalleryValidationFailed.new(details: errors.join(". ")))
        end
      end

      # @param [Hash] hash_to_validate
      # @return [Array]
      def self.validate_against_schema(hash_to_validate)
        schema = {
          "required" =>
            %w[
              service_url
              item_url
              resource_url_template
            ],
          "properties" => {
            "service_url" => {
              "type" => "string"
            },
            "item_url" => {
              "type" => "string"
            },
            "resource_url_template" => {
              "type" => "string"
            },
            "control_url" => {
              "type" => "string"
            },
            "nls_base_url" => {
              "type" => "string"
            },
            "publisher_url" => {
              "type" => "string"
            }
          }
        }

        schemer = JSONSchemer.schema(schema)
        errors = schemer.validate(hash_to_validate)
        errors.map { |error| JSONSchemer::Errors.pretty(error) }
      end

      private_class_method :validate_against_schema
    end
  end
end