{"name":"@valibot/to-json-schema","version":"1.6.0","keywords":["valibot","schema","converter","json-schema"],"author":{"name":"Fabian Hiller"},"license":"MIT","_id":"@valibot/to-json-schema@1.6.0","maintainers":[{"name":"fabian-hiller","email":"hillerfabian11@gmail.com"}],"contributors":[{"name":"Guillaume Cornut"}],"homepage":"https://valibot.dev","bugs":{"url":"https://github.com/open-circle/valibot/issues"},"dist":{"shasum":"d61354fb50629868a2de7169c3093899abad923d","tarball":"http://registry-npm.ywwl.com/@valibot/to-json-schema/download/@valibot/to-json-schema-1.6.0.tgz","fileCount":9,"integrity":"sha512-d6rYyK5KVa2XdqamWgZ4/Nr+cXhxjy7lmpe6Iajw15J/jmU+gyxl2IEd1Otg1d7Rl3gOQL5reulnSypzBtYy1A==","signatures":[{"sig":"MEUCID1Wt1MvcMhJYSi7cInlsaCoi2ZkT0sWw8rnggu8lleSAiEAwafiN+fCI1mzzB2rYNtY8vb8uoWz6fZXOG1hiuS06mM=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@valibot%2fto-json-schema@1.6.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":149941,"size":26925,"noattachment":false,"key":"/@valibot/to-json-schema/-/@valibot/to-json-schema-1.6.0.tgz"},"main":"./dist/index.mjs","type":"module","types":"./dist/index.d.mts","exports":{".":{"import":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"require":{"types":"./dist/index.d.cts","default":"./dist/index.cjs"}}},"gitHead":"c5fa221d34657b81c9a6ba3d3ed5bbcd9935a9aa","scripts":{"lint":"eslint \"src/**/*.ts*\" && tsc --noEmit && deno check ./src/index.ts","test":"vitest --typecheck","build":"tsdown","format":"prettier --write ./src","coverage":"vitest run --coverage --isolate","lint.fix":"eslint \"src/**/*.ts*\" --fix","format.check":"prettier --check ./src"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:44b4f58c-9e08-4868-a6ab-35c29b1c96ac"}},"repository":{"url":"git+https://github.com/open-circle/valibot.git","type":"git"},"_npmVersion":"11.11.1","description":"The official JSON schema converter for Valibot","directories":{},"sideEffects":false,"_nodeVersion":"24.14.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"vite":"^7.2.4","eslint":"^9.39.1","tsdown":"^0.16.6","vitest":"4.0.13","globals":"^16.5.0","valibot":"^1.3.0","@eslint/js":"^9.39.1","typescript":"^5.9.3","@types/node":"^24.10.1","typescript-eslint":"^8.47.0","@vitest/coverage-v8":"^4.0.13","eslint-plugin-jsdoc":"^61.4.0","vite-tsconfig-paths":"^5.1.4","eslint-plugin-import":"^2.32.0","eslint-plugin-security":"^3.0.1","eslint-import-resolver-typescript":"^4.4.4"},"peerDependencies":{"valibot":"^1.3.0"},"_npmOperationalInternal":{"tmp":"tmp/to-json-schema_1.6.0_1773786838209_0.7622549927447608","host":"s3://npm-registry-packages-npm-production"},"readme":"# Valibot to JSON Schema\n\nUtility to convert [Valibot](https://valibot.dev) schemas to JSON Schema. Supports JSON Schema draft-07, draft-2020-12, and OpenAPI 3.0 Schema Object formats.\n\n```js\nimport { toJsonSchema } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\ntoJsonSchema(v.string()); // { $schema: \"http://json-schema.org/draft-07/schema#\", type: \"string\" }\n```\n\nThis package is particularly popular for:\n\n- **API Documentation**: Generate OpenAPI specifications from your Valibot schemas\n- **Code Generation**: Create client SDKs and types from your validation schemas\n- **LLM Integration**: Generate structured outputs for Large Language Models\n- **Schema Sharing**: Share validation logic between different programming languages\n\n> Some Valibot features can't be mapped to JSON schema. For example, transformation actions have no equivalent in JSON schema. Also, some Valibot schemas or validations are too JS-specific and do not have an equivalent JSON schema attribute.\n\n## Supported features\n\n**Note**: Converted schemas may behave slightly differently in JSON schema validators (especially for string format) because their implementation is different from Valibot's.\n\n| Schema           | Status | Note                                                                                                                                  |\n| ---------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------- |\n| `any`            | ✅     |                                                                                                                                       |\n| `array`          | ✅     |                                                                                                                                       |\n| `boolean`        | ✅     |                                                                                                                                       |\n| `enum`           | ✅     |                                                                                                                                       |\n| `exactOptional`  | ✅     |                                                                                                                                       |\n| `intersect`      | ✅     |                                                                                                                                       |\n| `lazy`           | ⚠️     | The `.getter` function is always executed with `undefined` as input                                                                   |\n| `literal`        | ⚠️     | Only JSON compatible values are supported                                                                                             |\n| `looseObject`    | ✅     |                                                                                                                                       |\n| `looseTuple`     | ✅     |                                                                                                                                       |\n| `never`          | ✅     |                                                                                                                                       |\n| `null`           | ✅     |                                                                                                                                       |\n| `nullable`       | ✅     |                                                                                                                                       |\n| `nullish`        | ✅     |                                                                                                                                       |\n| `number`         | ✅     |                                                                                                                                       |\n| `objectWithRest` | ✅     |                                                                                                                                       |\n| `object`         | ✅     |                                                                                                                                       |\n| `optional`       | ✅     |                                                                                                                                       |\n| `picklist`       | ⚠️     | Only JSON compatible values are supported                                                                                             |\n| `record`         | ⚠️     | Only `string` schemas for the key of the record are supported. Adds `propertyNames` for key validation (not available in OpenAPI 3.0) |\n| `strictObject`   | ✅     |                                                                                                                                       |\n| `strictTuple`    | ✅     |                                                                                                                                       |\n| `string`         | ✅     |                                                                                                                                       |\n| `tupleWithRest`  | ✅     |                                                                                                                                       |\n| `tuple`          | ✅     |                                                                                                                                       |\n| `union`          | ✅     |                                                                                                                                       |\n| `undefinedable`  | ✅     |                                                                                                                                       |\n| `unknown`        | ✅     |                                                                                                                                       |\n| `variant`        | ⚠️     | The discriminator key will be ignored                                                                                                 |\n\n| Actions         | Status | Note                                                        |\n| --------------- | ------ | ----------------------------------------------------------- |\n| `base64`        | ✅     |                                                             |\n| `bic`           | ✅     |                                                             |\n| `cuid2`         | ✅     |                                                             |\n| `decimal`       | ✅     |                                                             |\n| `description`   | ✅     |                                                             |\n| `digits`        | ✅     |                                                             |\n| `domain`        | ✅     |                                                             |\n| `email`         | ✅     |                                                             |\n| `emoji`         | ✅     |                                                             |\n| `empty`         | ✅     |                                                             |\n| `endsWith`      | ✅     |                                                             |\n| `entries`       | ✅     |                                                             |\n| `examples`      | ✅     |                                                             |\n| `gtValue`       | ⚠️     | Only in combination with `number` and `integer` schema      |\n| `hash`          | ✅     |                                                             |\n| `hexadecimal`   | ✅     |                                                             |\n| `hexColor`      | ✅     |                                                             |\n| `includes`      | ✅     |                                                             |\n| `integer`       | ✅     |                                                             |\n| `ipv4`          | ✅     |                                                             |\n| `ipv6`          | ✅     |                                                             |\n| `isoDate`       | ✅     |                                                             |\n| `isoDateTime`   | ✅     |                                                             |\n| `isoTime`       | ✅     |                                                             |\n| `isoTimeSecond` | ✅     |                                                             |\n| `isoTimestamp`  | ✅     |                                                             |\n| `isoWeek`       | ✅     |                                                             |\n| `isrc`          | ✅     |                                                             |\n| `jwsCompact`    | ✅     |                                                             |\n| `length`        | ⚠️     | Only in combination with `string` and `array` schema        |\n| `ltValue`       | ⚠️     | Only in combination with `number` and `integer` schema      |\n| `mac`           | ✅     |                                                             |\n| `mac48`         | ✅     |                                                             |\n| `mac64`         | ✅     |                                                             |\n| `maxEntries`    | ✅     |                                                             |\n| `maxLength`     | ⚠️     | Only in combination with `string` and `array` schema        |\n| `maxValue`      | ⚠️     | Only in combination with `number` schema                    |\n| `metadata`      | ⚠️     | Only for valid `title`, `description` and `examples` values |\n| `minEntries`    | ✅     |                                                             |\n| `minLength`     | ⚠️     | Only in combination with `string` and `array` schema        |\n| `minValue`      | ⚠️     | Only in combination with `number` schema                    |\n| `multipleOf`    | ✅     |                                                             |\n| `nanoid`        | ✅     |                                                             |\n| `nonEmpty`      | ✅     |                                                             |\n| `notValue`      | ⚠️     | Only JSON compatible values are supported                   |\n| `notValues`     | ⚠️     | Only JSON compatible values are supported                   |\n| `octal`         | ✅     |                                                             |\n| `regex`         | ⚠️     | RegExp flags are not supported in JSON Schema               |\n| `rfcEmail`      | ✅     |                                                             |\n| `safeInteger`   | ✅     |                                                             |\n| `slug`          | ✅     |                                                             |\n| `startsWith`    | ✅     |                                                             |\n| `title`         | ✅     |                                                             |\n| `ulid`          | ✅     |                                                             |\n| `url`           | ✅     |                                                             |\n| `uuid`          | ✅     |                                                             |\n| `value`         | ⚠️     | Only JSON compatible values are supported                   |\n| `values`        | ⚠️     | Only JSON compatible values are supported                   |\n\n## Configurations\n\n| Option         | Type                                                                  | Note                                                                                                                      |\n| -------------- | --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |\n| target         | `'draft-07' \\| 'draft-2020-12' \\| 'openapi-3.0'`                      | The target JSON Schema format. Defaults to `'draft-07'`.                                                                  |\n| typeMode       | `'ignore' \\| 'input' \\| 'output'`                                     | Whether to convert the input or output type of the Valibot schema to JSON Schema.                                         |\n| errorMode      | `'throw' \\| 'warn' \\| 'ignore'`                                       | The policy for handling incompatible schemas and actions.                                                                 |\n| definitions    | `Record<string, GenericSchema>`                                       | The schema definitions for constructing recursive schemas. If not specified, the definitions are generated automatically. |\n| overrideSchema | `(context: OverrideSchemaContext) => JsonSchema \\| null \\| undefined` | Overrides the JSON Schema conversion for a specific Valibot schema.                                                       |\n| ignoreActions  | `string[]`                                                            | The actions that should be ignored during the conversion.                                                                 |\n| overrideAction | `(context: OverrideActionContext) => JsonSchema \\| null \\| undefined` | Overrides the JSON Schema reference for a specific Valibot action.                                                        |\n| overrideRef    | `(context: OverrideRefContext) => string \\| null \\| undefined`        | Overrides the JSON Schema reference for a specific reference ID.                                                          |\n\n### Target format\n\nThe `target` configuration allows you to specify which JSON Schema format to generate. Different targets have different capabilities and syntax:\n\n```js\nimport { toJsonSchema } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst schema = v.nullable(v.string());\n\n// JSON Schema draft-07 (default)\ntoJsonSchema(schema);\n// { $schema: \"http://json-schema.org/draft-07/schema#\", anyOf: [{ type: \"string\" }, { type: \"null\" }] }\n\n// JSON Schema draft-2020-12\ntoJsonSchema(schema, { target: 'draft-2020-12' });\n// { $schema: \"https://json-schema.org/draft/2020-12/schema\", anyOf: [{ type: \"string\" }, { type: \"null\" }] }\n\n// OpenAPI 3.0 Schema Object\ntoJsonSchema(schema, { target: 'openapi-3.0' });\n// { type: \"string\", nullable: true }\n```\n\n**Note**: Some features like `propertyNames` for record schemas are not available in OpenAPI 3.0.\n\n### Type mode\n\nThe `typeMode` configuration controls whether to convert the input or output type of the Valibot schema to JSON Schema.\n\n- When set to `'input'`, conversion stops before the first potential type transformation action or second schema in any pipeline.\n- When set to `'output'`, conversion of any pipelines starts from the last schema in the pipeline. Therefore, the output type must be specified explicitly with a schema after the last type transformation action.\n- When set to `'ignore'` (default), the entire pipeline is converted.\n\nThis is particularly useful when defining API endpoints where external developers need different schema information for requests vs responses:\n\n```js\nimport { toJsonSchema } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst ValibotSchema = v.pipe(\n  v.string(),\n  v.decimal(),\n  v.transform(Number),\n  v.number(),\n  v.maxValue(100)\n);\n\ntoJsonSchema(ValibotSchema, { typeMode: 'input' });\n\n// {\n//   $schema: \"http://json-schema.org/draft-07/schema#\",\n//   type: \"string\",\n//   pattern: \"^[+-]?(?:\\\\d*\\\\.)?\\\\d+$\"\n// }\n\ntoJsonSchema(ValibotSchema, { typeMode: 'output' });\n\n// {\n//   $schema: \"http://json-schema.org/draft-07/schema#\",\n//   type: \"number\",\n//   maximum: 100\n// }\n```\n\n### Error mode\n\nThe `errorMode` configuration controls how the converter handles unsupported schemas and actions. By default, the error mode is set to `'throw'`. To force the conversion of unsupported Valibot features, you can set it to `'ignore'`.\n\n> Unsupported schemas usually return an empty JSON schema (`{}`) and unsupported actions are usually ignored.\n\n```js\nimport { toJsonSchema } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\ntoJsonSchema(v.file(), { errorMode: 'ignore' }); // {}\n\ntoJsonSchema(v.pipe(v.string(), v.creditCard()), { errorMode: 'ignore' }); // { type: \"string\" }\n```\n\n### Override functions\n\nThe package provides powerful override capabilities to customize the JSON Schema conversion process. You can override the conversion of specific schemas, actions, or references.\n\n#### Override schema conversion\n\nHandle unsupported schemas or customize conversion behavior:\n\n```js\nimport { toJsonSchema } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst ValibotSchema = v.object({ createdAt: v.date() });\n\ntoJsonSchema(ValibotSchema, {\n  overrideSchema(context) {\n    if (context.valibotSchema.type === 'date') {\n      return { type: 'string', format: 'date-time' };\n    }\n  },\n});\n\n// {\n//   $schema: \"http://json-schema.org/draft-07/schema#\",\n//   type: \"object\",\n//   properties: {\n//     createdAt: { type: \"string\" format: \"date-time\" }\n//   },\n//   required: [\"createdAt\"]\n// }\n```\n\n#### Override reference IDs\n\nCustomize reference IDs for OpenAPI or other specifications:\n\n```js\nimport { toJsonSchemaDefs } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst UserSchema = v.object({ name: v.string() });\n\ntoJsonSchemaDefs(\n  { UserSchema },\n  { overrideRef: (context) => `#/schemas/${context.referenceId}` }\n);\n```\n\n### Enhanced metadata support\n\nUse the generic `metadata` action to add title, description, and examples to your schemas or the individual `title`and `description` actions:\n\n```js\nimport { toJsonSchema } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst ValibotSchema = v.pipe(\n  v.string(),\n  v.email(),\n  v.metadata({\n    title: 'Email Schema',\n    description: 'A schema that validates email addresses.',\n    examples: ['jane@example.com'],\n  })\n);\n\ntoJsonSchema(ValibotSchema);\n\n// {\n//   $schema: \"http://json-schema.org/draft-07/schema#\",\n//   type: \"string\",\n//   format: \"email\",\n//   title: \"Email Schema\",\n//   description: \"A schema that validates email addresses.\",\n//   examples: [\"jane@example.com\"]\n// }\n```\n\n### Definitions\n\nNested and recursive schemas can be broken in multiple reusable definitions.\n\n```js\nimport { toJsonSchema } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst EmailSchema = v.pipe(v.string(), v.email());\ntoJsonSchema(v.object({ email: EmailSchema }), {\n  definitions: { EmailSchema },\n});\n\n// {\n//   $schema: \"http://json-schema.org/draft-07/schema#\",\n//   type: \"object\",\n//   properties: {\n//     email: {\n//       $ref: \"#/$defs/EmailSchema\"\n//     }\n//   },\n//   required: [\"email\"],\n//   $defs: {\n//     EmailSchema: {\n//       type: \"string\",\n//       format: \"email\"\n//     }\n//   }\n// }\n```\n\nDefinitions are not required for converting `lazy` schemas. Missing definitions will be generated automatically.\n\n```js\nimport { toJsonSchema } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst StringSchema = v.string();\ntoJsonSchema(v.object({ key: v.lazy(() => StringSchema) }));\n\n// {\n//   $schema: \"http://json-schema.org/draft-07/schema#\",\n//   type: \"object\",\n//   properties: {\n//     key: {\n//       $ref: \"#/$defs/0\"\n//     }\n//   },\n//   required: [\"key\"],\n//   $defs: {\n//     0: {\n//       type: \"string\"\n//     }\n//   }\n// }\n```\n\n## Additional functions\n\n### `toStandardJsonSchema`\n\nConverts a Valibot schema to the [Standard JSON Schema](https://standardschema.dev/) format. This format is useful when working with tools and libraries that support the Standard JSON Schema specification.\n\n```js\nimport { toStandardJsonSchema } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst schema = toStandardJsonSchema(\n  v.object({\n    id: v.pipe(v.string(), v.uuid()),\n    name: v.pipe(v.string(), v.nonEmpty()),\n    age: v.optional(v.number()),\n  })\n);\n```\n\n### `toJsonSchemaDefs`\n\nConverts only the provided Valibot schema definitions to JSON Schema definitions, without wrapping them in a root schema. This is particularly useful for OpenAPI specifications where you need only the schema definitions.\n\n```js\nimport { toJsonSchemaDefs } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst EmailSchema = v.pipe(v.string(), v.email());\nconst UserSchema = v.object({\n  name: v.string(),\n  email: EmailSchema,\n});\n\ntoJsonSchemaDefs({ EmailSchema, UserSchema });\n\n// {\n//   EmailSchema: {\n//     type: \"string\",\n//     format: \"email\"\n//   },\n//   UserSchema: {\n//     type: \"object\",\n//     properties: {\n//       name: {\n//         type: \"string\"\n//       },\n//       email: {\n//         $ref: \"#/$defs/EmailSchema\"\n//       }\n//     },\n//     required: [\"name\", \"email\"]\n//   }\n// }\n```\n\n#### OpenAPI integration\n\nFor OpenAPI specifications, you can customize reference IDs:\n\n```js\nimport { toJsonSchemaDefs } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst ValibotSchema1 = v.string();\nconst ValibotSchema2 = v.number();\nconst ValibotSchema3 = v.tuple([ValibotSchema1, ValibotSchema2]);\n\ntoJsonSchemaDefs(\n  { ValibotSchema1, ValibotSchema2, ValibotSchema3 },\n  { overrideRef: (context) => `#/schemas/${context.referenceId}` }\n);\n\n// {\n//   ValibotSchema1: { type: \"string\" },\n//   ValibotSchema2: { type: \"number\" },\n//   ValibotSchema3: {\n//     type: \"array\",\n//     items: [\n//       { $ref: \"#/schemas/ValibotSchema1\" },\n//       { $ref: \"#/schemas/ValibotSchema2\" }\n//     ],\n//     minItems: 2\n//   }\n// }\n```\n\n### Global definitions\n\nFor advanced use cases, you can manage global schema definitions that will be automatically used when converting schemas. This is particularly useful for larger projects with many reusable schemas.\n\n```js\nimport { addGlobalDefs, toJsonSchema } from '@valibot/to-json-schema';\nimport * as v from 'valibot';\n\nconst ValibotSchema1 = v.string();\nconst ValibotSchema2 = v.number();\n\naddGlobalDefs({ ValibotSchema1, ValibotSchema2 });\n\nconst ValibotSchema3 = v.tuple([ValibotSchema1, ValibotSchema2]);\n\ntoJsonSchema(ValibotSchema3);\n\n// {\n//   $schema: \"http://json-schema.org/draft-07/schema#\",\n//   type: \"array\",\n//   items: [\n//     { $ref: \"#/$defs/ValibotSchema1\" },\n//     { $ref: \"#/$defs/ValibotSchema2\" }\n//   ],\n//   minItems: 2,\n//   $defs: {\n//     ValibotSchema1: { type: \"string\" },\n//     ValibotSchema2: { type: \"number\" }\n//   }\n// }\n```\n\nYou can also convert global definitions directly using `toJsonSchemaDefs`:\n\n```js\nconst globalDefs = getGlobalDefs();\nif (globalDefs) {\n  const schemaDefs = toJsonSchemaDefs(globalDefs);\n}\n```\n","publish_time":1773786838343,"_cnpm_publish_time":1773786838343,"dist-tags":{"beta":"1.0.0-beta.3","latest":"1.8.0"}}