6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/universe_compiler/entity/validation.rb', line 6
def invalid_fields
invalid = {}
self.class.fields_constraints.each do |field_name, constraints|
constraints.each do |constraint_name, value|
case constraint_name
when :not_null
invalid_for_constraint invalid, field_name, constraint_name, value if fields[field_name].nil?
when :not_empty
invalid_for_constraint invalid, field_name, constraint_name, value if fields[field_name].nil? or fields[field_name].empty?
when :should_match
unless fields[field_name].nil?
invalid_for_constraint invalid, field_name, constraint_name, value unless fields[field_name].to_s.match value
end
when :class_name
unless fields[field_name].nil?
case value
when String
invalid_for_constraint invalid, field_name, constraint_name, value unless fields[field_name].class.name == value
else
invalid_for_constraint invalid, field_name, constraint_name, value unless fields[field_name].class == value
end
end
when :is_array
if fields[field_name].nil? or not fields[field_name].is_a? Array
invalid_for_constraint invalid, field_name, constraint_name, value
end
when :is_hash
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?
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|
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
begin
send value[:actual_method] if value[:unique_result]
rescue UniverseCompiler::Error => uce
invalid_for_constraint invalid, value[:actual_method], constraint_name, value
end
else
UniverseCompiler.logger.warn "Cannot handle unknown constraint '#{constraint_name}'! Skipping..."
end
end
end
invalid
end
|