lib/avromatic/model/attributes.rb in avromatic-0.9.0.rc4 vs lib/avromatic/model/attributes.rb in avromatic-0.9.0.rc6
- old
+ new
@@ -75,17 +75,35 @@
when :enum
validates(field.name,
inclusion: { in: Set.new(field.type.symbols.map(&:freeze)).freeze })
when :fixed
validates(field.name, length: { is: field.type.size })
+ when :record
+ validate_record(field.name)
end
add_required_validation(field)
end
+ def validate_record(field_name)
+ validate do |instance|
+ record = instance.send(field_name)
+ if record && record.invalid?
+ record.errors.each do |key, message|
+ errors.add(field_name.to_sym, "invalid: #{key} #{message}")
+ end
+ end
+ end
+ end
+
def add_required_validation(field)
if required?(field) && field.default == :no_default
- validates(field.name, presence: true)
+ case field.type.type_sym
+ when :array, :map, :boolean
+ validates(field.name, exclusion: { in: [nil], message: "can't be nil" })
+ else
+ validates(field.name, presence: true)
+ end
end
end
# An optional field is represented as a union where the first member
# is null.