From 6aef3f0900c735a75f8dc29668692a89949165c3 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Thu, 4 Nov 2021 13:30:20 +0000 Subject: [PATCH] JsonValidate_Initial --- app/helpers/json_schema_validation.rb | 88 +++++++++++++++++++++++++ spec/fixtures/forms/test_validator.json | 24 +++++++ 2 files changed, 112 insertions(+) create mode 100644 app/helpers/json_schema_validation.rb create mode 100644 spec/fixtures/forms/test_validator.json diff --git a/app/helpers/json_schema_validation.rb b/app/helpers/json_schema_validation.rb new file mode 100644 index 000000000..be0a4db3a --- /dev/null +++ b/app/helpers/json_schema_validation.rb @@ -0,0 +1,88 @@ +require "json-schema" +require "json" + +# "form_type": "lettings", +# "start_year": 2021, +# "end_year": 2022, +# "sections": { +# "about_this_log": { +# "label": "About this log", +# "subsections": { +# "about_this_log": { +# "label": "About this log", +# "pages": { + # "tenant_code": { + # "header": "", + # "description": "", + # "questions": { + # "tenant_code": { + # "check_answer_label": "Tenant code", + # "header": "What is the tenant code?", + # "hint_text": "", + # "type": "text" + # } + # } + # }, + +schema = { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://example.com/product.schema.json", + "title": "Form", + "description": "A form", + "type": "object", + "properties": { + "form_type": { + "description": "", + "type": "string" + }, + "start_year": { + "description": "", + "type": "int" + }, + "end_year": { + "description": "", + "type": "int" + }, + "sections": { + "description": "", + "type": "object", + "properties": { + "page_name": { + "description": "", + "type": "string" + }, + } + } + } +} + +begin + + # file = File.open("config/forms/2021_2022.json") + file = File.open("spec/fixtures/forms/test_validator.json") + data = JSON.parse(file.read) + + if JSON::Validator.validate!(schema, data) + puts "Success" + else + puts "Validation failed" + end + + begin + JSON::Validator.validate!(schema, data) + rescue JSON::Schema::ValidationError => e + e.message + end + + # def get_all_form_paths + # form_paths = [] + # directories = ["config/forms", "spec/fixtures/forms"] + # directories.each do |directory| + # Dir.glob("#{directory}/*.json").each do |form_path| + # form_path = form_path.sub(".json", "").split("/")[-1] + # form_paths.push(form_path) + # end + # end + # form_paths + # end +end diff --git a/spec/fixtures/forms/test_validator.json b/spec/fixtures/forms/test_validator.json new file mode 100644 index 000000000..2ac97e26d --- /dev/null +++ b/spec/fixtures/forms/test_validator.json @@ -0,0 +1,24 @@ +{ + "form_type": "lettings", + "sections": { + "household": { + "label": "About the household", + "subsections": { + "household_characteristics": { + "label": "Household characteristics", + "pages": { + "tenant_code": { + "questions": { + "tenant_code": { + "check_answer_label": "Tenant code", + "header": "What is the tenant code?", + "type": "text" + } + } + } + } + } + } + } + } +}