Code Monkey home page Code Monkey logo

validates_zipcode's Introduction

ValidatesZipcode Build Status Code Climate Gem Version security

Adds zipcode / postal code validation support to Rails (ActiveModel), considering postal code formats for mostly every country.

ValidatesZipcode currently support 233 country codes. Regex data taken from several sources, being the main source the CLDR database (release 27, around 159). Any other country's postal code will validate without errors.

ValidatesZipcode supports Rails >= 4.2 and Ruby >= 2.4. This gem could work in Rails 3.2 and Ruby 1.9.3 as well, yet unsupported; try v0.2 series if having trouble with later versions. Truffleruby is also tested, but no reports of working in production apps for now.

Installation

Add this line to your application's Gemfile:

gem 'validates_zipcode'

And then execute:

$ bundle

Or install it yourself as:

$ gem install validates_zipcode

Usage

With ActiveModel::Validations

validates_zipcode :zipcode

validates :zipcode, zipcode: true

ValidatesZipcode expects the model to have an attribute called country_alpha2 to contain the country code. You can provide your own country_code using :country_code option, or specify which attribute contains this information using :country_code_attribute option.

validates :zipcode, zipcode: { country_code: :es }

validates :zipcode, zipcode: { country_code_attribute: :my_country_code_column }

Error Messaging

If you need to localize the error message, just add this to your I18n locale file:

errors:
  messages:
    invalid_zipcode: Your zipcode error message.

You can override this on a per-model basis by passing in a :message key with the validation:

validates :zipcode, zipcode: { message: 'Your per-model zipcode error message.' }

Without ActiveModel::Validations

ValidatesZipcode.valid?('93108', 'ES')
# => true

Formatting

This gem can also be used for formatting zipcodes according to country specific rules.

ValidatesZipcode.format('Sw1A 2aA', 'UK')
# => 'SW1A 2AA'

If the zipcode is not valid an exception is raised.

ValidatesZipcode.format('Sw1A 2aA', 'FR')
# => raises ValidatesZipcode::InvalidZipcodeError

At the moment not every country is supported. See lib/validates_zipcode/formatter.rb to find all available countries.

Test data

In order to generate test data, we suggest using the regexp-examples gem This dependency will allow you to create examples as follows:

require "regexp-examples"

ValidatesZipcode::CldrRegexpCollection::ZIPCODES_REGEX[:ES].examples

# => ["00000", "00001", "00002", "00003", ..., "44443", "44444"]

ValidatesZipcode::CldrRegexpCollection::ZIPCODES_REGEX[:ES].random_example

# => "27072"

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Also, you should read and follow our Code of Conduct.

Contributors

To see the generous people who have contributed code, take a look at the contributors list.

Maintainers

License

Copyright (c) 2014 David Gil Pérez, released under the MIT license

validates_zipcode's People

Contributors

dgilperez avatar ojsdude avatar chloe-meister avatar nunosilva800 avatar adlersantos avatar tahanson avatar gerard76 avatar ericlarch avatar kpricorn avatar neerajkumar avatar harrylewis avatar dmitrybereza avatar thomaschiesa avatar devthiago avatar nrser avatar northeastprince avatar droycewagner avatar pedrosfdcarneiro avatar lucasfcunha avatar johnmeehan avatar geoffroh avatar dimroc avatar aiomaster avatar andychongyz avatar amirmingaliev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.