How would you validate each object inside an array of object with Rails?
I am building a user profile form in our Rails app. Inside user model, we have basic string attributes but some jsonb fields as well. JSONb fields default to [] because we want to store an array of objects inside that attribute. Here is an example of simplified user model attributes:
name: stringemail: stringeducation: jsonb, default: []
Education is an array of objects such as:
[{
school: 'Harvard university',
degree: 'Computer Science',
from_date: 'Tue, 11 Jul 2017 16:22:12 +0200`,
to_date: 'Tue, 11 Jul 2017 16:22:12 +0200'
},{
school: 'High school',
degree: 'Whatever',
from_date: 'Tue, 11 Jul 2017 16:22:12 +0200`,
to_date: 'Tue, 11 Jul 2017 16:22:12 +0200'
}]
User should be able to click Add school button, to add more fields via jquery. That jquery part is not important for this question - maybe just an explanation why we used an Array of objects.
How would you validate each item in education array, so I can mark the text field containing validtion error with red color? I got adviced that using FormObject pattern might help here. I have also tried writing custom validator that inherits from ActiveModel::Validator class, but the main problem still lies in fact, that I am dealing with an array, not actual object..
Thanks for any constructive help.