Class: Canvas::Validator::BlockSchema

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

Overview

:documented: This class is used to validate a schema for a block. Example of a valid block schema: {

"attributes" => {
  "my_title" => {
    "type" => "string"
  },
  "my_color" => {
    "type" => "color",
    "label" => "My color",
    "hint" => "Select your favourite color"
  }
},
"layout" => [
  {
    "type" => "tab",
    "label" => "Content",
    "elements" => [
      "my_title",
      "my_color"
    ]
  }
]

}

Constant Summary collapse

PERMITTED_KEYS =
%w[attributes layout].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of BlockSchema.

Parameters:

  • schema (Hash)

    the schema to be validated

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

    a list of custom types



40
41
42
43
44
# File 'lib/canvas/validators/block_schema.rb', line 40

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



36
37
38
# File 'lib/canvas/validators/block_schema.rb', line 36

def errors
  @errors
end

#schemaObject (readonly)

Returns the value of attribute schema.



36
37
38
# File 'lib/canvas/validators/block_schema.rb', line 36

def schema
  @schema
end

Instance Method Details

#validateObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/canvas/validators/block_schema.rb', line 46

def validate
  @errors = []

  if ensure_valid_format
    ensure_no_unrecognized_keys
    ensure_layout_is_valid
    ensure_attributes_are_valid
  end

  @errors.empty?
end