Skip to content

Creates TypeBox code from JSON schemas

License

Notifications You must be signed in to change notification settings

shiftsmartinc/schema2typebox

 
 

Repository files navigation

Schema2TypeBox

Creating TypeBox code from JSON schemas.

ts2typebox is released under the MIT license. Current npm package version. State of Github Action

Installation

  • npm i -g schema2typebox

Use Case

  • You got JSON schemas that you want to validate your data against. But you also want automatic type inference after validating the data. You have chosen typebox for this, but figured that you would need to manually create the typebox code. To avoid this pain, you simply use schema2typebox to generate the required code for you🎉.

Usage

  • The cli can be used with schema2typebox --input <fileName> --output <fileName>, or by simply running schema2typebox. The input defaults to "schema.json" and the output to "generated-typebox.ts" relative to the current working directory. For more see cli usage.

Examples

//
// Let's start with our JSON schema
//

{
  "title": "Person",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 20
    },
    "age": {
      "type": "number",
      "minimum": 18,
      "maximum": 90
    },
    "hobbies": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string"
      }
    },
    "favoriteAnimal": {
      "enum": ["dog", "cat", "sloth"]
    }
  },
  "required": ["name", "age"]
}

//
// Which becomes..
//

export type Person = Static<typeof Person>;
export const Person = Type.Object({
  name: Type.String({ minLength: 20 }),
  age: Type.Number({ minimum: 18, maximum: 90 }),
  hobbies: Type.Optional(Type.Array(Type.String(), { minItems: 1 })),
  favoriteAnimal: Type.Optional(
    Type.Union([
      Type.Literal("dog"),
      Type.Literal("cat"),
      Type.Literal("sloth"),
    ])
  ),
});

//
// You can also split your JSON schema definitions into multiple files when
// using relative paths. Something like this:
//

// person.json
{
  "title": "Person",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "maxLength": 100
    },
    "age": {
      "type": "number",
      "minimum": 18
    }
  },
  "required": ["name", "age"]
}

// status.json
{
  "title": "Status",
  "enum": ["unknown", "accepted", "denied"]
}

// schema.json
{
  "title": "Contract",
  "type": "object",
  "properties": {
    "person": {
      "$ref": "./person.json"
    },
    "status": {
      "$ref": "./status.json"
    }
  },
  "required": ["person"]
}

//
// Will result in this:
//

export type Contract = Static<typeof Contract>;
export const Contract = Type.Object({
  person: Type.Object({
    name: Type.String({ maxLength: 100 }),
    age: Type.Number({ minimum: 18 }),
  }),
  status: Type.Optional(
      Type.Union([
        Type.Literal("unknown"),
        Type.Literal("accepted"),
        Type.Literal("denied"),
      ])
   ),
});

//
// For an example of programmatic usage check out the examples folder.
//

Please take a look at the feature list below to see the currently supported features. For examples, take a look into the examples folder. You can also check the test cases, every feature is tested.

Schema Support

The package is focused on supporting JSON schema draft-06 files, since this is the target TypeBox officially supports. These types are fully compatible with the JSON Schema Draft 6 specification. (from typebox repo).

However, since the amount of breaking changes is quite small between most JSON schema specs, support for other specs may "just work" or be implemented at a later stage. Feel free to open a discussion or issue when you find problems. Happy about contributions if you want to help out.

  • draft-04
  • draft-06 (main goal of this package, see Feature List for the state)
  • draft-07
  • draft-2019-09
  • should be working with the current feature set
  • draft-2020-12
    • Probably not working due to new keywords or semantic changes for previous keywords. Happy about issues with your JSON schema, expected TypeBox code and the currently generated TypeBox code.

Feature List

Tracking the progress/features of JSON schema -> TypeBox transformation to see whats already implemented and what is missing.

  • Type.String() via "string" instance type
  • Type.Boolean() via "boolean" instance type
  • Type.Number() via "number" instance type
  • Type.Null() via "null" instance type
  • Type.Array() via "array" instance type
  • Type.Object() via "object" instance type
  • Type.Literal() via "const" property
  • Type.Union() via "anyOf" or "enum" property
    • schema2typebox generates union types instead of enums. If you have a problem with this behaviour and valid arguments for using enums please create an issue and it may be considered again.
  • Type.Intersect() via "allOf" property
  • OneOf() via "oneOf" property
    • This adds oneOf to the typebox type registry as (Kind: 'ExtendedOneOf') in order to be able to align to oneOf json schema semantics and still be able to use the typebox compiler. More info.
  • Type.Not() via "not" property
  • schemaOptions
  • $refs anywhere using @apidevtools/json-schema-ref-parser
  • Name of generated value and type based on existing "title" attribute. Defaulting to "T" if title is not defined.
  • (low prio) Type.Tuple() via "array" instance type with minimalItems, maximalItems and additionalItems false

Open Tasks

See here and followup PRs.

  • Type.Array() with "array" instance type
  • Nullable Literal types, eg: type: ['string', 'null']
  • "Unknown" object types
  • Disambiguation of overlapping property names in nested schemas

DEV/CONTRIBUTOR NOTES

  • If you have an idea or want to help implement something, feel free to do so. Please always start by creating a discussion post to avoid any unnecessary work.
    • Please always create tests for new features that are implemented. This will decrease mental overhead for reviewing and developing in the long run.
  • See specification for JSON schema draft-06 here
  • Link to meta schema. Meta schema means that a JSON schema is created in order to validate that a given schema adheres to a given JSON schema draft.

cli usage

The following text is the output that will be displayed when you issue schema2typebox -h or schema2typebox --help.


    schema2typebox generates TypeBox code from JSON schemas. The generated
    output is formatted based on the prettier config inside your repo (or the
    default one, if you don't have one). Version: ${packageJson.version}

    Usage:

    schema2typebox [ARGUMENTS]

    Arguments:

    -h, --help
       Displays this menu.

    -i, --input
       Specifies the relative path to the file containing the JSON schema that
       will be used to generated typebox code. Defaults to "schema.json".

    -o, --output
       Specifies the relative path to generated file that will contain the
       typebox code. Defaults to "generated-typebox.ts".

    --output-stdout
       Does not generate an output file and prints the generated code to stdout
       instead. Has precedence over -o/--output.

Code coverage

This project aims for high code coverage. When you add new features or fix a bug, please add a test for it. This is what I could get out of the experimental code coverage from node test runner in v20.03.1. Was run with code from 28.06.2023.

File Line % Branch % Funcs %
dist/src/programmatic-usage.js 95.83 53.33 80.00
dist/src/schema-to-typebox.js 92.06 86.54 94.74

While I enjoy using the test runner from nodejs itself, this feature is still lacking.

Template Repo

Template for the repo setup was taken from here.

About

Creates TypeBox code from JSON schemas

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 81.3%
  • JavaScript 17.7%
  • Shell 1.0%