schema-truth 0.0.0-stage → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +143 -2
  2. package/dist/ajv-DTP2FXRr.js +105 -0
  3. package/dist/ajv.d.ts +24 -0
  4. package/dist/ajv.js +1 -0
  5. package/dist/arktype-BdvvaBQX.js +746 -0
  6. package/dist/arktype.d.ts +10 -0
  7. package/dist/arktype.js +1 -0
  8. package/dist/cli/ajv.d.ts +1 -0
  9. package/dist/cli/ajv.js +15 -0
  10. package/dist/cli/arktype.d.ts +1 -0
  11. package/dist/cli/arktype.js +15 -0
  12. package/dist/cli/effect.d.ts +1 -0
  13. package/dist/cli/effect.js +15 -0
  14. package/dist/cli/typebox.d.ts +1 -0
  15. package/dist/cli/typebox.js +15 -0
  16. package/dist/cli/valibot.d.ts +1 -0
  17. package/dist/cli/valibot.js +15 -0
  18. package/dist/cli/yup.d.ts +1 -0
  19. package/dist/cli/yup.js +15 -0
  20. package/dist/cli/zod.d.ts +1 -0
  21. package/dist/cli/zod.js +15 -0
  22. package/dist/cli-CL2mSark.js +124 -0
  23. package/dist/effect-mUknRPv_.js +645 -0
  24. package/dist/effect.d.ts +12 -0
  25. package/dist/effect.js +1 -0
  26. package/dist/helper-Cfvb5UDq.js +1115 -0
  27. package/dist/index-C2L8Ty0-.d.ts +65 -0
  28. package/dist/index-DbV3dEmN.d.ts +551 -0
  29. package/dist/index.d.ts +303 -0
  30. package/dist/index.js +10 -0
  31. package/dist/parser-CyJ2auea.js +36 -0
  32. package/dist/typebox-BdWJv3ND.js +544 -0
  33. package/dist/typebox.d.ts +21 -0
  34. package/dist/typebox.js +1 -0
  35. package/dist/utils-WNBq1HR0.js +1017 -0
  36. package/dist/valibot-CaJIynCH.js +664 -0
  37. package/dist/valibot.d.ts +9 -0
  38. package/dist/valibot.js +1 -0
  39. package/dist/yup-CM2TSFig.js +92 -0
  40. package/dist/yup.d.ts +17 -0
  41. package/dist/yup.js +1 -0
  42. package/dist/zod-BsXLZfwK.js +777 -0
  43. package/dist/zod.d.ts +9 -0
  44. package/dist/zod.js +1 -0
  45. package/package.json +112 -4
