schema2typebox icon indicating copy to clipboard operation
schema2typebox copied to clipboard

Support for glob input in CLI

Open SebastienGllmt opened this issue 10 months ago • 0 comments

Note

This PR is based on #54. It will have to be rebased once #54 is merged

Summary

This PR allows glob inputs so that you can combine multiple json schema files into a single output

This is done by allowing glob for the input parameter (ex: npx schema2typebox -i **/*.json)

Why is this better than generating one output per file?

Imagine you have the following schemas:

  1. file A contains schema A
  2. file B contains schema B, which as a ref to A
  3. file C contains schema C, which as a ref to B

If you codegen this, you end up with three separate files, containing:

  1. Three copies of A (one for each file)
  2. Two copies of B (one for file B and file C)
  3. One copy of C

With the work done in #54, we can now easily solve this by just representing the types of all files as part of a single module

Is this really the best way to do this?

Using globs is very flexible, but another option would be to encourage people who want to aggregate files into a single output to instead use a file that only contains a single $def:

{
  "$defs": {
    "Comment": {
      "$ref": "common.json#/$defs/comment"
    },
    "Post": {
      "$ref": "common.json#/$defs/post"
    },
  }
}

And you can see examples this single $def pattern here, but I wouldn't recommend this approach for a few reasons:

  1. It's awkward to have to ask people to create a new file with just a bunch of refs (asking them to setup a glob is much easier)
  2. $defs are typically only meant to be used within the same file in which they're defined (if they're meant to be used by external files often, it's encouraged to split them into separate schema files). That means this pattern of files with only $defs in it is not really an encouraged pattern
  3. There are standards like Compound Schema Document with tools to bundle all your schemas files into a single schema. However, even for tools like this, they're designed to have a top-level type that actually references these $defs (instead of having only top-level $defs)

Given this, I believe the glob solution to be the best

SebastienGllmt avatar Apr 09 '25 09:04 SebastienGllmt