# validates\_type

DevFeed: [validates\_type](<https://devfeed.tech/articles/validates-type-21023.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2015/05/10/validates-type/>)

Published: 2015-05-10T12:00:00Z

Content type: article

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Library](<https://devfeed.tech/topics/library.md>), [Rails](<https://devfeed.tech/topics/rails.md>), [Data Quality](<https://devfeed.tech/topics/data-quality.md>), [Object-relational mapping](<https://devfeed.tech/topics/orm.md>), [Code](<https://devfeed.tech/topics/code.md>), [API](<https://devfeed.tech/topics/api.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [code](<https://devfeed.tech/tags/code.md>), [data-quality](<https://devfeed.tech/tags/data-quality.md>), [json](<https://devfeed.tech/tags/json.md>), [library](<https://devfeed.tech/tags/library.md>), [orm](<https://devfeed.tech/tags/orm.md>), [rails](<https://devfeed.tech/tags/rails.md>)

## AI overview

The article introduces validates_type, a lightweight library for enforcing exact attribute types when saving data in Rails and Sinatra applications. It explains how ORM type coercion can allow invalid values through and presents model-level validation as a way to preserve data quality and make APIs more reliable.

## Source excerpt

In keeping with my apparent obsession with types, I created another helpful library: validates_type. I wanted a nice, lightweight way to assert that the types of my attributes are exactly what I need them to be once I save them to the database. No one is particularly fond of littering their code with trys and is_a?s. To remedy this, I make my models assert confidence that the data in my database is what I expect. I'm not talking about type coercion, type casting or any other munging of types that might happen on a typical read/write from your handy dandy ORM of choice. No no, this is the real deal, the bees knees, the elbows of a gazelle: actual type validation. Data quality is important to me, and should be important to you too. This library is an attempt at injecting some structure into your typical Rails/Sinatra app in order to keep your data clean and your system happy. Alright so when would this kind of thing be necessary? Let's see an example of how automagic type coercion can bite us. Let's say you're using Rails, and let's pretend for a moment that for some reason, you have some code that looks like this: A very important class: class ImportantResource < ActiveRecord::Base # Attribute named :settings with what we want to be an array # of important things encoded and stored in either # a nice postres json column or a text column store_accessor :settings, :array_of_things end And a matching very important controller: class ImportantResourceController < ApplicationController def create resource = ImportantResource.new # let's set the array_of_things to a parameter that is passed in # via a form or something resource.array_of_things = params[:array_of_things] # great, lets save that resource resource.save! # cool everything worked, this could not have gone better end end Oh no, george! What about if something other than array is passed to that parameter?? Well let's see what might happen. If params[:array_of_things] = 'a random string', any other method or objec