{"id":18022122,"url":"https://github.com/mdespuits/validates_formatting_of","last_synced_at":"2025-08-05T09:33:00.417Z","repository":{"id":1913612,"uuid":"2841118","full_name":"mdespuits/validates_formatting_of","owner":"mdespuits","description":"Common Rails validations wrapped in a gem.","archived":false,"fork":false,"pushed_at":"2015-05-15T17:59:13.000Z","size":437,"stargazers_count":91,"open_issues_count":4,"forks_count":13,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-08-01T11:56:55.445Z","etag":null,"topics":["gem","rails","ruby","validation-library"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/validates_formatting_of","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mdespuits.png","metadata":{"files":{"readme":"README.markdown","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-11-24T05:02:39.000Z","updated_at":"2023-03-06T20:01:04.000Z","dependencies_parsed_at":"2022-09-08T11:50:15.183Z","dependency_job_id":null,"html_url":"https://github.com/mdespuits/validates_formatting_of","commit_stats":null,"previous_names":["mattdbridges/validates_formatting_of"],"tags_count":25,"template":false,"template_full_name":null,"purl":"pkg:github/mdespuits/validates_formatting_of","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdespuits%2Fvalidates_formatting_of","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdespuits%2Fvalidates_formatting_of/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdespuits%2Fvalidates_formatting_of/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdespuits%2Fvalidates_formatting_of/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mdespuits","download_url":"https://codeload.github.com/mdespuits/validates_formatting_of/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdespuits%2Fvalidates_formatting_of/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268872281,"owners_count":24321473,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-05T02:00:12.334Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem","rails","ruby","validation-library"],"created_at":"2024-10-30T06:12:04.315Z","updated_at":"2025-08-05T09:33:00.330Z","avatar_url":"https://github.com/mdespuits.png","language":"Ruby","readme":"[![Build Status](https://secure.travis-ci.org/mattdbridges/validates_formatting_of.png)](http://travis-ci.org/mattdbridges/validates_formatting_of)\n[![Dependency Status](https://gemnasium.com/mattdbridges/validates_formatting_of.png?travis)](https://gemnasium.com/mattdbridges/validates_formatting_of)\n[![Code Climate](https://codeclimate.com/github/mattdbridges/validates_formatting_of.png)](https://codeclimate.com/github/mattdbridges/validates_formatting_of)\n\n# Validates Formatting Of\n\nThe `validates_formatting_of` gem adds several convenient methods to validate things such as emails, urls, and phone numbers in a Rails application.\n\n# Supported Ruby Versions\n\n* 1.9.3\n* 2.0.0\n* 2.1.0\n* 2.2.0\n\n# Installation\n\nTo install `validates_formatting_of`, add the following to your `Gemfile`:\n\n    gem 'validates_formatting_of'\n\nThen bundle install:\n\n    bundle install\n\n# Usage\n\nUsing `validates_formatting_of` is as simple as using Rails' built-in validation methods in models.\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  validates_formatting_of :email\nend\n```\n\nIf the name of the column is identical to the validation method you would use, it auto-selects that validation method. Thus, this is a shortcut for the following:\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  validates_formatting_of :email, :using =\u003e :email\nend\n```\n\nThis call will ensure that the user-provided email is a valid email. This way, you will not need to find or write your own regex to validate. All of that logic is contained within `validates_formatting_of`.\n\n# Rails validation options still available\n\nYou can still add the following options when using `validates_formatting_of`:\n\n* `:allow_nil`\n* `:allow_blank`\n* `:on`\n* `:if`\n* `:unless`\n* You can also specify the `:message` option to use a custom validation message.\n\n# Share your Validations\n\nSay, for example, you have identical plain-old regex validations for different columns or even on different models entirely. You can easily alleviate this problem by simply adding the validation(s) in an initializer file.\n\nWhile very unrealistic, these examples should serve their purpose in demonstrating this ability.\n\n```ruby\n# config/initializers/validates_formatting_of.rb\nValidatesFormattingOf::Method.add :loweralpha, /[a-z]/, \"must be lowercase and no spaces\"\nValidatesFormattingOf::Method.add :upperalpha, /[A-Z]/, \"must be uppercase and no spaces\"\nValidatesFormattingOf::Method.add :weak_password, /[a-zA-Z0-9]{8,}/, \"must contain only letters and numbers and be at least 8 characters long\".\n```\n\nKeep in mind, since this gem is only ActiveModel-dependent, you should be able to do this in whatever non-Rails application classes you have that include `ActiveModel::Validations` in some way (and that extend the `ValidatesFormattingOf::ModelAdditons` module).\n\n# Built-in Validations\n\n`validates_formatting_of` has the following built-in validations:\n\n```ruby\nclass ExampleModel \u003c ActiveRecord::Base\n  validates_formatting_of :email, :using =\u003e :email      # Email\n  validates_formatting_of :first_name, :using =\u003e :alpha # Letters\n  validates_formatting_of :website, :using =\u003e :url      # URLs\n  validates_formatting_of :text, :using =\u003e :alphanum    # Alpha-numeric\n  validates_formatting_of :zipcode, :using =\u003e :us_zip   # US Zip Code\n  validates_formatting_of :phone, :using =\u003e :us_phone   # US Phone Numbers\n  validates_formatting_of :ip, :using =\u003e :ip_address_v4 # IPv4\n  validates_formatting_of :ssn, :using =\u003e :ssn          # Social Security Numbers\n  validates_formatting_of :color, :using =\u003e :hex_color  # Hexadecimal Colors\n  validates_formatting_of :amount, :using =\u003e :dollars   # Dollars\n\n  # Credit Card (Visa, Mastercard, Discover, and American Express)\n  validates_formatting_of :cc, :using =\u003e :credit_card\nend\n```\n\n**Note**: If you use `client_side_validations` then you need to use the following validation method:\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  validates_formatting_of :email, :using =\u003e :simple_email\nend\n```\n\n### Plain Old Ruby Objects\n\nIf you are not using ActiveRecord or want to use it on some other type of object, this is the least you need to get validations running.\n\n```ruby\nclass User\n  include ActiveModel::Validations\n  extend ValidatesFormattingOf::ModelAdditions\n\n  attr_accessor :email\n\n  validates_formatting_of :email, :using =\u003e :email\nend\n```\n\n# Over-writable\n\nIf, for any reason, you want to overwrite the regex specified in the gem, you can using the `:with` option just like in ActiveModel's built-in `validates_format_of`.\n\n```ruby\nclass Person \u003c ActiveRecord::Base\n  validates_formatting_of :first_name, :with =\u003e /[A-Z]/i\nend\n```\n\n# Development and Contribution\n\nTo add a simple regex validation, all that is necessary is to add a single line to the `ValidatesFormattingOf::Method` module in the `lib/method.rb` file (with a line or two of documentation explaining it's use).\n\nFor example:\n\n```ruby\nmodule ValidatesFormattingOf::Method\n  # This :validation_name method is for example purposes only.\n  # You can use this in production, but it would require a value of 'example regex' to pass.\n  add :validation_name, %r{example regex}iux, \"is a message for validation method :validation_name\"\nend\n```\n\n1. Fork it\n2. Make it awesome (with passing tests)\n3. Create a new Pull Request.\n\n# Have Ideas?\n\nDo you use a particular pattern on a regular basis that isn't here or you would like to contribute? For now, [create a new issue](https://github.com/mattdbridges/validates_formatting_of/issues/new) in the issue tracker. I would be more than happy to consider adding it to the project.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdespuits%2Fvalidates_formatting_of","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmdespuits%2Fvalidates_formatting_of","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdespuits%2Fvalidates_formatting_of/lists"}