{"id":20252744,"url":"https://github.com/pragmarb/pragma-contract","last_synced_at":"2026-05-10T11:37:47.190Z","repository":{"id":56888647,"uuid":"73838646","full_name":"pragmarb/pragma-contract","owner":"pragmarb","description":"Like form objects but, you know, for APIs.","archived":false,"fork":false,"pushed_at":"2020-01-08T22:47:07.000Z","size":48,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-24T19:23:50.797Z","etag":null,"topics":["api","contract","ecosystem","pragma","ruby","ruby-on-rails","trailblazer","validation"],"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/pragmarb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2016-11-15T17:32:02.000Z","updated_at":"2020-01-08T22:47:09.000Z","dependencies_parsed_at":"2022-08-20T23:40:31.874Z","dependency_job_id":null,"html_url":"https://github.com/pragmarb/pragma-contract","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pragmarb%2Fpragma-contract","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pragmarb%2Fpragma-contract/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pragmarb%2Fpragma-contract/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pragmarb%2Fpragma-contract/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pragmarb","download_url":"https://codeload.github.com/pragmarb/pragma-contract/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241703743,"owners_count":20006248,"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":["api","contract","ecosystem","pragma","ruby","ruby-on-rails","trailblazer","validation"],"created_at":"2024-11-14T10:19:15.230Z","updated_at":"2026-05-10T11:37:42.165Z","avatar_url":"https://github.com/pragmarb.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pragma::Contract\n\n[![Build Status](https://travis-ci.org/pragmarb/pragma-contract.svg?branch=master)](https://travis-ci.org/pragmarb/pragma-contract)\n[![Coverage Status](https://coveralls.io/repos/github/pragmarb/pragma-contract/badge.svg?branch=master)](https://coveralls.io/github/pragmarb/pragma-contract?branch=master)\n[![Maintainability](https://api.codeclimate.com/v1/badges/e51e8d7489eb72ab97ba/maintainability)](https://codeclimate.com/github/pragmarb/pragma-contract/maintainability)\n\nContracts are form objects on steroids for your JSON API.\n\nThey are built on top of [Reform](https://github.com/apotonick/reform).\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'pragma-contract'\n```\n\nAnd then execute:\n\n```console\n$ bundle\n```\n\nOr install it yourself as:\n\n```console\n$ gem install pragma-contract\n```\n\n## Usage\n\nTo create a contract, simply inherit from `Pragma::Contract::Base`:\n\n```ruby\nmodule API\n  module V1\n    module Article\n      module Contract\n        class Base \u003c Pragma::Contract::Base\n          property :title\n          property :body\n        end\n      end\n    end\n  end\nend\n```\n\nSince Pragma::Contract is built on top of [Reform](https://github.com/apotonick/reform), you should\nconsult [its documentation](http://trailblazer.to/gems/reform/) for the basic usage of contracts;\nthe rest of this section only covers the features provided specifically by Pragma::Contract.\n\n### Coercion\n\nPragma::Contract supports Reform coercion through the [dry-types](https://github.com/dry-rb/dry-types)\ngem.\n\nYou can access types with the `Pragma::Contract::Types` module.\n\n```ruby\nmodule API\n  module V1\n    module Article\n      module Contract\n        class Base \u003c Pragma::Contract::Base\n          property :title, type: Pragma::Contract::Types::Coercible::String\n          property :body, type: Pragma::Contract::Types::Coercible::String\n          property :published_at, type: Pragma::Contract::Types::Form::Date\n        end\n      end\n    end\n  end\nend\n```\n\nHelpers are also provided as a shorthand syntax:\n\n```ruby\nmodule API\n  module V1\n    module Article\n      module Contract\n        class Base \u003c Pragma::Contract::Base\n          property :title, type: coercible(:string)\n          property :body, type: coercible(:string)\n          property :published_at, type: form(:date)\n        end\n      end\n    end\n  end\nend\n```\n\n### Model finders\n\nThis is a common pattern in API contracts:\n\n```ruby\nmodule API\n  module V1\n    module Invoice\n      module Contract\n        class Base \u003c Pragma::Contract::Base\n          property :customer\n\n          def customer=(val)\n            super ::Customer.find_by(id: val)\n          end\n        end\n      end\n    end\n  end\nend\n```\n\nPragma::Contract provides a shorthand syntax:\n\n```ruby\nmodule API\n  module V1\n    module Invoice\n      module Contract\n        class Base \u003c Pragma::Contract::Base\n          property :customer, type: Customer\n        end\n      end\n    end\n  end\nend\n```\n\nYou can also specify a custom column to find by!\n\n```ruby\nmodule API\n  module V1\n    module Invoice\n      module Contract\n        class Base \u003c Pragma::Contract::Base\n          property :customer, type: Customer, by: :email\n        end\n      end\n    end\n  end\nend\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/pragmarb/pragma-contract.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpragmarb%2Fpragma-contract","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpragmarb%2Fpragma-contract","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpragmarb%2Fpragma-contract/lists"}