Skip to content

Support for glob input in CLI#55

Open
SebastienGllmt wants to merge 7 commits intoxddq:mainfrom
SebastienGllmt:blob-input
Open

Support for glob input in CLI#55
SebastienGllmt wants to merge 7 commits intoxddq:mainfrom
SebastienGllmt:blob-input

Conversation

@SebastienGllmt
Copy link
Copy Markdown
Contributor

@SebastienGllmt SebastienGllmt commented Apr 9, 2025

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 SebastienGllmt changed the title Blob input Support for glob input in CLI Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant