{"id":19989591,"url":"https://github.com/wavesoft/json-schema-to-decoders","last_synced_at":"2025-05-04T09:33:14.393Z","repository":{"id":47367921,"uuid":"516048111","full_name":"wavesoft/json-schema-to-decoders","owner":"wavesoft","description":"A handy utility for converting JSON schema definition to decoders code","archived":false,"fork":false,"pushed_at":"2022-12-09T10:52:54.000Z","size":5533,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-12T01:40:52.020Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/wavesoft.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}},"created_at":"2022-07-20T16:02:50.000Z","updated_at":"2024-01-12T18:33:36.000Z","dependencies_parsed_at":"2023-01-25T13:31:45.779Z","dependency_job_id":null,"html_url":"https://github.com/wavesoft/json-schema-to-decoders","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wavesoft%2Fjson-schema-to-decoders","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wavesoft%2Fjson-schema-to-decoders/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wavesoft%2Fjson-schema-to-decoders/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wavesoft%2Fjson-schema-to-decoders/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wavesoft","download_url":"https://codeload.github.com/wavesoft/json-schema-to-decoders/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224391393,"owners_count":17303609,"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":[],"created_at":"2024-11-13T04:48:34.384Z","updated_at":"2024-11-13T04:48:34.938Z","avatar_url":"https://github.com/wavesoft.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-schema-to-decoders ![ci](https://github.com/wavesoft/json-schema-to-decoders/actions/workflows/ci-testing.yml/badge.svg) [![try-it](https://img.shields.io/badge/Try%20it-Live-blue)](https://wavesoft.github.io/json-schema-to-decoders/) [![npm](https://img.shields.io/npm/v/json-schema-to-decoders)](https://www.npmjs.com/package/json-schema-to-decoders)\n\n\u003e Generates Typescript/Javascript [decoders](https://decoders.cc) source code expressions from JSON schema specifications.\n\nYou can also [try it in your browser](https://wavesoft.github.io/json-schema-to-decoders/) with any JSON schema.\n\n## Description\n\nThis package aims to provide the best possible conversion of a JSON Schema type into it's equivalent decoder function.\n\nIt has a wide support of different features from different JSON Schema drafts, check the [Support Matrix](#support-matrix) for more details.\n\n## Installation\n\nTo use the CLI you can install the package globally:\n\n```sh\nnpm install -g json-schema-to-decoders\n```\n\nTo use it as a library in your package, install it locally:\n\n```sh\nnpm install --dev json-schema-to-decoders\n# or\nyarn add -D json-schema-to-decoders\n```\n\n## Usage\n\nTo convert a JSON definition file to decoders, use the following command CLI:\n\n```\njson-schema-to-decoders \u003cschema.json\u003e\n```\n\nThe package also exports an API that be used for more elaborate integrations:\n\n```ts\nimport Converter from \"json-schema-to-decoders\";\n\nconsole.log(await Converter.convertFile(\"path/to/schema.json\"));\n```\n\n## API Reference\n\nThere are a few functios you can use for invoking the converter:\n\n```ts\n// Synchronous variations\nconvertSchema(schema: Schema, options?: ConverterOptions): string;\nconvertContents(buffer: string, options?: ConverterOptions): string;\n\n// Asynchronous variations\nconvertFile(file: string, options?: ConverterOptions): Promise\u003cstring\u003e;\nconvert(url: string | Schema, options?: ConverterOptions): Promise\u003cstring\u003e;\n```\n\nThe `ConverterOptions` have the following properties:\n\n- `nsPrefix`: An optional namespace where to look for decoder functions into.\n\n  For example, if you are importing the decoders like so:\n\n  ```ts\n  import * as D from \"decoders\";\n  ```\n\n  Then you can use:\n\n  ```ts\n  const code = convertSchema(schema, {\n    nsPrefix: \"D.\",\n  });\n  ```\n\n- `nsLib`: An optional namespace where to look for extra decoder library functions exposed by the `json-schema-to-decoders` package.\n\n  If not specified, all of the avanced decoders would be disabled.\n\n  For example, if you can import the utility library like so:\n\n  ```ts\n  import * as L from \"json-schema-to-decoders/decoders\";\n  ```\n\n  Then you can use:\n\n  ```ts\n  const code = convertSchema(schema, {\n    nsLib: \"L.\",\n  });\n  ```\n\n- `resolveRefPointer` : An optional function to call when a $ref is encountered. The returned value will replace the contents of that ref.\n\n  For example, given the following logic:\n\n  ```ts\n  const schema = {\n    type: \"array\",\n    items: {\n      $ref: \"#/components/schema/Item\",\n    },\n  };\n  const code = convertSchema(schema, {\n    resolveRefPointer: (expr) =\u003e {\n      return expr.split(\"/\").pop();\n    },\n  });\n  ```\n\n  Will produce the following code:\n\n  ```ts\n  array(Item);\n  ```\n\n- `resolveRefSchema` : An optional function to call when a $ref is encountered and the schema of it is required.\n\n  In contrast to `resolveRefPointer`, where a variable reference is emitted, this function expects the value of the referred schema to be returned.\n\n  If missing, `resolveRefPointer` would be used when possible, and if not, an exception would be thrown.\n\n## Support Matrix\n\nThe following table summarizes the supported conversion between JSON Schema types and decoders.\n\n| Type                | Validation / Keyword     | Status |\n| :------------------ | :----------------------- | :----- |\n| _All Types_         | `enum`                   | ✅     |\n|                     | `const`                  | ✅     |\n| References          | _Basic Support_          | ✅ [1] |\n| `any`               | _Basic Support_          | ✅     |\n| `string`            | _Basic Support_          | ✅     |\n|                     | `minLength`              | ✅     |\n|                     | `maxLength`              | ✅     |\n|                     | `pattern`                | ✅     |\n|                     | `format: \"date-time`     | ✅     |\n|                     | `format: \"time`          | -      |\n|                     | `format: \"date`          | -      |\n|                     | `format: \"duration`      | -      |\n|                     | `format: \"email`         | ✅     |\n|                     | `format: \"idn-email`     | -      |\n|                     | `format: \"hostname`      | ✅     |\n|                     | `format: \"idn-hostname`  | -      |\n|                     | `format: \"ipv4`          | -      |\n|                     | `format: \"ipv6`          | -      |\n|                     | `format: \"uuid`          | ✅     |\n|                     | `format: \"uri`           | ✅     |\n|                     | `format: \"uri-reference` | -      |\n|                     | `format: \"iri`           | -      |\n|                     | `format: \"iri-reference` | -      |\n| `integer`           | _Basic Support_          | ✅     |\n| `number`            | _Basic Support_          | ✅     |\n|                     | `multipleOf`             | ✅     |\n|                     | `minimum`                | ✅     |\n|                     | `maximum`                | ✅     |\n|                     | `exclusiveMinimum`       | ✅     |\n|                     | `exclusiveMaximum`       | ✅     |\n| `boolean`           | _Basic Support_          | ✅     |\n| `null`              | _Basic Support_          | ✅     |\n| `array`             | _Basic Support_          | ✅     |\n|                     | Unevaluated Items        | -      |\n|                     | `items`                  | ✅     |\n|                     | `prefixItems`            | 🟨 [2] |\n|                     | `contains`               | -      |\n|                     | `minContains`            | -      |\n|                     | `maxContains`            | -      |\n|                     | `minItems`               | ✅     |\n|                     | `maxItems`               | ✅     |\n|                     | `uniqueItems`            | ✅     |\n| `object`            | _Basic Support_          | ✅ [3] |\n|                     | Unevaluated Properties   | -      |\n|                     | Extending Closed Schemas | -      |\n|                     | `properties`             | ✅     |\n|                     | `additionalProperties`   | ✅     |\n|                     | `required`               | ✅     |\n|                     | `patternProperties`      | ✅     |\n|                     | `propertyNames`          | ✅     |\n|                     | `minProperties`          | ✅     |\n|                     | `maxProperties`          | ✅     |\n| Schema Composition  | `allOf`                  | ✅     |\n|                     | `oneOf`                  | 🟨 [4] |\n|                     | `anyOf`                  | ✅     |\n|                     | `discriminator`          | ✅     |\n| Conditional Schemas | `dependentRequired`      | -      |\n|                     | `dependentSchemas`       | -      |\n|                     | `if`                     | -      |\n|                     | `then`                   | -      |\n|                     | `else`                   | -      |\n\nRemarks:\n\n\u003e [1] Implemented through a user-provided reference resolution function that returns the variable name of a previously defined decoder.\n\n\u003e [2] Currently `prefixItems` cannot be used together with `items`. This means that declaring additional items for arrays is not supported.\n\n\u003e [3] Note that while for `type: \"object\"` the JSON Schema spec indicates that \"Using non-strings as keys is invalid JSON\", the javascript implementation implicitly converts all properties to strings, so the decoders will always validate even numbers in the object keys.\n\n\u003e [4] The `oneOf` is currently polyfilled with the `anyOf` behaviour. This means that the \"exactly one\" validation is not respected.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwavesoft%2Fjson-schema-to-decoders","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwavesoft%2Fjson-schema-to-decoders","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwavesoft%2Fjson-schema-to-decoders/lists"}