Class: Canvas::Validator::MenuSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/canvas/validators/menu_schema.rb

Overview

:documented: This class is used to validate a schema for a menu.

Example: {

"max_item_levels" => 2,
"supports_open_new_tab" => "true",
"attributes" => {
   "fixed" => {
      "group" => "design",
      "label" => "Fixed when scrolling",
      "hint" => "The menu will stay fixed to the top when scrolling down the page.",
      "type" => "boolean",
      "default" => "false"
   },
   "background_color" => {
     "type" => "color"
   }
},
"layout" => [
  {
    "type" => "tab",
    "label" => "Content",
    "elements" => [
      "fixed",
      "background_color"
    ]
  }
]

}

Direct Known Subclasses

FooterSchema

Constant Summary collapse

PERMITTED_KEYS =
%w[max_item_levels supports_open_new_tab attributes layout].freeze
ADDITIONAL_RESERVED_NAMES =
%w[items type].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, custom_types: []) ⇒ MenuSchema

Returns a new instance of MenuSchema.

Parameters:

  • schema (Hash)

    the schema to be validated

  • custom_types (Array<Hash>) (defaults to: [])

    a list of custom types


43
44
45
46
47
# File 'lib/canvas/validators/menu_schema.rb', line 43

def initialize(schema:, custom_types: [])
  @schema = schema
  @custom_types = custom_types
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.


39
40
41
# File 'lib/canvas/validators/menu_schema.rb', line 39

def errors
  @errors
end

#schemaObject (readonly)

Returns the value of attribute schema.


39
40
41
# File 'lib/canvas/validators/menu_schema.rb', line 39

def schema
  @schema
end

Instance Method Details

#validateObject


49
50
51
52
53
54
55
56
57
58
# File 'lib/canvas/validators/menu_schema.rb', line 49

def validate
  if ensure_valid_format
    ensure_no_unrecognized_keys
    ensure_max_item_levels_is_valid
    ensure_layout_is_valid
    ensure_attributes_are_valid
  end

  @errors.empty?
end