{"id":25132220,"url":"https://github.com/mh4gf/graphql-ruby-constraint-directive","last_synced_at":"2025-04-23T20:43:49.237Z","repository":{"id":65153880,"uuid":"584708119","full_name":"MH4GF/graphql-ruby-constraint-directive","owner":"MH4GF","description":"Validate GraphQL Fields for graphql-ruby","archived":false,"fork":false,"pushed_at":"2023-01-04T06:53:55.000Z","size":26,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-18T21:11:20.102Z","etag":null,"topics":["graphql","graphql-ruby","ruby"],"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/MH4GF.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-03T10:04:53.000Z","updated_at":"2024-07-04T00:11:14.000Z","dependencies_parsed_at":"2023-02-02T02:16:02.690Z","dependency_job_id":null,"html_url":"https://github.com/MH4GF/graphql-ruby-constraint-directive","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MH4GF%2Fgraphql-ruby-constraint-directive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MH4GF%2Fgraphql-ruby-constraint-directive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MH4GF%2Fgraphql-ruby-constraint-directive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MH4GF%2Fgraphql-ruby-constraint-directive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MH4GF","download_url":"https://codeload.github.com/MH4GF/graphql-ruby-constraint-directive/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250513380,"owners_count":21443200,"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":["graphql","graphql-ruby","ruby"],"created_at":"2025-02-08T14:17:18.741Z","updated_at":"2025-04-23T20:43:49.217Z","avatar_url":"https://github.com/MH4GF.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GraphQL::Constraint::Directive\n[![Tests](https://github.com/MH4GF/graphql-ruby-constraint-directive/actions/workflows/main.yml/badge.svg)](https://github.com/MH4GF/graphql-ruby-constraint-directive/actions/workflows/main.yml)\n[![Gem Version](https://badge.fury.io/rb/graphql-constraint-directive.svg)](https://rubygems.org/gems/graphql-constraint-directive)\n\nAllows using @constraint as a directive to validate input data. Inspired by [Constraints Directives RFC](https://github.com/IvanGoncharov/graphql-constraints-spec) and OpenAPI.  \nThis gem is an implementation of [graphql-constraint-directive](https://github.com/confuser/graphql-constraint-directive) in Ruby.\n\n## Installation\n\nInstall the gem and add to the application's Gemfile by executing:\n\n    $ bundle add graphql-constraint-directive\n\n## Usage\n\nThis gem is an implemented as a plugin of [graphql-ruby](https://github.com/rmosolgo/graphql-ruby/). Attach to your schema as follows:\n\n```ruby\nclass MySchema \u003c GraphQL::Schema\n  use GraphQL::Constraint::Directive\nend\n```\n\nThen, set the directive to the argument for which you want to set validation:\n\n```ruby\nclass CreateBook \u003c BaseMutation\n  argument :title, String,\n           required: true,\n           directives: { GraphQL::Constraint::Directive::Constraint =\u003e { min_length: 1, max_length: 200 } }\n\n  field :book, Book, null: false\n\n  def resolve(title:)\n    # ...some codes\n    {\n      book: book\n    }\n  end\nend\n```\n\nThis will dump the following schema:\n\n```graphql\ninput CreateBookInput {\n  title: String! @constraint(minLength: 1, maxLength: 200)\n}\n\ntype Mutation {\n  createBook(\n    input: CreateBookInput!\n  ): CreateBookPayload\n}\n```\n\n## API\n### String\n#### minLength\n```@constraint(minLength: 5)```\nRestrict to a minimum length\n\n#### maxLength\n```@constraint(maxLength: 5)```\nRestrict to a maximum length\n\n#### startsWith(unimplemented)\n```@constraint(startsWith: \"foo\")```\nEnsure value starts with foo\n\n#### endsWith(unimplemented)\n```@constraint(endsWith: \"foo\")```\nEnsure value ends with foo\n\n#### contains(unimplemented)\n```@constraint(contains: \"foo\")```\nEnsure value contains foo\n\n#### notContains(unimplemented)\n```@constraint(notContains: \"foo\")```\nEnsure value does not contain foo\n\n#### pattern(unimplemented)\n```@constraint(pattern: \"^[0-9a-zA-Z]*$\")```\nEnsure value matches regex, e.g. alphanumeric\n\n### Int/Float\n#### min\n```@constraint(min: 3)```\nEnsure value is greater than or equal to\n\n#### max\n```@constraint(max: 3)```\nEnsure value is less than or equal to\n\n#### exclusiveMin(unimplemented)\n```@constraint(exclusiveMin: 3)```\nEnsure value is greater than\n\n#### exclusiveMax(unimplemented)\n```@constraint(exclusiveMax: 3)```\nEnsure value is less than\n\n#### multipleOf(unimplemented)\n```@constraint(multipleOf: 10)```\nEnsure value is a multiple\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/mh4gf/graphql-ruby-constraint-directive. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/graphql-ruby-constraint-directive/blob/main/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Graphql::Ruby::Constraint::Directive project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/graphql-ruby-constraint-directive/blob/main/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmh4gf%2Fgraphql-ruby-constraint-directive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmh4gf%2Fgraphql-ruby-constraint-directive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmh4gf%2Fgraphql-ruby-constraint-directive/lists"}