lib/universe_compiler/entity/validation.rb in universe_compiler-0.5.5 vs lib/universe_compiler/entity/validation.rb in universe_compiler-0.5.6
- old
+ new
@@ -33,24 +33,20 @@
if fields[field_name].nil? or not fields[field_name].is_a? Hash
invalid_for_constraint invalid, field_name, constraint_name, value
end
when :has_one
unless fields[field_name].nil?
- if fields[field_name].respond_to? :type
- invalid_for_constraint invalid, field_name, constraint_name, value unless fields[field_name].type == value
- else
+ unless provided_entity_compatible_with_type? fields[field_name], value, constraints[:strict_type]
invalid_for_constraint invalid, field_name, constraint_name, value
end
end
when :has_many
if fields[field_name].nil? or not fields[field_name].is_a? Array
invalid_for_constraint invalid, field_name, constraint_name, value
else
fields[field_name].each do |related_object|
- if related_object.respond_to? :type
- invalid_for_constraint invalid, field_name, constraint_name, value unless related_object.type == value
- else
+ unless provided_entity_compatible_with_type? related_object, value, constraints[:strict_type]
invalid_for_constraint invalid, field_name, constraint_name, value
end
end
end
when :reverse_method
@@ -84,9 +80,18 @@
end
true
end
private
+
+ def provided_entity_compatible_with_type?(linked_object, declared_type, strict_type)
+ declared_class = UniverseCompiler::Entity::TypeManagement.type_class_mapping(declared_type)
+ if strict_type
+ linked_object.class == declared_class
+ else
+ linked_object.is_a? declared_class
+ end
+ end
def invalid_for_constraint(invalid_fields_definition, field_name, constraint_name, value)
invalid_fields_definition[field_name] ||= []
invalid_fields_definition[field_name] << { constraint_name => value}
end