datacheck-kit
A small, zero-dependency toolkit for validating and formatting the kinds of data that show up in almost every form or import script: email addresses, phone numbers, URLs, IBANs, credit card numbers, postal codes and slugs.
It exists because most projects end up copy-pasting the same handful of regexes and checksum functions over and over. This gem collects the ones worth reusing into one place, with tests and no runtime dependencies.
This is a Ruby port of datacheck-kit
(originally written for npm, also on PyPI
and Packagist) — same
functionality, adapted to Ruby conventions (?-suffixed predicates, nil
instead of other languages' null-likes).
Homepage: https://luckymisters.uk
Install
gem install datacheck-kitOr add to your Gemfile:
gem 'datacheck-kit'Usage
require 'datacheck-kit'
DataCheckKit::Email.valid?('user@example.com') # true
DataCheckKit::Email.normalize(' User@Example.COM ') # 'user@example.com'
DataCheckKit::Url.valid?('example.com') # false (no protocol)
DataCheckKit::Url.normalize('example.com/path/') # 'https://example.com/path/'
DataCheckKit::Phone.valid?('+44 20 7123 4567') # true
DataCheckKit::Phone.format_e164('+44 20 7123 4567') # '+442071234567'
DataCheckKit::Iban.valid?('NL91ABNA0417164300') # true
DataCheckKit::Iban.format('NL91ABNA0417164300') # 'NL91 ABNA 0417 1643 00'
DataCheckKit::CreditCard.valid?('4111 1111 1111 1111') # true (Luhn check)
DataCheckKit::CreditCard.mask('4111111111111111') # '**** **** **** 1111'
DataCheckKit::PostalCode.valid?('1011 AB', 'NL') # true
DataCheckKit::PostalCode.supported_countries # ['AT', 'CH', 'DE', ...]
DataCheckKit::Slug.make('Café Müller & Söhne') # 'cafe-muller-sohne'API
-
DataCheckKit::Email.valid?,.normalize -
DataCheckKit::Url.valid?,.normalize -
DataCheckKit::Phone.valid?,.format_e164— E.164-style numbers -
DataCheckKit::Iban.valid?,.format— mod-97 (ISO 7064) checksum -
DataCheckKit::CreditCard.valid?,.mask— Luhn algorithm -
DataCheckKit::PostalCode.valid?,.supported_countries— NL, PL, DE, FR, IT, AT, CH, GB, ES, FI, US DataCheckKit::Slug.make
Testing
bundle install
rake testLicense
MIT — see LICENSE.txt.