Class: FluidCLI::Theme::SchemaValidator
- Inherits:
-
Object
- Object
- FluidCLI::Theme::SchemaValidator
- Defined in:
- lib/fluid_cli/theme/schema_validator.rb
Defined Under Namespace
Classes: Diagnostic, JsonTokenScan
Constant Summary collapse
- VALID_SETTING_TYPES =
%w[ text rich_text richtext textarea range select checkbox color text_alignment color_background radio url image image_picker video_picker font_picker html html_textarea link_list header blog posts posts_list products products_list product product_list forms collections collections_list collection collection_list categories categories_list category category_list enrollments enrollments_list enrollment_pack enrollment enrollment_list ].freeze
Instance Method Summary collapse
-
#initialize(file, blocks_schema_type: "unknown") ⇒ SchemaValidator
constructor
A new instance of SchemaValidator.
- #validate ⇒ Object
Constructor Details
#initialize(file, blocks_schema_type: "unknown") ⇒ SchemaValidator
Returns a new instance of SchemaValidator.
20 21 22 23 |
# File 'lib/fluid_cli/theme/schema_validator.rb', line 20 def initialize(file, blocks_schema_type: "unknown") @file = file @blocks_schema_type = blocks_schema_type end |
Instance Method Details
#validate ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fluid_cli/theme/schema_validator.rb', line 25 def validate text = @file.read.to_s diagnostics = [] match = text.match(/{% schema %}([\s\S]*?){% endschema %}/) return diagnostics.map(&:to_h) unless match json_text = match[1] || "" schema = nil begin schema = JSON.parse(json_text) rescue JSON::ParserError => e diagnostics << Diagnostic.new(severity: "error", message: "Invalid JSON:\n #{e.}") return diagnostics.map(&:to_h) end scan = JsonTokenScan.scan(json_text) scan.duplicate_blocks_key_tokens.each do diagnostics << Diagnostic.new( severity: "error", message: "Error in blocks: duplicate 'blocks' key in the same object" ) end if schema.is_a?(Hash) && schema["settings"].is_a?(Array) diagnostics.concat(validate_settings(schema["settings"])) end if @blocks_schema_type == "object" && !schema["blocks"].is_a?(Hash) scan.blocks_array_value_tokens.each do diagnostics << Diagnostic.new( severity: "error", message: "Error in blocks: expected an object ({})" ) end end if schema.is_a?(Hash) && schema.key?("blocks") blocks = schema["blocks"] case @blocks_schema_type when "array" if !blocks.is_a?(Array) diagnostics << Diagnostic.new( severity: "error", message: "Error in blocks: expected an array ([])" ) else diagnostics.concat(validate_blocks(blocks)) end when "object" unless blocks.is_a?(Hash) diagnostics << Diagnostic.new( severity: "error", message: "Error in blocks: expected an object ({})" ) end else # "unknown" diagnostics.concat(validate_blocks(blocks)) if blocks.is_a?(Array) end end diagnostics.map(&:to_h) end |