@@ -0,0 +1,10 @@
1
+ import { r as JSONSchema } from "./index-DbV3dEmN.js";
2
+ import { n as GenerateOptions } from "./index-C2L8Ty0-.js";
3
+ //#region src/generator/arktype/index.d.ts
4
+ /**
5
+ * A whole JSON Schema document as an ArkType module. A document with definitions becomes
6
+ * a `scope({...}).export()` so members can name one another; one without is a bare
7
+ * `type(...)` export.
8
+ */
9
+ export declare function schemaToArktype(schema: JSONSchema, options?: GenerateOptions): string;
10
+ //#endregion
@@ -0,0 +1 @@
1
+ export { t as schemaToArktype } from "./arktype-BdvvaBQX.js";
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ import { t as schemaToAjv } from "../ajv-DTP2FXRr.js";
3
+ import { n as version, t as cli } from "../cli-CL2mSark.js";
4
+ import * as Effect from "effect/Effect";
5
+ import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
6
+ import * as NodeServices from "@effect/platform-node/NodeServices";
7
+ //#region src/ajv.ts
8
+ NodeRuntime.runMain(cli({
9
+ name: "schema-truth-ajv",
10
+ generator: schemaToAjv,
11
+ description: "Generate Ajv schemas from a JSON Schema document",
12
+ version
13
+ }).pipe(Effect.provide(NodeServices.layer)));
14
+ //#endregion
15
+ export {};
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ import { t as schemaToArktype } from "../arktype-BdvvaBQX.js";
3
+ import { n as version, t as cli } from "../cli-CL2mSark.js";
4
+ import * as Effect from "effect/Effect";
5
+ import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
6
+ import * as NodeServices from "@effect/platform-node/NodeServices";
7
+ //#region src/arktype.ts
8
+ NodeRuntime.runMain(cli({
9
+ name: "schema-truth-arktype",
10
+ generator: schemaToArktype,
11
+ description: "Generate ArkType schemas from a JSON Schema document",
12
+ version
13
+ }).pipe(Effect.provide(NodeServices.layer)));
14
+ //#endregion
15
+ export {};
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ import { t as schemaToEffect } from "../effect-mUknRPv_.js";
3
+ import { n as version, t as cli } from "../cli-CL2mSark.js";
4
+ import * as Effect from "effect/Effect";
5
+ import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
6
+ import * as NodeServices from "@effect/platform-node/NodeServices";
7
+ //#region src/effect.ts
8
+ NodeRuntime.runMain(cli({
9
+ name: "schema-truth-effect",
10
+ generator: schemaToEffect,
11
+ description: "Generate Effect Schema code from a JSON Schema document",
12
+ version
13
+ }).pipe(Effect.provide(NodeServices.layer)));
14
+ //#endregion
15
+ export {};
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ import { t as schemaToTypebox } from "../typebox-BdWJv3ND.js";
3
+ import { n as version, t as cli } from "../cli-CL2mSark.js";
4
+ import * as Effect from "effect/Effect";
5
+ import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
6
+ import * as NodeServices from "@effect/platform-node/NodeServices";
7
+ //#region src/typebox.ts
8
+ NodeRuntime.runMain(cli({
9
+ name: "schema-truth-typebox",
10
+ generator: schemaToTypebox,
11
+ description: "Generate TypeBox schemas from a JSON Schema document",
12
+ version
13
+ }).pipe(Effect.provide(NodeServices.layer)));
14
+ //#endregion
15
+ export {};
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ import { t as schemaToValibot } from "../valibot-CaJIynCH.js";
3
+ import { n as version, t as cli } from "../cli-CL2mSark.js";
4
+ import * as Effect from "effect/Effect";
5
+ import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
6
+ import * as NodeServices from "@effect/platform-node/NodeServices";
7
+ //#region src/valibot.ts
8
+ NodeRuntime.runMain(cli({
9
+ name: "schema-truth-valibot",
10
+ generator: schemaToValibot,
11
+ description: "Generate Valibot schemas from a JSON Schema document",
12
+ version
13
+ }).pipe(Effect.provide(NodeServices.layer)));
14
+ //#endregion
15
+ export {};
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ import { t as schemaToYup } from "../yup-CM2TSFig.js";
3
+ import { n as version, t as cli } from "../cli-CL2mSark.js";
4
+ import * as Effect from "effect/Effect";
5
+ import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
6
+ import * as NodeServices from "@effect/platform-node/NodeServices";
7
+ //#region src/yup.ts
8
+ NodeRuntime.runMain(cli({
9
+ name: "schema-truth-yup",
10
+ generator: schemaToYup,
11
+ description: "Generate Yup schemas from a JSON Schema document",
12
+ version
13
+ }).pipe(Effect.provide(NodeServices.layer)));
14
+ //#endregion
15
+ export {};
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ import { t as schemaToZod } from "../zod-BsXLZfwK.js";
3
+ import { n as version, t as cli } from "../cli-CL2mSark.js";
4
+ import * as Effect from "effect/Effect";
5
+ import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
6
+ import * as NodeServices from "@effect/platform-node/NodeServices";
7
+ //#region src/zod.ts
8
+ NodeRuntime.runMain(cli({
9
+ name: "schema-truth-zod",
10
+ generator: schemaToZod,
11
+ description: "Generate Zod schemas from a JSON Schema document",
12
+ version
13
+ }).pipe(Effect.provide(NodeServices.layer)));
14
+ //#endregion
15
+ export {};
@@ -0,0 +1,124 @@
1
+ import { L as isRecord } from "./utils-WNBq1HR0.js";
2
+ import { i as fmt, n as parseSchemaFile } from "./parser-CyJ2auea.js";
3
+ import * as Effect from "effect/Effect";
4
+ import path from "node:path";
5
+ import { Argument, CliError, Command, Flag } from "effect/cli";
6
+ import * as Console from "effect/Console";
7
+ import * as Schema from "effect/Schema";
8
+ import * as Stdio from "effect/Stdio";
9
+ import "@effect/platform-node/NodeFileSystem";
10
+ import * as FileSystem from "effect/FileSystem";
11
+ //#region package.json
12
+ var version = "0.7.0";
13
+ //#endregion
14
+ //#region src/file/index.ts
15
+ /** Creates the directory and its parents. An existing directory is not an error. */
16
+ function mkdir(dir) {
17
+ return Effect.gen(function* () {
18
+ yield* (yield* FileSystem.FileSystem).makeDirectory(dir, { recursive: true });
19
+ });
20
+ }
21
+ /**
22
+ * Writes a UTF-8 file, leaving it untouched when the contents already match.
23
+ *
24
+ * Skipping the identical write keeps a no-op generation from changing mtime. An
25
+ * unreadable existing file falls through to the write rather than failing here.
26
+ */
27
+ function writeFile(path, data) {
28
+ return Effect.gen(function* () {
29
+ const fs = yield* FileSystem.FileSystem;
30
+ if ((yield* fs.readFileString(path).pipe(Effect.orElseSucceed(() => null))) === data) return;
31
+ yield* fs.writeFileString(path, data);
32
+ });
33
+ }
34
+ //#endregion
35
+ //#region src/cli/index.ts
36
+ const InputPathSchema = Schema.String.pipe(Schema.refine(Schema.is(Schema.TemplateLiteral([Schema.String, Schema.Literals([".json", ".yaml"])])), { message: "a JSON Schema document ending in .json or .yaml" }));
37
+ const OutputPathSchema = Schema.String.pipe(Schema.refine(Schema.is(Schema.TemplateLiteral([Schema.String, ".ts"])), { message: "a TypeScript file path ending in .ts" }));
38
+ /**
39
+ * The command line every `schema-truth-*` binary accepts: what each piece means, and the
40
+ * schema every value is decoded through before {@link generate} ever sees it.
41
+ */
42
+ const commandLine = {
43
+ input: Argument.File("input", { mustExist: true }).pipe(Argument.withSchema(InputPathSchema), Argument.withDescription("JSON Schema document to generate from"), Argument.withMetavar("input.{json,yaml}")),
44
+ output: Flag.String("output").pipe(Flag.withAlias("o"), Flag.withSchema(OutputPathSchema), Flag.withDescription("TypeScript file the generated schema is written to"), Flag.withMetavar("output.ts")),
45
+ exportType: Flag.Boolean("export-type").pipe(Flag.withDescription("Include the inferred type export in the output"), Flag.withDefault(false)),
46
+ readonly: Flag.Boolean("readonly").pipe(Flag.withDescription("Generate readonly types"), Flag.withDefault(false))
47
+ };
48
+ /**
49
+ * The sentence a failure is reported with.
50
+ *
51
+ * `FileSystem` normalises a host failure to `BadResource: FileSystem.writeFile (path)`,
52
+ * which says what could not be done but not why. The Node error it wrapped is kept on
53
+ * `reason.cause` and still carries the `ENOTDIR` / `EISDIR` line, so it is appended
54
+ * whenever there is one.
55
+ */
56
+ function describeFailure(error) {
57
+ const cause = platformCause(error);
58
+ return cause instanceof Error && cause.message !== "" ? `${error.message}: ${cause.message}` : error.message;
59
+ }
60
+ /** The host error a `PlatformError` wrapped, when it is one and it kept one. */
61
+ function platformCause(error) {
62
+ if (!isRecord(error)) return void 0;
63
+ const reason = error.reason;
64
+ return isRecord(reason) ? reason.cause : void 0;
65
+ }
66
+ /**
67
+ * Everything the command does once the command line has parsed: read the document,
68
+ * generate, format, and write the file — creating the directories leading to it, so an
69
+ * output under a path that does not exist yet is written rather than refused.
70
+ *
71
+ * Everything past here fails with something carrying a `message`, so the single
72
+ * `mapError` at the end is where all of it turns into rendered CLI output. The
73
+ * `FileSystem` it writes through comes from the environment the caller provides.
74
+ */
75
+ function generate(generator) {
76
+ return (args) => Effect.gen(function* () {
77
+ const schema = yield* parseSchemaFile(args.input);
78
+ const source = yield* fmt(generator(schema, {
79
+ exportType: args.exportType,
80
+ readonly: args.readonly
81
+ }));
82
+ yield* mkdir(path.dirname(args.output));
83
+ yield* writeFile(args.output, source);
84
+ return yield* Console.log(`Generated: ${args.output}`);
85
+ }).pipe(Effect.mapError((error) => CliError.isCliError(error) ? error : new CliError.UserError({
86
+ cause: error,
87
+ userMessage: describeFailure(error)
88
+ })));
89
+ }
90
+ /**
91
+ * The command for one generator: parsing, validation, `--help`, `--version` and shell
92
+ * completions are owned by `effect/cli`, {@link generate} is the rest.
93
+ *
94
+ * Every binary is the same command with a different name and generator, so they are built
95
+ * from one definition rather than one copy per target that can drift apart.
96
+ */
97
+ function makeCli(options) {
98
+ return Command.make(options.name, commandLine, generate(options.generator)).pipe(Command.withDescription(options.description), Command.withExamples([
99
+ {
100
+ command: `${options.name} schema.json -o src/schema.ts`,
101
+ description: "Generate a schema file"
102
+ },
103
+ {
104
+ command: `${options.name} schema.yaml -o src/schema.ts --export-type`,
105
+ description: "Also export the inferred type"
106
+ },
107
+ {
108
+ command: `${options.name} schema.json -o src/schema.ts --readonly`,
109
+ description: "Generate readonly types"
110
+ }
111
+ ]));
112
+ }
113
+ /** Runs a `schema-truth-*` command against an explicit argument list. */
114
+ function runCli(options, argv) {
115
+ return Command.runWith(makeCli(options), { version: options.version })(argv);
116
+ }
117
+ /** The entry point each binary runs, reading its arguments the way `Command.run` does. */
118
+ function cli(options) {
119
+ return Effect.gen(function* () {
120
+ return yield* runCli(options, yield* (yield* Stdio.Stdio).args);
121
+ });
122
+ }
123
+ //#endregion
124
+ export { version as n, cli as t };