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
end
group :test do
gem 'json-schema', '~> 2.8.0'
gem 'fuubar', '~> 2.2.0'
gem 'rspec-retry', '~> 0.6.1'
gem 'rspec_profiling', '~> 0.0.6'
......@@ -520,7 +521,6 @@ gem 'valid_email', '~> 0.1'
# JSON
gem 'json', '~> 2.3.0'
gem 'json-schema', '~> 2.8.0'
gem 'json_schemer', '~> 0.2.12'
gem 'oj', '~> 3.10.6'
gem 'multi_json', '~> 1.14.1'
......
......@@ -643,7 +643,7 @@ GEM
activesupport (>= 4.2)
aes_key_wrap
bindata
json-schema (2.8.0)
json-schema (2.8.1)
addressable (>= 2.4)
json_schemer (0.2.12)
ecma-re-validator (~> 0.2)
......
......@@ -12,7 +12,6 @@
class JsonSchemaValidator < ActiveModel::EachValidator
FILENAME_ALLOWED = /\A[a-z0-9_-]*\Z/.freeze
FilenameError = Class.new(StandardError)
JSON_VALIDATOR_MAX_DRAFT_VERSION = 4
BASE_DIRECTORY = %w(app validators json_schemas).freeze
def initialize(options)
......@@ -35,11 +34,11 @@ class JsonSchemaValidator < ActiveModel::EachValidator
attr_reader :base_directory
def valid_schema?(value)
if draft_version > JSON_VALIDATOR_MAX_DRAFT_VERSION
JSONSchemer.schema(Pathname.new(schema_path)).valid?(value)
else
JSON::Validator.validate(schema_path, value)
end
validator.valid?(value)
end
def validator
@validator ||= JSONSchemer.schema(Pathname.new(schema_path))
end
def schema_path
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Kroki formats",
"type": "object",
"properties": {
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "CI builds metadata secrets",
"type": "object",
"patternProperties": {
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Build report result data",
"type": "object",
"properties": {
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Build report result data tests",
"type": "object",
"properties": {
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Codequality used by codeclimate parser",
"type": "object",
"required": ["description", "fingerprint", "severity", "location"],
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Daily build group report result data",
"type": "object",
"properties": {
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Debian fields",
"type": "object",
"patternProperties": {
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Git trailer key/value pairs",
"type": "object",
"patternProperties": {
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"patternProperties": {
".*": {
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"global": [
{
"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
let(:test_value) { 'bar' }
let(:mock_subject) { double(:subject) }
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) }
before do
allow(JSON::Validator).to receive(:validate).and_return(true)
allow(JSONSchemer).to receive(:schema).and_return(fake_draft)
end
context 'when the schema file exists on CE' do
......@@ -21,7 +22,7 @@ RSpec.describe JsonSchemaValidator do
it 'calls the validator with CE schema' do
validate_subject
expect(JSON::Validator).to have_received(:validate).with(schema_path, test_value)
expect(fake_draft).to have_received(:valid?)
end
end
......@@ -32,7 +33,7 @@ RSpec.describe JsonSchemaValidator do
it 'calls the validator with EE schema' do
validate_subject
expect(JSON::Validator).to have_received(:validate).with(schema_path, test_value)
expect(fake_draft).to have_received(:valid?)
end
end
end
......
......@@ -32,9 +32,8 @@ module Gitlab
private
def valid_degradation?(degradation)
JSON::Validator.validate!(CODECLIMATE_SCHEMA_PATH, degradation)
rescue JSON::Schema::ValidationError => e
set_error_message("Invalid degradation format: #{e.message}")
JSONSchemer.schema(Pathname.new(CODECLIMATE_SCHEMA_PATH)).valid?(degradation)
rescue StandardError => _
false
end
end
......
......@@ -131,7 +131,6 @@ RSpec.describe Gitlab::Ci::Parsers::Codequality::CodeClimate do
expect { parse }.not_to raise_error
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
......
......@@ -34,8 +34,6 @@ RSpec.describe Gitlab::Ci::Reports::CodequalityReports do
it 'sets location as an error' do
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
......
......@@ -29,36 +29,6 @@ RSpec.describe JsonSchemaValidator do
expect(build_report_result.errors.full_messages).to eq(["Data must be a valid json schema"])
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
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