{"id":28932994,"url":"https://github.com/davyjoneslocker/ruby-context_validations","last_synced_at":"2026-03-05T14:31:10.032Z","repository":{"id":8328881,"uuid":"9881908","full_name":"DavyJonesLocker/ruby-context_validations","owner":"DavyJonesLocker","description":"Context Aware Validations for Rails","archived":false,"fork":false,"pushed_at":"2016-08-26T14:36:35.000Z","size":33,"stargazers_count":65,"open_issues_count":0,"forks_count":11,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-06-05T04:40:34.209Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":false,"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/DavyJonesLocker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-06T07:47:48.000Z","updated_at":"2022-08-22T08:09:20.000Z","dependencies_parsed_at":"2022-08-07T02:30:45.414Z","dependency_job_id":null,"html_url":"https://github.com/DavyJonesLocker/ruby-context_validations","commit_stats":null,"previous_names":["dockyard/context_validations"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/DavyJonesLocker/ruby-context_validations","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavyJonesLocker%2Fruby-context_validations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavyJonesLocker%2Fruby-context_validations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavyJonesLocker%2Fruby-context_validations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavyJonesLocker%2Fruby-context_validations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DavyJonesLocker","download_url":"https://codeload.github.com/DavyJonesLocker/ruby-context_validations/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DavyJonesLocker%2Fruby-context_validations/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261330134,"owners_count":23142484,"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":[],"created_at":"2025-06-22T17:10:34.499Z","updated_at":"2026-03-05T14:31:09.554Z","avatar_url":"https://github.com/DavyJonesLocker.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ContextValidations #\n\n[![Build Status](https://secure.travis-ci.org/dockyard/context_validations.png?branch=master)](http://travis-ci.org/dockyard/context_validations)\n[![Dependency Status](https://gemnasium.com/dockyard/context_validations.png?travis)](https://gemnasium.com/dockyard/context_validations)\n[![Code Climate](https://codeclimate.com/github/dockyard/context_validations.png)](https://codeclimate.com/github/dockyard/context_validations)\n\nContext based validations for model instances.\n\n## Looking for help? ##\n\nIf it is a bug [please open an issue on GitHub](https://github.com/dockyard/context_validations/issues).\n\n## Installation ##\n\nIn your `Gemfile`\n\n```ruby\ngem 'context_validations'\n```\n\nYou can either mixin the modules on a case-by-case basis or make the\nchanges global:\n\n### Case-by-case ###\n\n```ruby\n# model\nclass User \u003c ActiveModel::Base\n  include ContextValidations::Model\nend\n\n# controller\nclass UsersController \u003c ApplicationController\n  include ContextValidations::Controller\nend\n```\n\n### Global ###\nCreate an initializer: `config/initializers/context_validations.rb`\n\n```ruby\nclass ActiveRecord::Base\n  include ContextValidations::Model\nend\n\nclass ActionController::Base\n  include ContextValidations::Controller\nend\n```\n\n## Usage ##\n\n```ruby\nclass UserController \u003c ApplicationController\n  include ContextValidations::Controller\n\n  def create\n    @user = User.new(user_params)\n    @user.validations = validations(:create)\n\n    if @user.save\n      # happy path\n    else\n      # sad path\n    end\n  end\n\n  private\n\n  def create_validations\n    validates :password, :presence =\u003e true\n  end\n\n  def base_validations\n    validates :first_name, :last_name, :presence =\u003e true\n    validates :password, :confirmation =\u003e true\n    validates :email, :uniqueness =\u003e true, :format =\u003e EmailFormat\n  end\nend\n\nclass User \u003c ActiveRecord::Base\n  include ContextValidations::Model\nend\n```\n\n### Controllers ###\nIn the above example we just call `validations` and pass the context. We\nset the result to `#validations=` on the model.\n\nWhile this does introduce some complexity into our controllers it frees\nus from the mental gymnastics of conditional validators and state flags.\n\nThe corresponding `#{context}_validations` method, in this case\n`create_validations` defines the validations that will be used. Call the\n`#validates` method just like you would in model validations, the API is\nidentical.\n\nA `#base_validations` method is always called prior to\n`#{context}_validations` that will allow you to group together common\nvalidations. The result of these methods appends onto a `@validations`\narray.\n\nIf you are using `Ruby 2.0+` you can use implicit contexts:\n\n```ruby\ndef create\n  @user = User.new(user_params)\n  # Notice we are only calling the #validations method and not passing\n  # context. In this example the context is derived from the calling\n  # method `create`\n  @user.validations = validations  \nend\n\nprivate\n\ndef create_validations\n  ...\nend\n```\n\n### Models ###\nWhen the `ContextValidations::Model` module is mixed into the model all\nof the validation callbacks are removed from that model.\n\nBecause we are setting the validations on the instance you should not\nuse `ActiveRecord::Base.create` as we do not want to allow the\nvalidations to be set via mass-assignment. This does introduce an extra\nstep in some places but it shouldn't be that big of a deal.\n\n## Testing ##\n\nCurrently only `MiniTest` is supported. We are open to pull requests for supporting additional test frameworks but we only work with `MiniTest` \nso we probably won't go out of the way to support something we're not using.\n\nWe highly recommend using [ValidAttribute](https://github.com/bcardarella/valid_attribute) to test your validations. The following example is done using\n`ValidAttribute`.\n\n### MiniTest ###\n\nIn `/test/test_helper.rb`:\n\n```ruby\nrequire 'context_validations/minitest'\n```\n\nYou are given access to a `#validations_for(:action_name)` method. You should pass the action in your\ncontroller that is the context of the validations and use the `#validations=` setter on the model.\n\nThis is a common example of how to test:\n\n```ruby\nrequire 'test_helper'\n\ndescribe UserController do\n  context 'create' do\n    subject { User.new(:password =\u003e 'password', :validations =\u003e validations_for(:create)) }\n    it { must have_valid(:name).when('Brian Cardarella') }\n    it { wont have_valid(:name).when(nil, '') }\n  end\nend\n``` \n\n## ClientSideValidations Support ##\n\nThe [ClientSideValidations](https://github.com/bcardarella/client_side_validations) gem is fully supported.\n\n## Authors ##\n\n* [Brian Cardarella](http://twitter.com/bcardarella)\n\n[We are very thankful for the many contributors](https://github.com/dockyard/context_validations/graphs/contributors)\n\n## Versioning ##\n\nThis gem follows [Semantic Versioning](http://semver.org)\n\n## Want to help? ##\n\nPlease do! We are always looking to improve this gem. Please see our\n[Contribution Guidelines](https://github.com/dockyard/context_validations/blob/master/CONTRIBUTING.md)\non how to properly submit issues and pull requests.\n\n## Legal ##\n\n[DockYard](http://dockyard.com), LLC \u0026copy; 2013\n\n[@dockyard](http://twitter.com/dockyard)\n\n[Licensed under the MIT license](http://www.opensource.org/licenses/mit-license.php)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavyjoneslocker%2Fruby-context_validations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavyjoneslocker%2Fruby-context_validations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavyjoneslocker%2Fruby-context_validations/lists"}