lib/lutaml/model/attribute.rb in lutaml-model-0.3.19 vs lib/lutaml/model/attribute.rb in lutaml-model-0.3.20

- old
+ new

@@ -11,13 +11,16 @@ values ].freeze def initialize(name, type, options = {}) @name = name - @type = cast_type(type) + + validate_type!(type) + @type = cast_type!(type) + + validate_options!(options) @options = options - validate_options! @raw = !!options[:raw] if collection? validate_collection_range @@ -27,11 +30,11 @@ def delegate @options[:delegate] end - def cast_type(type) + def cast_type!(type) case type when Class type when String Type.const_get(type) @@ -210,13 +213,20 @@ end end private - def validate_options! - if (options = @options.keys - ALLOWED_OPTIONS).any? - raise StandardError, "Invalid options given for `#{name}` #{options}" + def validate_options!(options) + if (invalid_opts = options.keys - ALLOWED_OPTIONS).any? + raise StandardError, "Invalid options given for `#{name}` #{invalid_opts}" end + end + + def validate_type!(type) + return true if type.is_a?(Class) + return true if [Symbol, String].include?(type.class) && cast_type!(type) + + raise ArgumentError, "Invalid type: #{type}, must be a Symbol, String or a Class" end end end end