blob: f661392082f7e75015bfc232da48a1f55bf722cb (
plain)
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
|
# frozen_string_literal: true
#
# JsonSchemaValidator
#
# Custom validator for json schema.
# Create a json schema within the json_schemas directory
#
# class Project < ActiveRecord::Base
# validates :data, json_schema: { filename: "file" }
# end
#
class JsonSchemaValidator < ActiveModel::EachValidator
FILENAME_ALLOWED = /\A[a-z0-9_-]*\Z/
FilenameError = Class.new(StandardError)
BASE_DIRECTORY = %w[app validators json_schemas].freeze
def initialize(options)
raise ArgumentError, "Expected 'filename' as an argument" unless options[:filename]
raise FilenameError, "Must be a valid 'filename'" unless options[:filename].match?(FILENAME_ALLOWED)
@base_directory = options.delete(:base_directory) || BASE_DIRECTORY
super(options)
end
def validate_each(record, attribute, value)
value = value.to_h.deep_stringify_keys if options[:hash_conversion] == true
value = Gitlab::Json.parse(value.to_s) if options[:parse_json] == true && !value.nil?
if options[:detail_errors]
validator.validate(value).each do |error|
message = format(
_("'%{data_pointer}' must be a valid '%{type}'"),
data_pointer: error['data_pointer'], type: error['type']
)
record.errors.add(attribute, message)
end
else
record.errors.add(attribute, _("must be a valid json schema")) unless valid_schema?(value)
end
end
private
attr_reader :base_directory
def valid_schema?(value)
validator.valid?(value)
end
def validator
@validator ||= JSONSchemer.schema(Pathname.new(schema_path))
end
def schema_path
@schema_path ||= Rails.root.join(*base_directory, filename_with_extension).to_s
end
def filename_with_extension
"#{options[:filename]}.json"
end
def draft_version
options[:draft] || JSON_VALIDATOR_MAX_DRAFT_VERSION
end
end
JsonSchemaValidator.prepend_mod
|