diff --git a/Gemfile b/Gemfile index ed2af8973..599c5d369 100644 --- a/Gemfile +++ b/Gemfile @@ -29,7 +29,7 @@ gem "discard" gem "activeadmin" # Admin charts gem "chartkick" -#Json Schema +# Json Schema gem "json-schema" gem "uk_postcode" diff --git a/db/migrate/20211112105348_add_incref_field.rb b/db/migrate/20211112105348_add_incref_field.rb index 65df53aca..ece9a458b 100644 --- a/db/migrate/20211112105348_add_incref_field.rb +++ b/db/migrate/20211112105348_add_incref_field.rb @@ -5,4 +5,3 @@ class AddIncrefField < ActiveRecord::Migration[6.1] end end end - diff --git a/lib/tasks/form_definition.rake b/lib/tasks/form_definition.rake index 565728064..c2b3c6103 100644 --- a/lib/tasks/form_definition.rake +++ b/lib/tasks/form_definition.rake @@ -2,49 +2,48 @@ require "json" require "json-schema" def get_all_form_paths(directories) - form_paths = [] - directories.each do |directory| - Dir.glob("#{directory}/*.json").each do |form_path| - form_paths.push(form_path) - end + form_paths = [] + directories.each do |directory| + Dir.glob("#{directory}/*.json").each do |form_path| + form_paths.push(form_path) end - form_paths + end + form_paths end namespace :form_definition do - desc "Validate JSON against Generic Form Schema" - task :validate do - puts "#{Rails.root}" - path = "config/forms/schema/generic.json" - - file = File.open(path) - schema = JSON.parse(file.read) - metaschema = JSON::Validator.validator_for_name("draft4").metaschema - - puts path - - if JSON::Validator.validate(metaschema, schema) - puts "schema valid" - else - puts "schema not valid" - return - end - - directories = ["config/forms", "spec/fixtures/forms"] - # directories = ["config/forms"] - - get_all_form_paths(directories).each do |path| - puts path - file = File.open(path) - data = JSON.parse(file.read) - - puts JSON::Validator.fully_validate(schema, data, :strict => true) - - begin - JSON::Validator.validate!(schema, data) - rescue JSON::Schema::ValidationError => e - e.message - end - end + 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) + schema = JSON.parse(file.read) + metaschema = JSON::Validator.validator_for_name("draft4").metaschema + + puts path + + if JSON::Validator.validate(metaschema, schema) + puts "schema valid" + else + puts "schema not valid" + return end - end \ No newline at end of file + + directories = ["config/forms", "spec/fixtures/forms"] + + get_all_form_paths(directories).each do |path| + puts path + file = File.open(path) + data = JSON.parse(file.read) + + puts JSON::Validator.fully_validate(schema, data, strict: true) + + begin + JSON::Validator.validate!(schema, data) + rescue JSON::Schema::ValidationError => e + e.message + end + end + end +end