{"id":14955934,"url":"https://github.com/ahoshaiyan/mini-defender","last_synced_at":"2025-10-24T09:30:27.775Z","repository":{"id":65478436,"uuid":"543133757","full_name":"ahoshaiyan/mini-defender","owner":"ahoshaiyan","description":"Pragmatic data validation","archived":false,"fork":false,"pushed_at":"2024-08-29T19:10:31.000Z","size":1768,"stargazers_count":9,"open_issues_count":5,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-29T14:18:04.628Z","etag":null,"topics":["alternative","dry-validation","email","laravel","luhn","parameters","pragmatic","rails","ruby","strong","validation"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/mini_defender","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ahoshaiyan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-29T13:19:03.000Z","updated_at":"2024-08-29T19:10:27.000Z","dependencies_parsed_at":"2023-11-16T10:27:09.047Z","dependency_job_id":"ede89b36-b754-4efa-a9c3-d8371ca51cd1","html_url":"https://github.com/ahoshaiyan/mini-defender","commit_stats":{"total_commits":56,"total_committers":5,"mean_commits":11.2,"dds":0.0714285714285714,"last_synced_commit":"e630f51b667e3b0c0ef31e860754493be900ac2c"},"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahoshaiyan%2Fmini-defender","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahoshaiyan%2Fmini-defender/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahoshaiyan%2Fmini-defender/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahoshaiyan%2Fmini-defender/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahoshaiyan","download_url":"https://codeload.github.com/ahoshaiyan/mini-defender/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237944040,"owners_count":19391588,"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":["alternative","dry-validation","email","laravel","luhn","parameters","pragmatic","rails","ruby","strong","validation"],"created_at":"2024-09-24T13:12:02.637Z","updated_at":"2025-10-24T09:30:26.951Z","avatar_url":"https://github.com/ahoshaiyan.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MiniDefender\n\nA pragmatic approach to validation in Rails inspired by Laravel's Validator.\n\n\n## Installation\n\nInstall the gem and add to the application's Gemfile by executing:\n\n    $ bundle add mini_defender\n\nIf bundler is not being used to manage dependencies, install the gem by executing:\n\n    $ gem install mini_defender\n\n\n## Usage\n\nMini Defender allows the developer to quickly and easily validate incoming requests data as the verify first step\nin the request, alongside this Mini Defender also provides the following benefits:\n\n- Concise and easy to write validation logic\n- Validate complex and nested data structures\n- More than 80 validation rules available\n- Custom validation rules\n\nThe easiest way to use Mini Defender is to include the concern `MiniDefender::ValidatesInput` to your\n`ApplicationController` as follows:\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  include MiniDefender::ValidatesInput\nend\n```\n\nNow you will have access to a method called `validate!` which will accept a hash of keys and their respective rules.\n\n```ruby\n# frozen_string_literal: true\nclass BooksController \u003c ActionController::Base\n  def create\n    Book.create!(book_params)\n  end\n  \n  private\n  \n  def book_params\n    # Optional if you need easy access to rule definitions\n    rules = MiniDefender::Rules\n    \n    validate!({\n        'name' =\u003e 'string|required|max:255',\n        'type' =\u003e ['string', rules::In.new(Book::TYPES)],\n        'tags' =\u003e 'array',\n        'tags.*' =\u003e 'required|string|max:255',\n        'pages' =\u003e 'required|integer',\n        'author' =\u003e 'required|hash',\n        'author.name' =\u003e 'required|string',\n        'author.email' =\u003e 'email',\n    }, true)\n  end\nend\n```\n\nThe `validate!` method accepts two arguments, the first is a hash of keys and rules and the second a boolean to indicate\nif you want the values to be coerced to the type indicated by rule, i.e. `integer`.\n\nSuppose we passed the following input to our app:\n\n```json\n{\n  \"name\": \"The Story of Some Guy\",\n  \"type\": \"Biography\",\n  \"tags\": [\"inspiring\", \"business\"],\n  \"pages\": \"200\",\n  \"author\": {\n    \"name\": \"Some Guy himself\",\n    \"random_field\": \"hello i can haz hakc?\"\n  }\n}\n```\n\nNotice that we have passed a string `\"200\"` to page instead of `200` and added an extra field to `author` called `random_field`.\nWe didn't also provide an `email` field.\n\nThe validation will pass, since `email` is not optional, and we will get the following `Hash` as a result:\n\n```Ruby\n{\n  'name' =\u003e 'The Story of Some Guy',\n  'type' =\u003e 'Biography',\n  'tags' =\u003e %w[inspiring business],\n  'pages' =\u003e 200,\n  'author' =\u003e {\n    'name' =\u003e 'Some Guy himself'\n  }\n}\n```\n\nYou can see that `pages` has the value converted to `200` (integer), since we chose to coerce by passing `true` to `validate!`.\nYou can also see that Mini Defender left out the key `random_field` as it was not a part of our validations.\n\n\n## Rendering Errors\n\nWhen the validation fails, an error of type `MiniDefender::ValidationError` will be raised.\n\nYou can either handle the error using `rescue` or add a global `rescue_from` to handle the errors throughout the application.\n\n\n## Outside of a Controller\n\nThe `validate!` method is actually the following four lines of code:\n\n```ruby\ndef validate!(rules, coerced = false)\n    data = params.to_unsafe_hash.deep_stringify_keys\n    validator = MiniDefender::Validator.new(rules, data)\n    validator.validate!\n    coerced ? validator.coerced : validator.data\nend\n```\n\nYou can use Mini Defender out side of the controller by creating a new instance of `MiniDefender::Validator` and calling\nany of the methods on it, see [validator.rb](./lib/mini_defender/validator.rb) for the API.\n \n\n## Rules\n\nMini Defender tries to implement the same set of rules provided by Laravel, you can see the available rules here at\n[lib/mini_defender/rules](./lib/mini_defender/rules)\n\n\n## Add a Custom Rule\n\nTo implement your custom rules, you need to create a new class that inherent from `MiniDefender::Rule` and implement\nat least the following:\n\n```ruby\nclass MyAwesomeRule \u003c MiniDefender::Rule\n  def self.signature\n    'all_caps'\n  end\n\n  def passes?(attribute, value, validator)\n    value.is_a?(String) \u0026\u0026 /^[A-Z]+$/.match?(value)\n  end\n\n  def message(attribute, value, validator)\n    'ONLY CAPS ALLOWED!!!'\n  end\nend\n```\n\nAfter creating the class, you can register it as follows:\n\n```ruby\nMiniDefender::RulesFactory.register(MyAwesomeRule)\n```\n\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/ahoshaiyan/mini_defender.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](./LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahoshaiyan%2Fmini-defender","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahoshaiyan%2Fmini-defender","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahoshaiyan%2Fmini-defender/lists"}