diff --git a/app/helpers/json_schema_validation.rb b/app/helpers/json_schema_validation.rb index 4d947c450..d500e018d 100644 --- a/app/helpers/json_schema_validation.rb +++ b/app/helpers/json_schema_validation.rb @@ -5,72 +5,385 @@ require "json" # "start_year": 2021, # "end_year": 2022, # "sections": { +# "about_this_log": { +# "label": "About this log", +# "subsections": { # "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" - # } - # } - # }, +# "pages": { + # "tenant_code": { + # "header": "", + # "description": "", + # "questions": { + # "tenant_code": { + # "check_answer_label": "Tenant code", + # "header": "What is the tenant code?", + # "hint_text": "", + # "type": "text" + # } + # } + # }, begin schema = { - "$schema": "http://json-schema.org/draft-04/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" - }, + "$schema": "http://json-schema.org/draft-04/schema", + "$id": "http://example.com/example.json", + "type": "object", + "title": "The root schema", + "description": "The root schema comprises the entire JSON document.", + "default": {}, + "examples": [ + { + "form_type": "lettings", + "start_year": 2021, + "end_year": 2022, "sections": { - "description": "", - "type": "object", - "patternProperties": { - "^[0-9]+$": { - "description": "", - "type": "string" - }, - "label": { - "description": "", - "type": "string" - }, + "household": { + "label": "About the household", "subsections": { - "type": "object" + "household_characteristics": { + "label": "Household characteristics", + "pages": { + "tenant_code": { + "questions": { + "tenant_code": { + "check_answer_label": "Tenant code", + "header": "What is the tenant code?", + "type": "text" + } + } + } + } + } } } } } - } + ], + "required": [ + "form_type", + "start_year", + "end_year", + "sections" + ], + "properties": { + "form_type": { + "$id": "#/properties/form_type", + "type": "string", + "title": "The form_type schema", + "description": "An explanation about the purpose of this instance.", + "default": "", + "examples": [ + "lettings" + ] + }, + "start_year": { + "$id": "#/properties/start_year", + "type": "integer", + "title": "The start_year schema", + "description": "An explanation about the purpose of this instance.", + "default": 0, + "examples": [ + 2021 + ] + }, + "end_year": { + "$id": "#/properties/end_year", + "type": "integer", + "title": "The end_year schema", + "description": "An explanation about the purpose of this instance.", + "default": 0, + "examples": [ + 2022 + ] + }, + "sections": { + "$id": "#/properties/sections", + "type": "object", + "title": "The sections schema", + "description": "An explanation about the purpose of this instance.", + "default": {}, + "examples": [ + { + "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" + } + } + } + } + } + } + } + } + ], + "required": [ + "household" + ], + "properties": { + "household": { + "$id": "#/properties/sections/properties/household", + "type": "object", + "title": "The household schema", + "description": "An explanation about the purpose of this instance.", + "default": {}, + "examples": [ + { + "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" + } + } + } + } + } + } + } + ], + "required": [ + "label", + "subsections" + ], + "properties": { + "label": { + "$id": "#/properties/sections/properties/household/properties/label", + "type": "string", + "title": "The label schema", + "description": "An explanation about the purpose of this instance.", + "default": "", + "examples": [ + "About the household" + ] + }, + "subsections": { + "$id": "#/properties/sections/properties/household/properties/subsections", + "type": "object", + "title": "The subsections schema", + "description": "An explanation about the purpose of this instance.", + "default": {}, + "examples": [ + { + "household_characteristics": { + "label": "Household characteristics", + "pages": { + "tenant_code": { + "questions": { + "tenant_code": { + "check_answer_label": "Tenant code", + "header": "What is the tenant code?", + "type": "text" + } + } + } + } + } + } + ], + "required": [ + "household_characteristics" + ], + "properties": { + "household_characteristics": { + "$id": "#/properties/sections/properties/household/properties/subsections/properties/household_characteristics", + "type": "object", + "title": "The household_characteristics schema", + "description": "An explanation about the purpose of this instance.", + "default": {}, + "examples": [ + { + "label": "Household characteristics", + "pages": { + "tenant_code": { + "questions": { + "tenant_code": { + "check_answer_label": "Tenant code", + "header": "What is the tenant code?", + "type": "text" + } + } + } + } + } + ], + "required": [ + "label", + "pages" + ], + "properties": { + "label": { + "$id": "#/properties/sections/properties/household/properties/subsections/properties/household_characteristics/properties/label", + "type": "string", + "title": "The label schema", + "description": "An explanation about the purpose of this instance.", + "default": "", + "examples": [ + "Household characteristics" + ] + }, + "pages": { + "$id": "#/properties/sections/properties/household/properties/subsections/properties/household_characteristics/properties/pages", + "type": "object", + "title": "The pages schema", + "description": "An explanation about the purpose of this instance.", + "default": {}, + "examples": [ + { + "tenant_code": { + "questions": { + "tenant_code": { + "check_answer_label": "Tenant code", + "header": "What is the tenant code?", + "type": "text" + } + } + } + } + ], + "required": [ + "tenant_code" + ], + "properties": { + "tenant_code": { + "$id": "#/properties/sections/properties/household/properties/subsections/properties/household_characteristics/properties/pages/properties/tenant_code", + "type": "object", + "title": "The tenant_code schema", + "description": "An explanation about the purpose of this instance.", + "default": {}, + "examples": [ + { + "questions": { + "tenant_code": { + "check_answer_label": "Tenant code", + "header": "What is the tenant code?", + "type": "text" + } + } + } + ], + "required": [ + "questions" + ], + "properties": { + "questions": { + "$id": "#/properties/sections/properties/household/properties/subsections/properties/household_characteristics/properties/pages/properties/tenant_code/properties/questions", + "type": "object", + "title": "The questions schema", + "description": "An explanation about the purpose of this instance.", + "default": {}, + "examples": [ + { + "tenant_code": { + "check_answer_label": "Tenant code", + "header": "What is the tenant code?", + "type": "text" + } + } + ], + "required": [ + "tenant_code" + ], + "properties": { + "tenant_code": { + "$id": "#/properties/sections/properties/household/properties/subsections/properties/household_characteristics/properties/pages/properties/tenant_code/properties/questions/properties/tenant_code", + "type": "object", + "title": "The tenant_code schema", + "description": "An explanation about the purpose of this instance.", + "default": {}, + "examples": [ + { + "check_answer_label": "Tenant code", + "header": "What is the tenant code?", + "type": "text" + } + ], + "required": [ + "check_answer_label", + "header", + "type" + ], + "properties": { + "check_answer_label": { + "$id": "#/properties/sections/properties/household/properties/subsections/properties/household_characteristics/properties/pages/properties/tenant_code/properties/questions/properties/tenant_code/properties/check_answer_label", + "type": "string", + "title": "The check_answer_label schema", + "description": "An explanation about the purpose of this instance.", + "default": "", + "examples": [ + "Tenant code" + ] + }, + "header": { + "$id": "#/properties/sections/properties/household/properties/subsections/properties/household_characteristics/properties/pages/properties/tenant_code/properties/questions/properties/tenant_code/properties/header", + "type": "string", + "title": "The header schema", + "description": "An explanation about the purpose of this instance.", + "default": "", + "examples": [ + "What is the tenant code?" + ] + }, + "type": { + "$id": "#/properties/sections/properties/household/properties/subsections/properties/household_characteristics/properties/pages/properties/tenant_code/properties/questions/properties/tenant_code/properties/type", + "type": "string", + "title": "The type schema", + "description": "An explanation about the purpose of this instance.", + "default": "", + "examples": [ + "text" + ] + } + }, + "additionalProperties": true + } + }, + "additionalProperties": true + } + }, + "additionalProperties": true + } + }, + "additionalProperties": true + } + }, + "additionalProperties": true + } + }, + "additionalProperties": true + } + }, + "additionalProperties": true + } + }, + "additionalProperties": true + } + }, + "additionalProperties": true +} metaschema = JSON::Validator.validator_for_name("draft4").metaschema - # => true + if JSON::Validator.validate(metaschema, schema) - puts "schema valid" + puts "schema valid" else - puts "schema not valid" - return + puts "schema not valid" + return end path = "spec/fixtures/forms/test_validator.json" @@ -84,20 +397,20 @@ begin puts JSON::Validator.fully_validate(schema, data, :strict => true) begin - JSON::Validator.validate!(schema, data) + JSON::Validator.validate!(schema, data) rescue JSON::Schema::ValidationError => e - e.message + 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 + # 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 - # form_paths + # end + # form_paths # end end diff --git a/spec/fixtures/forms/test_validator.json b/spec/fixtures/forms/test_validator.json index 0539a719b..13aed9e5f 100644 --- a/spec/fixtures/forms/test_validator.json +++ b/spec/fixtures/forms/test_validator.json @@ -8,6 +8,7 @@ "subsections": { "household_characteristics": { "label": "Household characteristics", + "baddata": "Shouldn't be here but what you gonna do?", "pages": { "tenant_code": { "questions": {