# \[ANN\] Semantic Attributes (validation, machine/human formatting)

**URL:** <https://www.ruby-forum.com/t/ann-semantic-attributes-validation-machine-human-formatting/137770>\
**Category:** Rails\
**Created:** [May 16, 2008, 6:13am UTC](https://www.ruby-forum.com/t/ann-semantic-attributes-validation-machine-human-formatting/137770 "2008-05-16T06:13:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cainlevy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@cainlevy](https://www.ruby-forum.com/u/cainlevy)\
**Post date:** [May 16, 2008, 6:13am UTC](https://www.ruby-forum.com/t/ann-semantic-attributes-validation-machine-human-formatting/137770/1 "2008-05-16T06:13:18Z")

</div>

I’ve never taken the time to package/release/announce this plugin of  
mine,  
have I? Forgive me. The time has come.

I introduce to you Semantic Attributes!

From the README:

The method-chained validation routine built into ActiveRecord must die!  
It’s  
time for an object-oriented approach to attribute validations. The  
Semantic  
Attributes plugin provides this approach by letting you attach  
predicates to  
your attributes with a tasty DSL. These predicates package up some  
really  
sweet behavior, where validations are really only the beginning. I’ve  
also  
discovered that it can be really useful to use these predicates to  
convert  
between human and machine formats: for example, with the phone number  
predicate you can let your users enter phone numbers with whatever  
formatting they want, always save the values to the database as numeric  
strings, and then present the values back to the user with standard  
formatting.

I’ve also found other nifty uses for object-oriented predicates that  
package  
up validation. For example, it becomes easy to run a quick validation  
check  
on a field with a sample value and report true/false. This is exactly  
what  
the expected\_error\_for(:field, value)\> method does, and it lets you  
build a  
validation routine that listens to form data as it’s being typed and  
report  
problems without duplicating your validation code client-side. In a  
similar  
vein, the \_valid? attribute suffix lets you do single-attribute  
validation  
on a record anytime you want.  
For more, see:  
\*  
[http://codelevy.com/articles/2008/05/16/semantic-attributes-a-rails-validation-and-attribute-formatting-plugin](http://codelevy.com/articles/2008/05/16/semantic-attributes-a-rails-validation-and-attribute-formatting-plugin)

- [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/semanticattributes/issues/list)

and of course:

- [GitHub - cainlevy/semantic-attributes: object-oriented activerecord validations and machine/human formatting](http://github.com/cainlevy/semantic-attributes/tree/master)

-Lance

p.s. this is completely compatible with ActiveRecord validations … you  
can  
use 'em both if you want

–  
rails blog: [http://codelevy.com](http://codelevy.com)  
co-founder: [http://uservoice.com](http://uservoice.com)
