Commit 0c13730b authored by Stan Hu's avatar Stan Hu

Merge branch 'change-json-validation' into 'master'

Use json schemer gem for all schemas

See merge request gitlab-org/gitlab!56745
parents 2a7c3adf 0e892aaf
...@@ -414,6 +414,7 @@ group :development, :test, :omnibus do ...@@ -414,6 +414,7 @@ group :development, :test, :omnibus do
end end
group :test do group :test do
gem 'json-schema', '~> 2.8.0'
gem 'fuubar', '~> 2.2.0' gem 'fuubar', '~> 2.2.0'
gem 'rspec-retry', '~> 0.6.1' gem 'rspec-retry', '~> 0.6.1'
gem 'rspec_profiling', '~> 0.0.6' gem 'rspec_profiling', '~> 0.0.6'
...@@ -520,7 +521,6 @@ gem 'valid_email', '~> 0.1' ...@@ -520,7 +521,6 @@ gem 'valid_email', '~> 0.1'
# JSON # JSON
gem 'json', '~> 2.3.0' gem 'json', '~> 2.3.0'
gem 'json-schema', '~> 2.8.0'
gem 'json_schemer', '~> 0.2.12' gem 'json_schemer', '~> 0.2.12'
gem 'oj', '~> 3.10.6' gem 'oj', '~> 3.10.6'
gem 'multi_json', '~> 1.14.1' gem 'multi_json', '~> 1.14.1'
......
...@@ -643,7 +643,7 @@ GEM ...@@ -643,7 +643,7 @@ GEM
activesupport (>= 4.2) activesupport (>= 4.2)
aes_key_wrap aes_key_wrap
bindata bindata
json-schema (2.8.0) json-schema (2.8.1)
addressable (>= 2.4) addressable (>= 2.4)
json_schemer (0.2.12) json_schemer (0.2.12)
ecma-re-validator (~> 0.2) ecma-re-validator (~> 0.2)
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
class JsonSchemaValidator < ActiveModel::EachValidator 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
BASE_DIRECTORY = %w(app validators json_schemas).freeze BASE_DIRECTORY = %w(app validators json_schemas).freeze
def initialize(options) def initialize(options)
...@@ -35,11 +34,11 @@ class JsonSchemaValidator < ActiveModel::EachValidator ...@@ -35,11 +34,11 @@ class JsonSchemaValidator < ActiveModel::EachValidator
attr_reader :base_directory attr_reader :base_directory
def valid_schema?(value) def valid_schema?(value)
if draft_version > JSON_VALIDATOR_MAX_DRAFT_VERSION validator.valid?(value)
JSONSchemer.schema(Pathname.new(schema_path)).valid?(value) end
else
JSON::Validator.validate(schema_path, value) def validator
end @validator ||= JSONSchemer.schema(Pathname.new(schema_path))
end end
def schema_path def schema_path
......
{ {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Kroki formats", "description": "Kroki formats",
"type": "object", "type": "object",
"properties": { "properties": {
......
{ {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "CI builds metadata secrets", "description": "CI builds metadata secrets",
"type": "object", "type": "object",
"patternProperties": { "patternProperties": {
......
{ {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Build report result data", "description": "Build report result data",
"type": "object", "type": "object",
"properties": { "properties": {
......
{ {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Build report result data tests", "description": "Build report result data tests",
"type": "object", "type": "object",
"properties": { "properties": {
......
{ {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Codequality used by codeclimate parser", "description": "Codequality used by codeclimate parser",
"type": "object", "type": "object",
"required": ["description", "fingerprint", "severity", "location"], "required": ["description", "fingerprint", "severity", "location"],
......
{ {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Daily build group report result data", "description": "Daily build group report result data",
"type": "object", "type": "object",
"properties": { "properties": {
......
{ {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Debian fields", "description": "Debian fields",
"type": "object", "type": "object",
"patternProperties": { "patternProperties": {
......
{ {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Git trailer key/value pairs", "description": "Git trailer key/value pairs",
"type": "object", "type": "object",
"patternProperties": { "patternProperties": {
......
{ {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object", "type": "object",
"patternProperties": { "patternProperties": {
".*": { ".*": {
......
{ {
"$schema": "http://json-schema.org/draft-07/schema#",
"global": [ "global": [
{ {
"field" : "SECURE_ANALYZERS_PREFIX", "field" : "SECURE_ANALYZERS_PREFIX",
......
---
title: Stop using json-schema gem for production
merge_request: 56745
author:
type: other
...@@ -7,11 +7,12 @@ RSpec.describe JsonSchemaValidator do ...@@ -7,11 +7,12 @@ RSpec.describe JsonSchemaValidator do
let(:test_value) { 'bar' } let(:test_value) { 'bar' }
let(:mock_subject) { double(:subject) } let(:mock_subject) { double(:subject) }
let(:validator) { described_class.new(attributes: [:foo], filename: schema_name, base_directory: %w(spec fixtures)) } let(:validator) { described_class.new(attributes: [:foo], filename: schema_name, base_directory: %w(spec fixtures)) }
let(:fake_draft) { double('Draft7', valid?: true) }
subject(:validate_subject) { validator.validate_each(mock_subject, :foo, test_value) } subject(:validate_subject) { validator.validate_each(mock_subject, :foo, test_value) }
before do before do
allow(JSON::Validator).to receive(:validate).and_return(true) allow(JSONSchemer).to receive(:schema).and_return(fake_draft)
end end
context 'when the schema file exists on CE' do context 'when the schema file exists on CE' do
...@@ -21,7 +22,7 @@ RSpec.describe JsonSchemaValidator do ...@@ -21,7 +22,7 @@ RSpec.describe JsonSchemaValidator do
it 'calls the validator with CE schema' do it 'calls the validator with CE schema' do
validate_subject validate_subject
expect(JSON::Validator).to have_received(:validate).with(schema_path, test_value) expect(fake_draft).to have_received(:valid?)
end end
end end
...@@ -32,7 +33,7 @@ RSpec.describe JsonSchemaValidator do ...@@ -32,7 +33,7 @@ RSpec.describe JsonSchemaValidator do
it 'calls the validator with EE schema' do it 'calls the validator with EE schema' do
validate_subject validate_subject
expect(JSON::Validator).to have_received(:validate).with(schema_path, test_value) expect(fake_draft).to have_received(:valid?)
end end
end end
end end
......
...@@ -32,9 +32,8 @@ module Gitlab ...@@ -32,9 +32,8 @@ module Gitlab
private private
def valid_degradation?(degradation) def valid_degradation?(degradation)
JSON::Validator.validate!(CODECLIMATE_SCHEMA_PATH, degradation) JSONSchemer.schema(Pathname.new(CODECLIMATE_SCHEMA_PATH)).valid?(degradation)
rescue JSON::Schema::ValidationError => e rescue StandardError => _
set_error_message("Invalid degradation format: #{e.message}")
false false
end end
end end
......
...@@ -131,7 +131,6 @@ RSpec.describe Gitlab::Ci::Parsers::Codequality::CodeClimate do ...@@ -131,7 +131,6 @@ RSpec.describe Gitlab::Ci::Parsers::Codequality::CodeClimate do
expect { parse }.not_to raise_error expect { parse }.not_to raise_error
expect(codequality_report.degradations_count).to eq(0) expect(codequality_report.degradations_count).to eq(0)
expect(codequality_report.error_message).to eq("Invalid degradation format: The property '#/' did not contain a required property of 'location'")
end end
end end
end end
......
...@@ -34,8 +34,6 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReports do ...@@ -34,8 +34,6 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReports do
it 'sets location as an error' do it 'sets location as an error' do
codequality_report.add_degradation(invalid_degradation) codequality_report.add_degradation(invalid_degradation)
expect(codequality_report.error_message).to eq("Invalid degradation format: The property '#/' did not contain a required property of 'location'")
end end
end end
end end
......
...@@ -29,36 +29,6 @@ RSpec.describe JsonSchemaValidator do ...@@ -29,36 +29,6 @@ RSpec.describe JsonSchemaValidator do
expect(build_report_result.errors.full_messages).to eq(["Data must be a valid json schema"]) expect(build_report_result.errors.full_messages).to eq(["Data must be a valid json schema"])
end end
end end
context 'when draft is > 4' do
let(:validator) { described_class.new(attributes: [:data], filename: "build_report_result_data", draft: 6) }
it 'uses JSONSchemer to perform validations' do
expect(JSONSchemer).to receive(:schema).with(Pathname.new(Rails.root.join('app', 'validators', 'json_schemas', 'build_report_result_data.json').to_s)).and_call_original
subject
end
end
context 'when draft is <= 4' do
let(:validator) { described_class.new(attributes: [:data], filename: "build_report_result_data", draft: 4) }
it 'uses JSON::Validator to perform validations' do
expect(JSON::Validator).to receive(:validate).with(Rails.root.join('app', 'validators', 'json_schemas', 'build_report_result_data.json').to_s, build_report_result.data)
subject
end
end
context 'when draft value is not provided' do
let(:validator) { described_class.new(attributes: [:data], filename: "build_report_result_data") }
it 'uses JSON::Validator to perform validations' do
expect(JSON::Validator).to receive(:validate).with(Rails.root.join('app', 'validators', 'json_schemas', 'build_report_result_data.json').to_s, build_report_result.data)
subject
end
end
end end
context 'when filename is not set' do context 'when filename is not set' do
......
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