Skip to content

Commit

Permalink
refine validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
terrywbrady committed Dec 19, 2023
1 parent 1ef51ff commit 113cac6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ jobs:
run: bundle exec rubocop
working-directory: coll-health

# Install Ruby and run bundler
# Run the Rubocop linter checks
- name: 'Validate JSON schemas'
run: bundle exec ruby sample/ci_validate.rb
working-directory: coll-health

# Install Ruby and run bundler
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.0'
Expand Down
31 changes: 31 additions & 0 deletions coll-health-obj-analysis/sample/ci_validate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'json'
require 'json-schema'
require 'yaml'

@val_errors = {}

def validate(schema, doc, label)
stat = JSON::Validator.fully_validate(schema, doc)
return if stat.empty?
@val_errors[label] = stat
puts "Validation errors for #{label}"
stat.each do |s|
puts "- #{s}"
end
puts
end

schema_for_schema = JSON::Validator.validator_for_name('draft6').metaschema

obj_schema = JSON.parse(File.read('config/obj_schema.json'))
validate(schema_for_schema, obj_schema, 'Validate Object Schema')

yaml_schema = JSON.parse(YAML.safe_load(File.read('config/yaml_schema.yml'), aliases: true).to_json)
validate(schema_for_schema, yaml_schema, 'Validate Yaml Schema')

config = JSON.parse(YAML.safe_load(File.read('config/merritt_classifications.yml'), aliases: true).to_json)
validate(yaml_schema, config, 'Validate merritt_classifications.yml')

exit 1 unless @val_errors.empty?
21 changes: 11 additions & 10 deletions coll-health-obj-analysis/sample/validate_object.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
# frozen_string_literal: true

require 'json'
require 'json-schema'
require 'yaml'

obj = JSON.parse(File.read("sample/objects_details.json"))
schema = JSON.parse(File.read("config/obj_schema.json"))
obj = JSON.parse(File.read('sample/objects_details.json'))
schema = JSON.parse(File.read('config/obj_schema.json'))
stat = JSON::Validator.fully_validate(schema, obj)
puts "valid" if stat.empty?
puts 'valid' if stat.empty?
puts stat unless stat.empty?

obj = JSON.parse(File.read("sample/objects_details.bad.json"))
obj = JSON.parse(File.read('sample/objects_details.bad.json'))
stat = JSON::Validator.fully_validate(schema, obj)
puts "valid" if stat.empty?
puts 'valid' if stat.empty?
puts stat unless stat.empty?

schema_for_schema = JSON::Validator.validator_for_name('draft6').metaschema
stat = JSON::Validator.fully_validate(schema_for_schema, schema)
puts "valid" if stat.empty?
puts 'valid' if stat.empty?
puts stat unless stat.empty?

yaml_schema = JSON.parse(YAML.safe_load(File.read('config/yaml_schema.yml'), aliases: true).to_json)
config = JSON.parse(YAML.safe_load(File.read('config/merritt_classifications.yml'), aliases: true).to_json)
stat = JSON::Validator.fully_validate(schema_for_schema, schema)
puts "valid" if stat.empty?
stat = JSON::Validator.fully_validate(yaml_schema, config)
puts 'valid' if stat.empty?
puts stat unless stat.empty?

schema_for_schema = JSON::Validator.validator_for_name('draft6').metaschema
yaml_schema = JSON.parse(YAML.safe_load(File.read('config/yaml_schema.yml'), aliases: true).to_json)
stat = JSON::Validator.fully_validate(schema_for_schema, yaml_schema)
puts "valid" if stat.empty?
puts 'valid' if stat.empty?
puts stat unless stat.empty?

0 comments on commit 113cac6

Please sign in to comment.