Browse Source

Refactor

pull/88/head
baarkerlounger 3 years ago
parent
commit
0e88b5e703
  1. 9
      README.md
  2. 35
      lib/tasks/form_definition.rake
  3. 32
      spec/lib/tasks/form_definition_validator_spec.rb

9
README.md

@ -163,12 +163,13 @@ Assumptions made by the format:
## JSON Form Validation against Schema ## JSON Form Validation against Schema
To validate the form JSON against the schema you can run: To validate the form JSON against the schema you can run:
`rake form_definition:validate` `rake form_definition:validate["config/forms/2021_22.json"]`
This will validate all forms in: This will validate the given form definition against the schema in `config/forms/schema/generic.json`.
directories = ["config/forms", "spec/fixtures/forms"]
against the schema in (config/forms/schema/generic.json) You can also run:
`rake form_definition:validate_all`
This will validate all forms in directories = ["config/forms", "spec/fixtures/forms"]
## Useful documentation (external dependencies) ## Useful documentation (external dependencies)

35
lib/tasks/form_definition.rake

@ -13,37 +13,32 @@ end
namespace :form_definition do namespace :form_definition do
desc "Validate JSON against Generic Form Schema" desc "Validate JSON against Generic Form Schema"
task validate: :environment do
puts Rails.root.to_s
path = "config/forms/schema/generic.json"
file = File.open(path) task validate_all: :environment do
schema = JSON.parse(file.read) puts Rails.root.to_s
metaschema = JSON::Validator.validator_for_name("draft4").metaschema
puts path directories = ["config/forms", "spec/fixtures/forms"]
paths = get_all_form_paths(directories) + ["config/forms/schema/generic.json"]
if JSON::Validator.validate(metaschema, schema) paths.each do |path|
puts "schema valid" Rake::Task["form_definition:validate"].reenable
else Rake::Task["form_definition:validate"].invoke(path)
puts "schema not valid" end
return
end end
directories = ["config/forms", "spec/fixtures/forms"] task :validate, %i[path] => :environment do |_task, args|
path = Rails.root.join(args.path)
get_all_form_paths(directories).each do |path|
puts path
file = File.open(path) file = File.open(path)
data = JSON.parse(file.read) form_definition = JSON.parse(file.read)
schema = JSON::Validator.validator_for_name("draft4").metaschema
puts JSON::Validator.fully_validate(schema, data, strict: true) puts path
puts JSON::Validator.fully_validate(schema, form_definition, strict: true)
begin begin
JSON::Validator.validate!(schema, data) JSON::Validator.validate!(schema, form_definition)
rescue JSON::Schema::ValidationError => e rescue JSON::Schema::ValidationError => e
e.message e.message
end end
end end
end
end end

32
spec/lib/tasks/form_definition_validator_spec.rb

@ -0,0 +1,32 @@
require "rails_helper"
require "rake"
describe "rake form_definition:validate_all", type: :task do
subject(:task) { Rake::Task["form_definition:validate_all"] }
before do
Rake.application.rake_require("tasks/form_definition")
Rake::Task.define_task(:environment)
task.reenable
end
it "runs the validate task for each form definition in the project" do
expect(Rake::Task["form_definition:validate"]).to receive(:invoke).exactly(5).times
task.invoke
end
end
describe "rake form_definition:validate", type: :task do
subject(:task) { Rake::Task["form_definition:validate"] }
before do
Rake.application.rake_require("tasks/form_definition")
Rake::Task.define_task(:environment)
task.reenable
end
it "runs the validate task for the given form definition" do
expect(JSON::Validator).to receive(:validate!).at_least(1).time
task.invoke("config/forms/2021_2022.json")
end
end
Loading…
Cancel
Save