Commit d0f4d241 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'support_ee_schemas' into 'master'

Support JSON schemas under the `ee` directory for JsonSchemaValidator

See merge request gitlab-org/gitlab!56368
parents 80cf476e 8e5bac39
...@@ -13,11 +13,14 @@ class JsonSchemaValidator < ActiveModel::EachValidator ...@@ -13,11 +13,14 @@ class JsonSchemaValidator < ActiveModel::EachValidator
FILENAME_ALLOWED = /\A[a-z0-9_-]*\Z/.freeze FILENAME_ALLOWED = /\A[a-z0-9_-]*\Z/.freeze
FilenameError = Class.new(StandardError) FilenameError = Class.new(StandardError)
JSON_VALIDATOR_MAX_DRAFT_VERSION = 4 JSON_VALIDATOR_MAX_DRAFT_VERSION = 4
BASE_DIRECTORY = %w(app validators json_schemas).freeze
def initialize(options) def initialize(options)
raise ArgumentError, "Expected 'filename' as an argument" unless options[:filename] raise ArgumentError, "Expected 'filename' as an argument" unless options[:filename]
raise FilenameError, "Must be a valid 'filename'" unless options[:filename].match?(FILENAME_ALLOWED) raise FilenameError, "Must be a valid 'filename'" unless options[:filename].match?(FILENAME_ALLOWED)
@base_directory = options.delete(:base_directory) || BASE_DIRECTORY
super(options) super(options)
end end
...@@ -29,6 +32,8 @@ class JsonSchemaValidator < ActiveModel::EachValidator ...@@ -29,6 +32,8 @@ class JsonSchemaValidator < ActiveModel::EachValidator
private private
attr_reader :base_directory
def valid_schema?(value) def valid_schema?(value)
if draft_version > JSON_VALIDATOR_MAX_DRAFT_VERSION if draft_version > JSON_VALIDATOR_MAX_DRAFT_VERSION
JSONSchemer.schema(Pathname.new(schema_path)).valid?(value) JSONSchemer.schema(Pathname.new(schema_path)).valid?(value)
...@@ -38,10 +43,16 @@ class JsonSchemaValidator < ActiveModel::EachValidator ...@@ -38,10 +43,16 @@ class JsonSchemaValidator < ActiveModel::EachValidator
end end
def schema_path def schema_path
Rails.root.join('app', 'validators', 'json_schemas', "#{options[:filename]}.json").to_s @schema_path ||= Rails.root.join(*base_directory, filename_with_extension).to_s
end
def filename_with_extension
"#{options[:filename]}.json"
end end
def draft_version def draft_version
options[:draft] || JSON_VALIDATOR_MAX_DRAFT_VERSION options[:draft] || JSON_VALIDATOR_MAX_DRAFT_VERSION
end end
end end
JsonSchemaValidator.prepend_ee_mod
# frozen_string_literal: true
module EE
module JsonSchemaValidator
private
def schema_path
@schema_path ||= begin
if File.exist?(super)
super
else
Rails.root.join('ee', *base_directory, filename_with_extension).to_s
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe JsonSchemaValidator do
describe '#validates_each' do
let(:test_value) { 'bar' }
let(:mock_subject) { double(:subject) }
let(:validator) { described_class.new(attributes: [:foo], filename: schema_name, base_directory: %w(spec fixtures)) }
subject(:validate_subject) { validator.validate_each(mock_subject, :foo, test_value) }
before do
allow(JSON::Validator).to receive(:validate).and_return(true)
end
context 'when the schema file exists on CE' do
let(:schema_name) { 'ce_sample_schema' }
let(:schema_path) { Rails.root.join('spec', 'fixtures', 'ce_sample_schema.json').to_s }
it 'calls the validator with CE schema' do
validate_subject
expect(JSON::Validator).to have_received(:validate).with(schema_path, test_value)
end
end
context 'when the schema file exists on EE' do
let(:schema_name) { 'ee_sample_schema' }
let(:schema_path) { Rails.root.join('ee', 'spec', 'fixtures', 'ee_sample_schema.json').to_s }
it 'calls the validator with EE schema' do
validate_subject
expect(JSON::Validator).to have_received(:validate).with(schema_path, test_value)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment