Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use json_schemer to validate schema #4583

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ gem 'image_processing', '~> 1.12.2'
gem 'bootsnap', '~> 1.16.0', require: false

# used to validate container responses
gem 'json-schema', '~> 3.0.0'
gem 'json_schemer', '~> 0.2.24'

# delayed jobs
gem 'delayed_job_active_record', '~> 4.1.7'
Expand Down
13 changes: 10 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ GEM
docker-api (2.2.0)
excon (>= 0.47.0)
multi_json
ecma-re-validator (0.4.0)
regexp_parser (~> 2.2)
ed25519 (1.3.0)
erubi (1.12.0)
exception_notification (4.5.0)
Expand All @@ -185,6 +187,7 @@ GEM
glob (0.4.0)
globalid (1.1.0)
activesupport (>= 5.0)
hana (1.3.7)
has_scope (0.8.1)
actionpack (>= 5.2)
activesupport (>= 5.2)
Expand Down Expand Up @@ -220,8 +223,11 @@ GEM
bindata
faraday (~> 2.0)
faraday-follow_redirects
json-schema (3.0.0)
addressable (>= 2.8)
json_schemer (0.2.24)
ecma-re-validator (~> 0.3)
hana (~> 1.3)
regexp_parser (~> 2.0)
uri_template (~> 0.7)
jwt (2.7.0)
kramdown (2.4.0)
rexml
Expand Down Expand Up @@ -466,6 +472,7 @@ GEM
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
uri_template (0.7.0)
validate_email (0.1.6)
activemodel (>= 3.0)
mail (>= 2.2.5)
Expand Down Expand Up @@ -536,7 +543,7 @@ DEPENDENCIES
image_processing (~> 1.12.2)
jbuilder (~> 2.11.5)
jsbundling-rails (~> 1.1.1)
json-schema (~> 3.0.0)
json_schemer (~> 0.2.24)
jwt (~> 2.7.0)
kramdown (~> 2.4.0)
kramdown-parser-gfm (~> 1.1.0)
Expand Down
13 changes: 8 additions & 5 deletions app/runners/result_constructor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'json' # JSON support
jorg-vr marked this conversation as resolved.
Show resolved Hide resolved
require 'json-schema' # json schema validation, from json-schema gem
require 'json_schemer' # json schema validation

class ResultConstructorError < StandardError
attr_accessor :title, :description
Expand All @@ -12,8 +12,8 @@ def initialize(title, description = nil)
end

class ResultConstructor
FULL_SCHEMA = JSON.parse(Rails.public_path.join('schemas/judge_output.json').read)
PART_SCHEMA = JSON.parse(Rails.public_path.join('schemas/partial_output.json').read)
FULL_SCHEMER = JSONSchemer.schema(Rails.public_path.join('schemas/judge_output.json'))
PART_SCHEMER = JSONSchemer.schema(Rails.public_path.join('schemas/partial_output.json'))

LEVELSA = %i[judgement tab context testcase test].freeze
LEVELSH = { judgement: 0, tab: 1, context: 2, testcase: 3, test: 4 }.freeze
Expand All @@ -29,9 +29,12 @@ def feed(judge_output)
raise ResultConstructorError, 'No judge output' if judge_output.empty?

split_jsons(judge_output).each do |json|
if JSON::Validator.validate(PART_SCHEMA, json)
# Required by the gem. See issue below for context.
# https://github.com/davishmcclurg/json_schemer/issues/123
json_with_string_keys = json.deep_stringify_keys
if PART_SCHEMER.valid?(json_with_string_keys)
update(json)
elsif JSON::Validator.validate(FULL_SCHEMA, json)
elsif FULL_SCHEMER.valid?(json_with_string_keys)
@result = json
else
raise ResultConstructorError.new(
Expand Down