Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/canvas/validators/schema_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class SchemaAttribute

attr_reader :attribute, :custom_types, :errors, :additional_reserved_names

class << self
def permitted_keys
VALIDATORS.flat_map { |_, validator| validator.new({}).permitted_keys }.uniq
end
end

def initialize(attribute:, custom_types: [], additional_reserved_names: [])
@attribute = attribute
@custom_types = custom_types
Expand Down
10 changes: 7 additions & 3 deletions lib/canvas/validators/schema_attributes/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def validate
ensure_keys_are_correct_types
end

def permitted_keys
permitted_schema.keys
end

private

# The base keys required for this attribute to be valid and their
Expand Down Expand Up @@ -59,7 +63,7 @@ def optional_keys
}
end

def all_permitted_keys
def permitted_schema
required_keys.merge(optional_keys)
end

Expand All @@ -72,15 +76,15 @@ def ensure_has_required_keys
end

def ensure_no_unrecognized_keys
unrecognized_keys = attribute.keys - all_permitted_keys.keys
unrecognized_keys = attribute.keys - permitted_schema.keys
return true if unrecognized_keys.empty?

@errors << "Unrecognized keys: #{unrecognized_keys.join(', ')}"
false
end

def ensure_keys_are_correct_types
all_permitted_keys.each do |key, expected|
permitted_schema.each do |key, expected|
if expected.is_a?(Class)
if attribute.key?(key) && !attribute[key].is_a?(expected)
actual = attribute[key].class.name
Expand Down
2 changes: 1 addition & 1 deletion lib/canvas/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Canvas
VERSION = "3.4.0"
VERSION = "3.5.0"
end
5 changes: 5 additions & 0 deletions spec/lib/canvas/validators/schema_attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
end
end

it "exposes all keys permitted to be in attribute schemas, in the order of validators" do
expect(described_class.permitted_keys)
.to eq(%w[name type default array label hint group only unit options min max step only_from])
end

describe "#validate" do
context "when the attribute has only required keys" do
let(:attribute) {
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/canvas/validators/schema_attributes/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
Canvas::Validator::SchemaAttribute::Base.new(attribute)
}

describe "#permitted_keys" do
let(:attribute) { {} }
let(:common_attribute_keys) { %w[name type default array label hint group] }

it "exposes all common permitted keys" do
expect(validator.permitted_keys).to eq(common_attribute_keys)
end

it "exposes all extra optional and required keys for subclassed attributes" do
class NewAttribute < described_class
private
def required_keys = super.merge("requiredkey" => String)
def optional_keys = super.merge("optionalkey" => String)
end

expect(NewAttribute.new(attribute).permitted_keys).to match_array(common_attribute_keys + %w[requiredkey optionalkey])
end
end

describe "#validate" do
context "when attribute is valid" do
let(:attribute) {
Expand Down