Skip to content

support arktype (generalise json-schemable validation libraries) - #59

Merged
mmkal merged 26 commits into
mainfrom
general-json-schema
Mar 19, 2025
Merged

mmkal merged 26 commits into
mainfrom
general-json-schema

Conversation

@mmkal

@mmkal mmkal commented Mar 18, 2025 •

Copy link
Copy Markdown
Owner

Be less zod-biased, so that arktype (or in theory another validation library implementing .toJsonSchema()) can be used instead of zod.

Mostly out of my own curiosity, and zod will remain the recommended way to use this, but feels sensible to not be too coupled to zod.

Notes:

  • arktype's .toJsonSchema() doesn't always produce a usable json schema, see A few toJsonSchema requests arktypeio/arktype#1379 and arktype.test.ts
  • removes some overly-clever logic that attempts to smart-parse positional arguments based on zod tuple elements etc. Still support distinction between number and integer and string (so that mycli foo 2.2 will parse as "2.2" for an input of type z.union([z.string(), z.number().int()]). But even this is arguably too smart. Maybe an argument of type z.union([z.string(), z.number()]) should just always parse as a number

Future:

  • valibot
  • typebox
  • effect
  • more??
  • maybe a proposal to add .toJsonSchema() to the standard-schema spec? I doubt it would be accpeted, but maybe, if there were some official caveats like it being optional, and it being able to rely on external libs or something
  • maybe even more logic around how to pick a winner in a union
  • probably an env var to allow doing something like TRPC_CLI_JSON_INPUTS=true mycli foo --input '{"xx": 1, "yy" 2}' - idea being that if you don't like the way this library has transformed the input type into CLI arguments, you can opt out as an escape hatch

@mmkal
mmkal force-pushed the general-json-schema branch from e49503b to 7c888c8 Compare March 18, 2025 15:34
@pkg-pr-new

pkg-pr-new Bot commented Mar 18, 2025 •

Copy link
Copy Markdown

Open in Stackblitz

pnpm add https://pkg.pr.new/mmkal/trpc-cli@59

commit: 200b62c

@mmkal
mmkal force-pushed the general-json-schema branch 2 times, most recently from 882f90b to 49aafe7 Compare March 19, 2025 14:32
@mmkal
mmkal force-pushed the general-json-schema branch from 49aafe7 to 46a2e18 Compare March 19, 2025 14:40
@mmkal mmkal changed the title support arktype support arktype (generalise json-schemable validation libraries) Mar 19, 2025
@mmkal
mmkal force-pushed the general-json-schema branch from 4cb4964 to 200b62c Compare March 19, 2025 16:09
@mmkal
mmkal marked this pull request as ready for review March 19, 2025 17:45
@mmkal
mmkal merged commit a9d274f into main Mar 19, 2025
@mmkal mmkal mentioned this pull request Mar 19, 2025
mmkal added a commit that referenced this pull request Mar 19, 2025
Follow on to #59 - this adds valibot support. Satisfyingly easy now that
the only requirement is json schema conversion. Others have done the
hard work!

Notes: 

- requires `@valibot/to-json-schema` installed. if not detected valibot
schemas will require raw json input (and output helptext explaining how
to get richer inputs)
- `@valibot/to-json-schema` throws when `v.pipe(...)` usage is detected,
so there's a custom preprocessor that takes advantage of how slim and
simple valibot schemas are - it just walks the "schema" object and swaps
`.pipe` arrays with only the first element and metadata elements. I
think this is safe but raised an issue against valibot:
open-circle/valibot#1090

---------

Co-authored-by: Misha Kaletsky <[email protected]>
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