MadeTech Dushan
3 years ago
6 changed files with 58 additions and 59 deletions
@ -1,50 +1,51 @@ |
|||||||
require "json" |
require "json" |
||||||
require "json-schema" |
require "json-schema" |
||||||
|
# rubocop:disable Lint/ShadowingOuterLocalVariable |
||||||
def get_all_form_paths(directories) |
def get_all_form_paths(directories) |
||||||
form_paths = [] |
form_paths = [] |
||||||
directories.each do |directory| |
directories.each do |directory| |
||||||
Dir.glob("#{directory}/*.json").each do |form_path| |
Dir.glob("#{directory}/*.json").each do |form_path| |
||||||
form_paths.push(form_path) |
form_paths.push(form_path) |
||||||
end |
|
||||||
end |
end |
||||||
form_paths |
end |
||||||
|
form_paths |
||||||
end |
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 do |
task validate: :environment do |
||||||
puts "#{Rails.root}" |
puts Rails.root.to_s |
||||||
path = "config/forms/schema/generic.json" |
path = "config/forms/schema/generic.json" |
||||||
|
|
||||||
file = File.open(path) |
file = File.open(path) |
||||||
schema = JSON.parse(file.read) |
schema = JSON.parse(file.read) |
||||||
metaschema = JSON::Validator.validator_for_name("draft4").metaschema |
metaschema = JSON::Validator.validator_for_name("draft4").metaschema |
||||||
|
|
||||||
puts path |
puts path |
||||||
|
|
||||||
if JSON::Validator.validate(metaschema, schema) |
if JSON::Validator.validate(metaschema, schema) |
||||||
puts "schema valid" |
puts "schema valid" |
||||||
else |
else |
||||||
puts "schema not valid" |
puts "schema not valid" |
||||||
return |
return |
||||||
end |
end |
||||||
|
|
||||||
directories = ["config/forms", "spec/fixtures/forms"] |
directories = ["config/forms", "spec/fixtures/forms"] |
||||||
# directories = ["config/forms"] |
# directories = ["config/forms"] |
||||||
|
|
||||||
get_all_form_paths(directories).each do |path| |
get_all_form_paths(directories).each do |path| |
||||||
puts path |
puts path |
||||||
file = File.open(path) |
file = File.open(path) |
||||||
data = JSON.parse(file.read) |
data = JSON.parse(file.read) |
||||||
|
|
||||||
puts JSON::Validator.fully_validate(schema, data, :strict => true) |
puts JSON::Validator.fully_validate(schema, data, strict: true) |
||||||
|
|
||||||
begin |
begin |
||||||
JSON::Validator.validate!(schema, data) |
JSON::Validator.validate!(schema, data) |
||||||
rescue JSON::Schema::ValidationError => e |
rescue JSON::Schema::ValidationError => e |
||||||
e.message |
e.message |
||||||
end |
end |
||||||
end |
|
||||||
end |
end |
||||||
end |
end |
||||||
|
end |
||||||
|
# rubocop:enable Lint/ShadowingOuterLocalVariable |
||||||
|
Loading…
Reference in new issue