schema-truth 0.0.0-stage → 0.8.1

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-DS9gtNZC.js +105 -0
  3. package/dist/ajv.d.ts +24 -0
  4. package/dist/ajv.js +1 -0
  5. package/dist/arktype-CRZ8vXZ5.js +803 -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-eAY8LqwP.js +124 -0
  23. package/dist/effect-BkWovUi3.js +678 -0
  24. package/dist/effect.d.ts +12 -0
  25. package/dist/effect.js +1 -0
  26. package/dist/helper-CbeedY35.js +1798 -0
  27. package/dist/index-DYzAccyT.d.ts +69 -0
  28. package/dist/index-G7UqNQ4w.d.ts +603 -0
  29. package/dist/index.d.ts +332 -0
  30. package/dist/index.js +10 -0
  31. package/dist/parser-CyJ2auea.js +36 -0
  32. package/dist/typebox-wYeJqy7T.js +563 -0
  33. package/dist/typebox.d.ts +21 -0
  34. package/dist/typebox.js +1 -0
  35. package/dist/utils-Bky98UmP.js +1146 -0
  36. package/dist/valibot-DQUEDYKt.js +705 -0
  37. package/dist/valibot.d.ts +9 -0
  38. package/dist/valibot.js +1 -0
  39. package/dist/yup-BMpa9Co1.js +92 -0
  40. package/dist/yup.d.ts +17 -0
  41. package/dist/yup.js +1 -0
  42. package/dist/zod-Dq9LtqPL.js +816 -0
  43. package/dist/zod.d.ts +9 -0
  44. package/dist/zod.js +1 -0
  45. package/package.json +112 -4
