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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:) ⇒ CustomType

Returns a new instance of CustomType.



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

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

Instance Method Details

#validateObject



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

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

  errors.empty?
end