{"id":16487130,"url":"https://github.com/vsimko/node-graphql-constraint-lambda","last_synced_at":"2025-07-12T00:07:59.770Z","repository":{"id":33451516,"uuid":"137956124","full_name":"vsimko/node-graphql-constraint-lambda","owner":"vsimko","description":"GraphQL 'constraint' directive written in functional programming style","archived":false,"fork":false,"pushed_at":"2022-12-09T13:14:01.000Z","size":1646,"stargazers_count":11,"open_issues_count":10,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-03T02:08:01.173Z","etag":null,"topics":["constraints","directive","functional-style","graphql","graphql-directive","nodejs","ramda","validation"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/vsimko.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-19T23:29:13.000Z","updated_at":"2021-11-10T22:13:14.000Z","dependencies_parsed_at":"2023-01-15T00:58:50.572Z","dependency_job_id":null,"html_url":"https://github.com/vsimko/node-graphql-constraint-lambda","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/vsimko/node-graphql-constraint-lambda","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vsimko%2Fnode-graphql-constraint-lambda","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vsimko%2Fnode-graphql-constraint-lambda/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vsimko%2Fnode-graphql-constraint-lambda/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vsimko%2Fnode-graphql-constraint-lambda/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vsimko","download_url":"https://codeload.github.com/vsimko/node-graphql-constraint-lambda/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vsimko%2Fnode-graphql-constraint-lambda/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264916028,"owners_count":23682957,"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":["constraints","directive","functional-style","graphql","graphql-directive","nodejs","ramda","validation"],"created_at":"2024-10-11T13:32:47.082Z","updated_at":"2025-07-12T00:07:59.741Z","avatar_url":"https://github.com/vsimko.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# node-graphql-constraint-lambda\n\n[![Build Status](https://travis-ci.org/vsimko/node-graphql-constraint-lambda.svg?branch=master)](https://travis-ci.org/vsimko/node-graphql-constraint-lambda)\n[![npm module](https://badge.fury.io/js/node-graphql-constraint-lambda.svg)](https://www.npmjs.org/package/node-graphql-constraint-lambda)\n[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)\n\nGraphQL constraint directive written in functional programming style.\nThis directive provides declarative validation of GraphQL arguments.\n\n## Install\n\n```sh\nyarn add node-graphql-constraint-lambda\n# or\nnpm install node-graphql-constraint-lambda\n```\n\n## Usage\n\nExample GraphQL Schema:\n```graphql\ntype Query {\n  createUser (\n    name: String! @constraint(minLength: 5, maxLength: 40)\n    emailAddr: String @constraint(format: \"email\")\n    otherEmailAddr: String @constraint(format: \"email\", differsFrom: \"emailAddr\")\n    age: Int @constraint(min: 18)\n  ): User\n}\n```\n\nUse the constraint from your code:\n```js\n// when using es6 modules\nimport { constraint } from 'node-graphql-constraint-lambda'\n\n// when using commonjs\nconst { constraint } = require('node-graphql-constraint-lambda')\n\n// ... initialize your typeDefs and resolvers here ...\n\nconst server = new GraphQLServer({\n  typeDefs,\n  resolvers,\n  schemaDirectives: {\n    constraint\n  }\n  // ... additional graphql server config\n})\n// ... start your server\n```\n\nYou may need to declare the directive in the schema:\n\n```graphql\ndirective @constraint(\n  minLength: Int\n  maxLength: Int\n  startsWith: String\n  endsWith: String\n  contains: String\n  notContains: String\n  pattern: String\n  format: String\n  differsFrom: String\n  min: Float\n  max: Float\n  exclusiveMin: Float\n  exclusiveMax: Float\n  notEqual: Float\n) on ARGUMENT_DEFINITION\n```\n\n## API\n\n### Available constraints\nSee `stringValidators`, `numericValidators` and `formatValidator` mapping in [src/validators.js].\n\n### Available formats\nWe use some functions from the `validator` package.\nSee `format2fun` mapping in [src/validators.js].\n\n[src/validators.js]: https://github.com/vsimko/node-graphql-constraint-lambda/blob/master/src/validators.js\n\n\n## Customization\n\n### Default behavoir\n\nThe following code shows how the constraint directive is configured with default behaviour:\n```js\n// this code:\nimport { constraint } from 'node-graphql-constraint-lambda'\n\n// is equivalent to:\nimport {\n  prepareConstraintDirective,\n  defaultValidationCallback,\n  defaultErrorMessageCallback\n} from 'node-graphql-constraint-lambda'\n\nconst constraint = prepareConstraintDirective(\n  defaultValidationCallback, defaultErrorMessageCallback )\n\n```\n\n### Custom error messages\n\nError messages are generated using a **callback** function that by default\nshows a generic error message. It is possible to change this behavior\nby implementing a custom callback similar to this one:\n```js\nconst myErrorMessageCallback = ({ argName, cName, cVal, data }) =\u003e\n  `Error at field ${argName} in constraint ${cName}:${cVal}, data=${data}`\n```\n\nYou might also want to customize certain messages and to keep the default callback as a fallback for all other messages:\n```js\nconst myErrorMessageCallback = input =\u003e {\n  const { argName, cName, cVal, data } = input\n  if (/* decide whether to show custom message */)\n    return \"custom error message\" // based on input\n  else\n    return defaultErrorMessageCallback(input)\n}\n\nconst constraint = prepareConstraintDirective(\n  defaultValidationCallback, myErrorMessageCallback )\n```\n\n### Custom validation functions\n\nAlso the validation functions are implemented through a callback function.\nThe constraint directive comes with a set of useful defaults but if you\nwant to add your own validator, it can be done as follows:\n```js\nimport {\n  createValidationCallback,\n  prepareConstraintDirective,\n  defaultValidators } from 'node-graphql-constraint-lambda'\n\n// you can merge default validators with your own validator\nconst myValidators = {\n  ...defaultValidators,\n\n  // your custom validator comes here\n  constraintName: constrintValue =\u003e dataToValidate =\u003e true/false\n\n  // Example: numerical pin codes of certain size `@constraint(pin:4)`\n  pin: size =\u003e code =\u003e length(code) === size \u0026\u0026 match(/[0-9]+/)(code)\n}\n\nconst myValidationCallback = createValidationCallback(myValidators)\n\n// now you can create the constraint class\nconst constraint = prepareConstraintDirective(\n  myValidationCallback, defaultErrorMessageCallback )\n```\n\nThere is a special `format` validator that supports the following:\n-  `@constraint(format: \"email\")`\n-  `@constraint(format: \"base64\")`\n-  `@constraint(format: \"date\")`\n-  `@constraint(format: \"ipv4\")`\n-  `@constraint(format: \"ipv6\")`\n-  `@constraint(format: \"url\")`\n-  `@constraint(format: \"uuid\")`\n-  `@constraint(format: \"futuredate\")`\n-  `@constraint(format: \"pastdate\")`\n-  `@constraint(format: \"creditcard\")`\n\nLet's say we want to extend it to support `format: \"uppercase\"` format that checks whether all characters are just uppercase letters:\n```js\nimport {\n  formatValidator,\n  numericValidators,\n  format2fun,\n  stringValidators } from 'node-graphql-constraint-lambda'\n\nconst customFormat2Fun = {\n  ...format2fun,\n\n  uppercase: x =\u003e match(/[A-Z]*/)(x)\n  // we could have omitted the `x` parameter due to currying in the\n  // `match` function from ramda\n}\n\nconst validators = {\n  ...formatValidator(customFormat2Fun),\n  ...numericValidators,\n  ...stringValidators\n}\n\n// now you can create the constraint class\nconst constraint = prepareConstraintDirective(\n  createValidationCallback(validators),\n  defaultErrorMessageCallback\n)\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvsimko%2Fnode-graphql-constraint-lambda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvsimko%2Fnode-graphql-constraint-lambda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvsimko%2Fnode-graphql-constraint-lambda/lists"}