项目作者: mtarnovan

项目描述 :
Collection of validations for Cod Numeric Personal (CNP), Cod de identificare fiscală (CIF) and IBAN (only Romanian format, as published by Romanian National Bank
高级语言: Ruby
项目地址: git://github.com/mtarnovan/romanianvalidators.git
创建时间: 2012-11-12T20:57:56Z
项目社区:https://github.com/mtarnovan/romanianvalidators

开源协议:MIT License

下载


Romanian Validators Build Status Gem Version Code Climate

Validators for:

  • Cod Numeric Personal (CNP)
  • Cod de identificare fiscală (CIF) and
  • IBAN (only Romanian format as published by Romanian National Bank).
  • BIC

Extracted from openapi.ro, business APIs for Romanian developers.
Includes ActiveModel validators.

Installation

Include it in your Gemfile. If you just want the validation, without ActiveModel, use:

  1. gem 'romanianvalidators'

If you want ActiveModel integration, use instead:

  1. gem 'romanianvalidators', require: 'romanian_validators/active_model'

Has no other dependency (other than ActiveModel if you require it).

Tested with MRI 1.9.3, 2.0, 2.1, 2.3, REE, Rubinius and JRuby (see .travis.yml)

Usage

Without ActiveModel, just call the valid? method on the corresponding module

  1. > RomanianValidators::Cif.valid?(13548146)
  2. => true
  3. > RomanianValidators::Iban.valid?(123)
  4. => false

With ActiveModel, include it in your model

  1. include RomanianValidators::ActiveModel::Validations

then use like this:

  1. validates :my_attribute, cif: true

Example:

  1. class User
  2. include ActiveModel::Model
  3. include RomanianValidators::ActiveModel::Validations
  4. attr_accessor :cnp, :company_cif
  5. validates :cnp, cnp: { message: 'This is not a valid CNP' }
  6. validates :company_cif, cif: true
  7. end
  8. > u = User.new(cnp: 123, company_cif: 123)
  9. => #<User:0x007fbf7f959b38 @cnp=123, @company_cif=123>
  10. > u.valid?
  11. => false
  12. > u.errors
  13. => #<ActiveModel::Errors:0x007fbf80958548 @base=#<User:0x007fbf7f959b38 @cnp=123, @company_cif=123, @validation_context=nil, @errors=#<ActiveModel::Errors:0x007fbf80958548 ...>>, @messages={:cnp=>["This is not a valid CNP"], :company_cif=>["is invalid"]}, @details={:cnp=>[{:error=>"This is not a valid CNP"}], :company_cif=>[{:error=>:invalid}]}>

For CIFs, a (lazy) enumerator is provided. This enumerator is significantly
more efficient than iterating over a range of numbers and filtering valid CIFs, because
it generates the control digit from cif % 10 directly.

Example:

  1. > RomanianValidators::Cif.enumerator(1).take(10).to_a
  2. => [19, 27, 35, 43, 51, 60, 78, 86, 94, 108]
  3. > RomanianValidators::Cif.enumerator(1_000_000, :down).take(10).to_a
  4. => [999993, 999985, 999977, 999969, 999950, 999942, 999934, 999926, 999918, 999900]

Upgrading

If upgrading from 0.1.x, note that the ActiveModel validations are now in a separate module so you need to add
include RomanianValidators::ActiveModel::Validations in your models. Additionaly you need to add a require in your Gemfile (again, only if using ActiveModel):

  1. gem 'romanianvalidators', require: 'romanianvalidators/active_model'

Changelog

0.2.0

  • moved validations from ActiveModel::Validations to RomanianValidators::ActiveModel::Validations.
    This module must be manually required in models.
  • added a lazy enumerator that generates valid CIFs: RomanianValidators::Cif.enumerator(start, direction = :up)

Copyright (c) 2007-2018 Mihai Târnovan. MIT LICENSE. See LICENSE for details.