{"id":15405144,"url":"https://github.com/fnando/validators","last_synced_at":"2025-04-06T14:11:58.915Z","repository":{"id":48140404,"uuid":"1068987","full_name":"fnando/validators","owner":"fnando","description":"Some ActiveModel/ActiveRecord validators","archived":false,"fork":false,"pushed_at":"2022-07-22T01:45:32.000Z","size":2023,"stargazers_count":97,"open_issues_count":2,"forks_count":13,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-30T12:09:20.836Z","etag":null,"topics":["activerecord","activerecord-validators","rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fnando.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["fnando"]}},"created_at":"2010-11-10T17:41:35.000Z","updated_at":"2025-02-01T23:34:55.000Z","dependencies_parsed_at":"2022-07-29T01:18:21.386Z","dependency_job_id":null,"html_url":"https://github.com/fnando/validators","commit_stats":null,"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fvalidators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fvalidators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fvalidators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fvalidators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fnando","download_url":"https://codeload.github.com/fnando/validators/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247492557,"owners_count":20947545,"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","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":["activerecord","activerecord-validators","rails"],"created_at":"2024-10-01T16:15:13.492Z","updated_at":"2025-04-06T14:11:58.890Z","avatar_url":"https://github.com/fnando.png","language":"Ruby","readme":"# Validators\n\n[![Build Status](https://travis-ci.org/fnando/validators.svg)](https://travis-ci.org/fnando/validators)\n[![Code Climate](https://codeclimate.com/github/fnando/validators/badges/gpa.svg)](https://codeclimate.com/github/fnando/validators)\n[![Gem](https://img.shields.io/gem/v/validators.svg)](https://rubygems.org/gems/validators)\n[![Gem](https://img.shields.io/gem/dt/validators.svg)](https://rubygems.org/gems/validators)\n\nAdd some nice ActiveModel/ActiveRecord validators.\n\n## Installation\n\n```\ngem install validators\n```\n\nThen add it to your Gemfile:\n\n```ruby\ngem \"validators\"\n```\n\n## Usage\n\n**Notice**: Some validators will require additional dependencies; whenever you\nget a message like\n`gem-name is not part of the bundle. Add it to your project's Gemfile.`, all you\nhave to do is adding that gem to your own project's Gemfile.\n\n### validates_email_format_of\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  # old fashion\n  validates_email_format_of :email\n\n  # alternative way\n  validates :email, email: true\nend\n```\n\nBy default, it rejects disposable e-mails (e.g. mailinator). This loads a lot of\ndata (~1.7MB), but you can disable this validation by setting\n`disposable: true`.\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  validates_email_format_of :email, disposable: true\nend\n```\n\nYou can also validate the tld.\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  validates_email_format_of :email, tld: true\nend\n```\n\n### validates_url_format_of\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  validates_url_format_of :site\n\n  # validates TLD against list of valid TLD.\n  # Loads ~10KB of text.\n  validates_url_format_of :site, tld: true\nend\n```\n\n### validates_ownership_of\n\n```ruby\nclass Task \u003c ActiveRecord::Base\n  belongs_to :user\n  belongs_to :category\n\n  validates_ownership_of :category, with: :user\nend\n\nuser = User.find(1)\nanother_user = User.find(2)\n\nuser_category = user.categories.first\nanother_user_category = another_user.categories.first\n\ntask = user.tasks.create(category: user_category)\ntask.valid?\n#=\u003e true\n\ntask = user.tasks.create(category: another_user_category)\ntask.valid?\n#=\u003e false\n```\n\n### validates_ip_address\n\n```ruby\nclass Server \u003c ActiveRecord::Base\n  validates_ip_address :address\n  validates_ip_address :address, only: :v4\n  validates_ip_address :address, only: :v6\nend\n```\n\n### validates_datetime\n\n```ruby\nclass Server \u003c ActiveRecord::Base\n  validates_datetime :starts_at\n  validates_datetime :ends_at, after: :starts_at, if: :starts_at?\n  validates_datetime :ends_at, after: :now\n  validates_datetime :ends_at, before: :today\n\n  validates :starts_at, datetime: true\nend\n```\n\n### validates_cpf_format_of\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  validates_cpf_format_of :document\nend\n```\n\n### validates_cnpj_format_of\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  validates_cnpj_format_of :document\nend\n```\n\n### validates_hostname\n\nRules:\n\n- maximum length of hostname is 255 characters\n- maximum length of each hostname label is 63 characters\n- characters allowed in hostname labels are a-z, A-Z, 0-9 and hyphen\n- labels do not begin or end with a hyphen\n- labels do not consist of numeric values only\n- TLD validation (optional)\n\n```ruby\nclass Server \u003c ActiveRecord::Base\n  validates_hostname :hostname\n  validates_hostname :hostname, tld: true\nend\n```\n\n### validates_username / validates_subdomain\n\nA valid username/subdomain follows the hostname label validation:\n\n- maximum length is 63 characters\n- allowed characters are a-z, A-Z, 0-9 and hyphen\n- cannot begin or end with a hyphen\n- cannot consist of numeric values only\n\nThe compiled list will be used for both username and subdomain validations. This\nvalidation loads ~20KB of text.\n\n```ruby\nclass Server \u003c ActiveRecord::Base\n  validates_subdomain :subdomain\nend\n\nclass User \u003c ActiveRecord::Base\n  validates_username :username\nend\n```\n\nYou can also provide your own list if you want. Any string that starts with `/`\nwill be parsed with `Regexp.compile`.\n\n```ruby\nReservedUsernames = Validators::ReservedHostnames.parse_list([\n  \"www\",\n  \"/www[0-9-]+/\"\n])\n\nclass User \u003c ActiveRecord::Base\n  validates_username :username, in: ReservedUsernames\nend\n```\n\nTo disable the reserved validation, use `reserved: false`.\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  validates_username :username, reserved: false\nend\n```\n\n## Maintainer\n\n- [Nando Vieira](http://nandovieira.com)\n\n## License\n\n(The MIT License)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the 'Software'), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","funding_links":["https://github.com/sponsors/fnando"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fvalidators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffnando%2Fvalidators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fvalidators/lists"}