bloc referral

If you are interested in learning to code with a mentor, try bloc and get $100 off.

Monday, August 11, 2014

email validations for arrays in rails

Sometimes things just aren't that easy. Rails is definitely nice when you do everything out of the box and do not veer to far from the path; I am quickly finding that there are some things that are difficult figure out.

For example I had a task late afternoon to validate emails that are stored in an Array. I original stored all emails as strings but due to way the Rails Mailer works and the needs of the application, the emails are now being collected using CSV(separate by commas) in a string. I am easily able to split that string using split(",") and serializing that into an array. I am also handling single emails by converting them using the Rails method Array.wrap(). Those 2 methods could be blog post in their selves but I am opting to just explain my next issue.

The trouble with storing the emails in an Array is that it broke all my email validations, which does not check a string of emails,  I now am tasked with testing the validations by hand, after discovering that the Active Validator only validates strings.


Validate an array of emails.
emails: ["ilikerobot[at]gmail.com, hello[at]briandouglas.me"]

emails.split(",")

emails: ["ilikerobot[at]gmail.com", "hello[at]briandouglas.me"]

With this being my first major case where Rails couldn't do what, I am considering looking at the Active Validator to see if I can contribute to it and add this functionality, but it might be better as a gem. I am sure it would take longer than a night to implement but it could be a cool challenge to create my first gem and this idea seems simple enough.

My first implementation:








My final implementation *after peer review, it turned out I wrote too much code just to check for the validation of an email :)







I also did the same validation in Javascript, on the client-side.




1 comment:

  1. Just previewed thanks for the information, wanna advise another great tool - https://www.mailcheck.co/ it's linked to social profiles less to direct SMPT checks.

    ReplyDelete