{"id":15821137,"url":"https://github.com/eomm/joi-compiler","last_synced_at":"2025-05-08T07:35:03.169Z","repository":{"id":63044486,"uuid":"564335859","full_name":"Eomm/joi-compiler","owner":"Eomm","description":"Build and manage the JOI instances for the fastify framework","archived":false,"fork":false,"pushed_at":"2025-05-05T23:29:34.000Z","size":22,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-06T00:32:07.845Z","etag":null,"topics":["fastify","fastify-plugin","joi","joi-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/Eomm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null},"funding":{"github":"Eomm"}},"created_at":"2022-11-10T13:55:00.000Z","updated_at":"2025-05-05T23:29:32.000Z","dependencies_parsed_at":"2024-10-26T12:46:54.743Z","dependency_job_id":"d60c0300-7962-4cca-882b-327d83b73776","html_url":"https://github.com/Eomm/joi-compiler","commit_stats":{"total_commits":6,"total_committers":2,"mean_commits":3.0,"dds":"0.16666666666666663","last_synced_commit":"35a5ff9c2a74d33d7cf0bd935ada0e26159be493"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fjoi-compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fjoi-compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fjoi-compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fjoi-compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Eomm","download_url":"https://codeload.github.com/Eomm/joi-compiler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253023224,"owners_count":21842189,"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":["fastify","fastify-plugin","joi","joi-validation"],"created_at":"2024-10-05T07:20:36.191Z","updated_at":"2025-05-08T07:35:03.146Z","avatar_url":"https://github.com/Eomm.png","language":"JavaScript","funding_links":["https://github.com/sponsors/Eomm"],"categories":[],"sub_categories":[],"readme":"# joi-compiler\n\n[![Continuous Integration](https://github.com/Eomm/joi-compiler/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/Eomm/joi-compiler/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/joi-compiler.svg?style=flat)](https://www.npmjs.com/package/joi-compiler)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)\n\nBuild and manage the [`joi`](https://joi.dev/) instances for the Fastify framework.\n\nIt isolates the `joi` dependency so its version is not tightly coupled to the Fastify version.\nThis allows the user to decide which version of `joi` to use in their Fastify based application.\n\n\n## Versions\n\n| `joi-compiler` | `joi` | `fastify` |\n|---------------:|------:|----------:|\n|           v2.x | v17.x |     ^5.0 |\n|           v1.x | v17.x |     ^4.18 |\n\n\n### JOI Configuration\n\nThe `joi`'s default configuration is:\n\n```js\n{\n  stripUnknown: true\n}\n```\n\nTo know all the possible options, please refer to the [`joi.validate()` documentation](https://joi.dev/api/?v=17.7.0#anyvalidatevalue-options).\n\n\n## Usage\n\nThis module is a Fastify schema compiler, it is used by Fastify to build the schema validator.  \nYou can configure it by setting the [Schema Controller](https://www.fastify.io/docs/latest/Reference/Server/#schemacontroller), but let's see how to use it directly:\n\n```js\nconst fastify = require('fastify')\nconst JoiCompiler = require('joi-compiler')\n\n// Instantiate the compiler\nconst factory = JoiCompiler()\n\n// Install it to Fastify\nconst app = fastify({\n  schemaController: {\n    compilersFactory: {\n      buildValidator: factory.buildValidator\n    }\n  }\n})\n\n// Use it!\napp.get('/', {\n  handler: echo,\n  schema: {\n    headers: factory.joi.object({\n      foo: factory.joi.string().required()\n    })\n  }\n})\n```\n\n### Options\n\nThe `JoiCompiler` accepts an optional configuration object:\n\n```js\nconst factory = JoiCompiler({\n  // optionally: provide all the JOI options you need\n  prefs: {\n    allowUnknown: true\n  },\n\n// optionally: an array with custom JOI extensions\n  extensions: [\n    require('@joi/date')\n  ],\n\n  // optionally: if you want to use the async validation. Default: false\n  // Note that this option is supported ONLY by Fastify since v4.18.0\n  asyncValidation: true\n})\n```\n\nWhen you instantiate the compiler, the returned object has the following properties:\n\n- `buildValidator`: the function to pass to the `schemaController` option of Fastify\n- `bucket`: the object to support `fastify.addSchema()`\n- `joi`: a customized `joi` instance that contains the installed `extensions`.\n\n\u003e **Warning**\n\u003e The async `joi` extensions are supported by Fastify since [`v4.18.0`](https://github.com/fastify/fastify/pull/4752).\n\n\n### How to use `ref`\n\nThe `joi` module supports the [`ref`](https://joi.dev/api/?v=17.7.0#refkey-options) feature to link the `context` configuration.\n\nYou can configure the `context` option by using the [`fastify.addSchema()`](https://www.fastify.io/docs/latest/Reference/Server/#addschema) method.  \nNote that, as the Fastify default compiler, this feature is fully encapsulated!\n\nIn this case you will need to use a special schema object:\n\n```js\napp.addSchema({\n  $id: 'x', // the ID you want to use\n  $value: 42 // the value you want to store in the context\n})\n```\n\nHere is an example:\n\n```js\nconst factory = JoiCompiler()\n\nconst app = fastify({\n  schemaController: {\n    bucket: factory.bucket,\n    compilersFactory: {\n      buildValidator: factory.buildValidator\n    }\n  }\n})\n\napp.addSchema({ $id: 'x', $value: 42 })\n\napp.post('/', {\n  handler: echo,\n  schema: {\n    body: Joi.object({\n      a: Joi.ref('b.c'),\n      b: { c: Joi.any() },\n      c: Joi.ref('$x')\n    })\n  }\n})\n```\n\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feomm%2Fjoi-compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feomm%2Fjoi-compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feomm%2Fjoi-compiler/lists"}