# DRY validates\_format\_of?

**URL:** <https://www.ruby-forum.com/t/dry-validates-format-of/139817>\
**Category:** Rails\
**Created:** [June 7, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/dry-validates-format-of/139817 "2008-06-07T16:59:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [June 7, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/dry-validates-format-of/139817/1 "2008-06-07T16:59:23Z")

</div>

In a model I have following validates\_format\_of. Is there an easy way to  
DRY this up? It seems to be rather repetitive.

validates\_format\_of :expiration\_date, :with =\>  
/^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/  
validates\_format\_of :activation\_date, :with =\>  
/^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/  
validates\_format\_of :some\_other\_date, :with =\>  
/^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/  
…

Thanks

---

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [June 7, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/dry-validates-format-of/139817/2 "2008-06-07T17:28:35Z")

</div>

Hi Jens,

validates\_format\_of takes a regular expression as its argument:

> with - The regular expression used to validate the format with (note: must be supplied!)

… so you just assign the regular expression to a variable

> date\_format = /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/

… and use it in your validates\_format\_of calls as follows:

> validates\_format\_of :expiration\_date, :with =\> date\_format  
> validates\_format\_of :activation\_date, :with =\> date\_format  
> validates\_format\_of :some\_other\_date, :with =\> date\_format

Here’s how that works:

~ $ irb

> > date\_format = /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/  
> > =\> /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/
> 
> > date\_format.class  
> > =\> Regexp
> 
> > /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/.class  
> > =\> Regexp
> 
> > exit  
> > ~ $ \_

Peter

---

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [June 7, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/dry-validates-format-of/139817/3 "2008-06-07T17:56:05Z")

</div>

Sigh! … so easy!

Thanks Peter for your time

---

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [June 7, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/dry-validates-format-of/139817/4 "2008-06-07T19:58:30Z")

</div>

Hi Jens,

validates\_format\_of takes a regular expression as its argument:

> â€¢ with - The regular expression used to validate the format with  
> (note: must be supplied!)

… so you just assign the regular expression to a variable

> date\_format = /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/

… and use it in your validates\_format\_of calls as follows:

> > validates\_format\_of :expiration\_date, :with =\> date\_format  
> > validates\_format\_of :activation\_date, :with =\> date\_format  
> > validates\_format\_of :some\_other\_date, :with =\> date\_format

Note that class of the regexp:

> ~ $ irb
> 
> > > date\_format = /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/  
> > > =\> /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/
> > 
> > > date\_format.class  
> > > =\> Regexp
> > 
> > > /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/.class  
> > > =\> Regexp
> > 
> > > exit  
> > > ~ $

DRY is the future 🙂

Peter

---

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [June 7, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/dry-validates-format-of/139817/5 "2008-06-07T20:14:23Z")

</div>

On Jun 7, 2008, at 7:59 AM, Jens – wrote:

> /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/  
> …

I wrote a series of custom validations that in addition to defining a  
number of common character patterns as named validations, it also  
allowed multiple fields per validation

[http://www.railsdev.ws/blog/11/custom-validations-in-rails/](http://www.railsdev.ws/blog/11/custom-validations-in-rails/)

It made a huge difference in DRYness and the amount of code needed  
for validations.

–  
def gw  
writes\_at ‘www.railsdev.ws’  
end
