diff --git a/spec/save_operation_spec.cr b/spec/save_operation_spec.cr index 3c238d191..2d9e6d8df 100644 --- a/spec/save_operation_spec.cr +++ b/spec/save_operation_spec.cr @@ -62,6 +62,14 @@ describe "Avram::SaveOperation" do operation.required_attributes.should eq({operation.title}) end + it "can save empty arrays" do + bucket = BucketBox.create + + bucket = Bucket::SaveOperation.update!(bucket, names: [] of String) + + bucket.names.should eq([] of String) + end + it "set params if passed in" do now = Time.utc.at_beginning_of_minute user = SaveUser.create!(name: "Dan", age: 34, joined_at: now) diff --git a/src/avram/validations.cr b/src/avram/validations.cr index 02d8bdd47..125c8af29 100644 --- a/src/avram/validations.cr +++ b/src/avram/validations.cr @@ -48,7 +48,7 @@ module Avram::Validations # ``` def validate_required(*attributes, message : Avram::Attribute::ErrorMessage = "is required") attributes.each do |attribute| - if attribute.value.blank? && attribute.value != false + if (attribute.value.blank? || attribute.value == false) && !attribute.value.is_a?(Array) attribute.add_error message end end