Class: Canvas::Validator::CustomType

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

Overview

:documented: This class is used to validate the JSON defining a custom type.

An example of a valid custom type: {

"key": "faq",
"name": "Faq",
"attributes": [
  {
    "name": "question",
    "label": "Question",
    "type": "string"
  },
  {
    "name": "answer",
    "label": "Answer",
    "type": "text"
  }
]

}

Constant Summary collapse

REQUIRED_KEYS =
%w[key name attributes].freeze
OPTIONAL_KEYS =
%w[layout].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CustomType.



32
33
34
35
36
# File 'lib/canvas/validators/custom_type.rb', line 32

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

Instance Attribute Details

#custom_typesObject (readonly)

Returns the value of attribute custom_types.



30
31
32
# File 'lib/canvas/validators/custom_type.rb', line 30

def custom_types
  @custom_types
end

#errorsObject (readonly)

Returns the value of attribute errors.



30
31
32
# File 'lib/canvas/validators/custom_type.rb', line 30

def errors
  @errors
end

#schemaObject (readonly)

Returns the value of attribute schema.



30
31
32
# File 'lib/canvas/validators/custom_type.rb', line 30

def schema
  @schema
end

Instance Method Details

#validateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/canvas/validators/custom_type.rb', line 38

def validate
  ensure_valid_format &&
    ensure_has_required_keys &&
    ensure_no_unrecognized_keys &&
    ensure_keys_are_correct_types &&
    ensure_key_is_lowercase &&
    ensure_key_value_is_not_reserved &&
    ensure_no_duplicate_attributes &&
    ensure_attributes_are_valid &&
    ensure_first_attribute_not_array &&
    ensure_layout_is_valid

  errors.empty?
end