Skip to content

Commit

Permalink
224: validate PolymorphicModels
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricL-Gitster committed May 22, 2024
1 parent 4dca354 commit 27b680d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Keep nested model validation working in Rails post-7.1, by no longer trying to re-use ActiveRecord::Validations::AssociatedValidator, instead supplying our own custom code. https://github.com/jrochkind/attr_json/pull/220

*
* Validate PolymorphicModel instances. https://github.com/jrochkind/attr_json/pull/225

*

Expand Down
2 changes: 1 addition & 1 deletion lib/attr_json/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def attr_json(name, type, **options)
)

# By default, automatically validate nested models
if type.kind_of?(AttrJson::Type::Model) && options[:validate] != false
if (type.kind_of?(AttrJson::Type::Model) || type.kind_of?(AttrJson::Type::PolymorphicModel)) && options[:validate] != false
# Post validations up with something based on ActiveRecord::Validations::AssociatedValidator
self.validates_with ::AttrJson::Model::NestedModelValidator, attributes: [name.to_sym]
end
Expand Down
48 changes: 48 additions & 0 deletions spec/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,54 @@ def self.model_name ; ActiveModel::Name.new(self, nil, "Klass") ; end
end
end

describe "nested polymorphic model with validation" do
let(:nested_class1) do
Class.new do
include AttrJson::Model
def self.model_name ; ActiveModel::Name.new(self, nil, "NestedClass1") ; end
attr_json :str1, :string
validates_presence_of :str
end
end
let(:nested_class2) do
Class.new do
include AttrJson::Model
def self.model_name ; ActiveModel::Name.new(self, nil, "NestedClass2") ; end
attr_json :str2, :string
validates_presence_of :str
end
end
let(:klass) do
Class.new do
include AttrJson::Model
def self.model_name ; ActiveModel::Name.new(self, nil, "Klass") ; end

attr_json :nested, AttrJson::Type::PolymorphicModel.new(NestedClass1, NestedClass2), array: true
end
end

it "is invalid when nested are" do
instance.nested = [nested_class1.new, nested_class2.new]

expect(instance.valid?).to be false
expect(instance.errors.key?(:nested)).to be true
expect(instance.errors[:nested]).to include("is invalid")

expect(instance.nested[0].errors.key?(:str1))
expect(instance.nested[0].errors[:str1]).to include a_string_matching(/\Acan'|’t be blank\z/)
expect(instance.errors.details[:nested].first[:value][0]).to be_kind_of(nested_class1)

expect(instance.nested[1].errors.key?(:str2))
expect(instance.nested[1].errors[:str2]).to include a_string_matching(/\Acan'|’t be blank\z/)
expect(instance.errors.details[:nested].first[:value][1]).to be_kind_of(nested_class2)
end

it "is valid when nested are" do
instance.nested = [nested_class1.new(str1: "something"), nested_class2.new(str2: "something")]
expect(instance.valid?).to be true
end
end

describe "nested array of models with validation" do
let(:nested_class) do
Class.new do
Expand Down

0 comments on commit 27b680d

Please sign in to comment.