{"id":18580390,"url":"https://github.com/lulalala/adequate_errors","last_synced_at":"2025-04-10T10:31:57.500Z","repository":{"id":59150452,"uuid":"87804571","full_name":"lulalala/adequate_errors","owner":"lulalala","description":"Overcoming limitation of Rails model errors API","archived":false,"fork":false,"pushed_at":"2018-04-16T04:44:47.000Z","size":50,"stargazers_count":48,"open_issues_count":2,"forks_count":1,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-24T19:52:34.333Z","etag":null,"topics":["model-validation","rail","redesign","validation-errors"],"latest_commit_sha":null,"homepage":"","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/lulalala.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-10T11:46:32.000Z","updated_at":"2020-12-22T20:22:30.000Z","dependencies_parsed_at":"2022-09-14T04:01:24.035Z","dependency_job_id":null,"html_url":"https://github.com/lulalala/adequate_errors","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulalala%2Fadequate_errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulalala%2Fadequate_errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulalala%2Fadequate_errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulalala%2Fadequate_errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lulalala","download_url":"https://codeload.github.com/lulalala/adequate_errors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248145037,"owners_count":21055035,"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":["model-validation","rail","redesign","validation-errors"],"created_at":"2024-11-06T23:45:55.894Z","updated_at":"2025-04-10T10:31:57.171Z","avatar_url":"https://github.com/lulalala.png","language":"Ruby","readme":"# AdequateErrors\n\nOvercoming limitation of Rails model errors API:\n\n* fine-grained `where` query\n* object-oriented Error object\n* turn off message's attribute prefix.\n* lazy evaluation of messages\n\n## We want some of this as part of Rails\n\nI have opened a [pull request](https://github.com/rails/rails/pull/32313) to migrate some ideas to Rails. It's a great chance for you to shape the future of the API. Please join the discussion there :)\n\n## Introduction\n\nRails errors API is simple to use, but can be inadequate when coping with more complex requirements.\n\nExamples of how existing Rails API is limiting are listed here: http://lulalala.logdown.com/posts/2909828-adequate-errors\n\nThe existing API was originally a collection of message strings without much meta data, making it very restrictive. Though `details` hash was added later for storing meta information, many fundamental issues can not be fixed without altering the API and the architecture.\n\nThis gem redesigned the API, placing it in its own object, co-existing with existing Rails API. Thus nothing will break, allowing you to migrate the code one at a time.\n\n\n## Quick start\n\nTo access the AdequateErrors, call:\n\n    model.errors.adequate\n\nFrom this `Errors` object, many convenience methods are provided:\n\nReturn an array of AdequateErrors::Error objects, matching a condition:\n\n    model.errors.adequate.where(attribute:'title', :type =\u003e :too_short, length: 5)\n\nPrints out each error's full message one by one:\n\n    model.errors.adequate.each {|error| puts error.message }\n    \nReturn an array of all message strings:\n\n    model.errors.adequate.messages\n    \n## `Error` object\n\nAn `Error` object provides the following:\n\n* `attribute` is the model attribute the error belongs to.\n* `type` is the error type.\n* `options` is a hash containing additional information such as `:count` or `:length`.\n* `message` is the error message. It is full message by design.\n\n## `where` query\n\nUse `where` method to find errors matching different conditions. An array of Error objects are returned.\n\nTo find all errors of `title` attribute, pass it with `:attribute` key:\n\n    model.errors.adequate.where(:attribute =\u003e :title)\n    \nYou can also filter by error type using the `:type` key:\n\n    model.errors.adequate.where(:type =\u003e :too_short)\n    \nCustom attributes passed can also be used to filter errors:\n\n    model.errors.adequate.where(:attribute =\u003e :title, :type =\u003e :too_short, length: 5)\n    \n    \n## `include?`\n\nSame as Rails, provide the attribute name to see if that attribute has errors.\n\n## `add`, `delete`\n\nSame as built-in counterparts.\n\n## `import`\n\nFor copying an error from inner model to outer model, such as form object. This ensures lazy message generation can still reference all information that the inner error has.\n\n```ruby\ninner_model.errors.adequate.each do |error|\n  errors.adequate.import(error)\nend\n```\n\nAttribute and type can be overriden in case when attribute does not exist in the outer model:\n\n```ruby\n  errors.adequate.import(error, attribute: :foo, type: :bar)\n```\n\n## Message and I18n\n\nError message strings reside under `adequate_errors` namespace. Unlike Rails, there is no global prefixing of attributes. Instead, `%{attribute}` is added into each error message when needed.\n\n```yaml\nen:\n  adequate_errors:\n    messages:\n      invalid: \"%{attribute} is invalid\"\n      inclusion: \"%{attribute} is not included in the list\"\n      exclusion: \"%{attribute} is reserved\"\n```\n\nThis allows omission of attribute prefix. You no longer need to attach errors to `:base` for that purpose.\n\nBuilt-in Rails error types already have been prefixed out of the box, but error types from other gems have to be handled manually by copying entries to the  `adequate_errors` namespace and prefixing with attributes.\n\nError messages are evaluated lazily, which means it can be rendered in a different locale at view rendering time.\n\n\n## `messages`\n\nReturns an array of all messages.\n\n    model.errors.adequate.messages\n\n## `messages_for`\n\nReturns an array of messages, filtered by conditions. Method argument is the same as `where`.\n\n    model.errors.adequate.messages_for(:attribute =\u003e :title, :type =\u003e :too_short, length: 5)\n    \n## Full documentation\n\nhttp://www.rubydoc.info/github/lulalala/adequate_errors\n\n## Note\n\nCalls to Rails' API are synced to AdequateErrors object, but not in reverse. Deprecated methods such as `[]=`, `get` and `set` are not sync'ed however.\n\nThe gem is developed from ActiveModel 5.1, but it should work with earlier versions.\n\n## We want to hear your issues too\n\nIf you also have issues with exsting API, share it by filing that issue here.\n\nWe collect use cases in issues and analyze the problem in wiki (publicly editable):\n\n[So come to our wiki, see what's going on, and join us!](https://github.com/lulalala/adequate_errors/wiki)\n\n---\n\nThis repo was called Rails Error API Redesign Initiative.  \nThis is a fan project and is not affiliated to Rails Core team,  \nbut my wish is that one day this can be adapted into Rails too.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flulalala%2Fadequate_errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flulalala%2Fadequate_errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flulalala%2Fadequate_errors/lists"}