package/README.md CHANGED
@@ -1,3 +1,144 @@
1
- # Temporary Holding Version
1
+ # Schema Truth
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **[schema-truth](https://www.npmjs.com/package/schema-truth)** generates type-safe validation schemas from a [JSON Schema](https://json-schema.org/) or [OpenAPI](https://www.openapis.org/) document — one CLI per target library, each written in that library's own idiom.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -D schema-truth
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ One binary per target library, each taking the same arguments:
14
+
15
+ ```bash
16
+ npx schema-truth-zod path/to/input.{json,yaml} -o path/to/output.ts
17
+ npx schema-truth-valibot path/to/input.{json,yaml} -o path/to/output.ts
18
+ npx schema-truth-effect path/to/input.{json,yaml} -o path/to/output.ts
19
+ npx schema-truth-typebox path/to/input.{json,yaml} -o path/to/output.ts
20
+ npx schema-truth-arktype path/to/input.{json,yaml} -o path/to/output.ts
21
+ npx schema-truth-ajv path/to/input.{json,yaml} -o path/to/output.ts
22
+ npx schema-truth-yup path/to/input.{json,yaml} -o path/to/output.ts
23
+ ```
24
+
25
+ | Flag | What it does |
26
+ | ---------------- | ------------------------------------------------------- |
27
+ | `--output`, `-o` | TypeScript file the generated schema is written to |
28
+ | `--export-type` | Also export the type inferred from the generated schema |
29
+ | `--readonly` | Generate readonly types |
30
+
31
+ Run a command with `--help` for the full list.
32
+
33
+ ## Example
34
+
35
+ input:
36
+
37
+ ```json
38
+ {
39
+ "title": "User",
40
+ "type": "object",
41
+ "properties": {
42
+ "name": { "type": "string" },
43
+ "email": { "type": "string", "format": "email" },
44
+ "age": { "type": "integer", "minimum": 0 }
45
+ },
46
+ "required": ["name", "email"]
47
+ }
48
+ ```
49
+
50
+ output:
51
+
52
+ ### [Zod](https://zod.dev/)
53
+
54
+ ```ts
55
+ import * as z from 'zod'
56
+
57
+ export const User = z.object({
58
+ name: z.string(),
59
+ email: z.email(),
60
+ age: z.int().min(0).exactOptional(),
61
+ })
62
+ ```
63
+
64
+ ### [Valibot](https://valibot.dev/)
65
+
66
+ ```ts
67
+ import * as v from 'valibot'
68
+
69
+ export const User = v.object({
70
+ name: v.string(),
71
+ email: v.pipe(v.string(), v.email()),
72
+ age: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0))),
73
+ })
74
+ ```
75
+
76
+ ### [Effect Schema](https://effect.website/docs/v4/schema/introduction)
77
+
78
+ ```ts
79
+ import { Schema } from 'effect'
80
+
81
+ export const User = Schema.Struct({
82
+ name: Schema.String,
83
+ email: Schema.String.check(Schema.isPattern(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/)),
84
+ age: Schema.optional(Schema.Number.check(Schema.isInt(), Schema.isGreaterThanOrEqualTo(0))),
85
+ })
86
+ ```
87
+
88
+ ### [TypeBox](https://github.com/sinclairzx81/typebox)
89
+
90
+ ```ts
91
+ import { Type, type Static } from 'typebox'
92
+
93
+ export const User = Type.Object({
94
+ name: Type.String(),
95
+ email: Type.String({ format: 'email' }),
96
+ age: Type.Optional(Type.Integer({ minimum: 0 })),
97
+ })
98
+ ```
99
+
100
+ ### [ArkType](https://arktype.io/)
101
+
102
+ ```ts
103
+ import { type } from 'arktype'
104
+
105
+ export const User = type({ name: 'string', email: 'string.email', 'age?': 'number.integer >= 0' })
106
+ ```
107
+
108
+ ### [Ajv](https://ajv.js.org/)
109
+
110
+ ```ts
111
+ import Ajv from 'ajv'
112
+ import addFormats from 'ajv-formats'
113
+
114
+ const ajv = new Ajv()
115
+ addFormats(ajv)
116
+
117
+ export const schema = {
118
+ type: 'object',
119
+ properties: {
120
+ name: { type: 'string' },
121
+ email: { type: 'string', format: 'email' },
122
+ age: { type: 'integer', minimum: 0 },
123
+ },
124
+ required: ['name', 'email'],
125
+ }
126
+
127
+ export const validate = ajv.compile(schema)
128
+ ```
129
+
130
+ ### [Yup](https://github.com/jquense/yup)
131
+
132
+ ```ts
133
+ import * as yup from 'yup'
134
+
135
+ export const User = yup.object({
136
+ name: yup.string().required(),
137
+ email: yup.string().email().required(),
138
+ age: yup.number().integer().min(0),
139
+ })
140
+ ```
141
+
142
+ ## License
143
+
144
+ Distributed under the MIT License. See [LICENSE](https://github.com/nakita628/schema-truth?tab=MIT-1-ov-file) for more information.
@@ -0,0 +1,105 @@
1
+ import { c as normalizeTypes, s as makeSafeKey } from "./utils-Bky98UmP.js";
2
+ //#region src/generator/ajv/integer.ts
3
+ /**
4
+ * Reconstruct a draft-07 integer schema. `minimum` is the only numeric keyword
5
+ * in this slice; `maximum`, `exclusiveMinimum` and `multipleOf` are dropped.
6
+ */
7
+ function integer(schema) {
8
+ const parts = [`type:'integer'`];
9
+ if (typeof schema.minimum === "number") parts.push(`minimum:${schema.minimum}`);
10
+ return `{${parts.join(",")}}`;
11
+ }
12
+ //#endregion
13
+ //#region src/generator/ajv/number.ts
14
+ /**
15
+ * Reconstruct a draft-07 number schema. `minimum` is the only numeric keyword
16
+ * in this slice; `maximum`, `exclusiveMinimum` and `multipleOf` are dropped.
17
+ */
18
+ function number(schema) {
19
+ const parts = [`type:'number'`];
20
+ if (typeof schema.minimum === "number") parts.push(`minimum:${schema.minimum}`);
21
+ return `{${parts.join(",")}}`;
22
+ }
23
+ //#endregion
24
+ //#region src/generator/ajv/object.ts
25
+ /**
26
+ * Reconstruct a draft-07 object schema from `type`, `properties` and `required`.
27
+ * Property values recurse through {@link ajv}; other object keywords are dropped.
28
+ */
29
+ function object(schema) {
30
+ const parts = [`type:'object'`];
31
+ if (schema.properties !== void 0) {
32
+ const props = Object.entries(schema.properties).map(([key, propSchema]) => `${makeSafeKey(key)}:${ajv(propSchema)}`);
33
+ parts.push(`properties:{${props.join(",")}}`);
34
+ }
35
+ if (Array.isArray(schema.required) && schema.required.length > 0) {
36
+ const required = schema.required.filter((key) => typeof key === "string");
37
+ if (required.length > 0) parts.push(`required:[${required.map((key) => JSON.stringify(key)).join(",")}]`);
38
+ }
39
+ return `{${parts.join(",")}}`;
40
+ }
41
+ //#endregion
42
+ //#region src/generator/ajv/string.ts
43
+ /**
44
+ * Reconstruct a draft-07 string schema. `format` is emitted only for `email`
45
+ * (the first-slice format); every other string keyword is dropped.
46
+ */
47
+ function string(schema) {
48
+ const parts = [`type:'string'`];
49
+ if (schema.format === "email") parts.push(`format:'email'`);
50
+ return `{${parts.join(",")}}`;
51
+ }
52
+ //#endregion
53
+ //#region src/generator/ajv/ajv.ts
54
+ /**
55
+ * One schema node to one Ajv draft-07 schema-object expression.
56
+ *
57
+ * Unsupported keywords, combinators, `$ref` and `x-*` extensions are silent
58
+ * no-ops: the node still becomes a balanced object literal (`{}` when nothing
59
+ * in this slice applies).
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * ajv({ type: 'string', format: 'email' }) // "{type:'string',format:'email'}"
64
+ * ```
65
+ */
66
+ function ajv(schema) {
67
+ if (schema.properties !== void 0) return object(schema);
68
+ const types = normalizeTypes(schema.type);
69
+ if (types.includes("string")) return string(schema);
70
+ if (types.includes("number")) return number(schema);
71
+ if (types.includes("integer")) return integer(schema);
72
+ if (types.includes("object")) return object(schema);
73
+ return "{}";
74
+ }
75
+ //#endregion
76
+ //#region src/generator/ajv/index.ts
77
+ /**
78
+ * Convert a JSON Schema document to Ajv TypeScript: a reconstructed draft-07
79
+ * schema object and `ajv.compile(...)`.
80
+ *
81
+ * First slice: `object` / `string` / `number` / `integer`, `format: email`
82
+ * and `minimum`. Unsupported keywords, combinators, `$ref`, `$defs`, cycles,
83
+ * JTD and `x-*` extensions are silent no-ops. `--export-type` /
84
+ * `JSONSchemaType<T>` is out of scope and ignored.
85
+ *
86
+ * `addFormats(ajv)` is emitted only when a `format` this slice understands
87
+ * appears — Ajv's default class throws on unknown formats unless `strict` is
88
+ * off.
89
+ *
90
+ * @param schema - JSON Schema object to convert
91
+ * @param options - Accepted for CLI compatibility; `exportType` and `readonly` are ignored
92
+ * @returns Generated TypeScript/Ajv code string
93
+ */
94
+ function schemaToAjv(schema, options) {
95
+ const expression = ajv(schema);
96
+ const withFormats = expression.includes("format:'email'");
97
+ return [
98
+ withFormats ? `import Ajv from 'ajv'\nimport addFormats from 'ajv-formats'` : `import Ajv from 'ajv'`,
99
+ withFormats ? `const ajv = new Ajv()\naddFormats(ajv)` : `const ajv = new Ajv()`,
100
+ `export const schema = ${expression}`,
101
+ `export const validate = ajv.compile(schema)`
102
+ ].join("\n\n");
103
+ }
104
+ //#endregion
105
+ export { ajv as n, schemaToAjv as t };
package/dist/ajv.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { r as JSONSchema } from "./index-G7UqNQ4w.js";
2
+ //#region src/generator/ajv/index.d.ts
3
+ /**
4
+ * Convert a JSON Schema document to Ajv TypeScript: a reconstructed draft-07
5
+ * schema object and `ajv.compile(...)`.
6
+ *
7
+ * First slice: `object` / `string` / `number` / `integer`, `format: email`
8
+ * and `minimum`. Unsupported keywords, combinators, `$ref`, `$defs`, cycles,
9
+ * JTD and `x-*` extensions are silent no-ops. `--export-type` /
10
+ * `JSONSchemaType<T>` is out of scope and ignored.
11
+ *
12
+ * `addFormats(ajv)` is emitted only when a `format` this slice understands
13
+ * appears — Ajv's default class throws on unknown formats unless `strict` is
14
+ * off.
15
+ *
16
+ * @param schema - JSON Schema object to convert
17
+ * @param options - Accepted for CLI compatibility; `exportType` and `readonly` are ignored
18
+ * @returns Generated TypeScript/Ajv code string
19
+ */
20
+ export declare function schemaToAjv(schema: JSONSchema, options?: {
21
+ exportType?: boolean;
22
+ readonly?: boolean;
23
+ }): string;
24
+ //#endregion
package/dist/ajv.js ADDED
@@ -0,0 +1 @@
1
+ export { t as schemaToAjv } from "./ajv-DS9gtNZC.js";