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.
- package/README.md +143 -2
- package/dist/ajv-DTP2FXRr.js +105 -0
- package/dist/ajv.d.ts +24 -0
- package/dist/ajv.js +1 -0
- package/dist/arktype-BdvvaBQX.js +746 -0
- package/dist/arktype.d.ts +10 -0
- package/dist/arktype.js +1 -0
- package/dist/cli/ajv.d.ts +1 -0
- package/dist/cli/ajv.js +15 -0
- package/dist/cli/arktype.d.ts +1 -0
- package/dist/cli/arktype.js +15 -0
- package/dist/cli/effect.d.ts +1 -0
- package/dist/cli/effect.js +15 -0
- package/dist/cli/typebox.d.ts +1 -0
- package/dist/cli/typebox.js +15 -0
- package/dist/cli/valibot.d.ts +1 -0
- package/dist/cli/valibot.js +15 -0
- package/dist/cli/yup.d.ts +1 -0
- package/dist/cli/yup.js +15 -0
- package/dist/cli/zod.d.ts +1 -0
- package/dist/cli/zod.js +15 -0
- package/dist/cli-CL2mSark.js +124 -0
- package/dist/effect-mUknRPv_.js +645 -0
- package/dist/effect.d.ts +12 -0
- package/dist/effect.js +1 -0
- package/dist/helper-Cfvb5UDq.js +1115 -0
- package/dist/index-C2L8Ty0-.d.ts +65 -0
- package/dist/index-DbV3dEmN.d.ts +551 -0
- package/dist/index.d.ts +303 -0
- package/dist/index.js +10 -0
- package/dist/parser-CyJ2auea.js +36 -0
- package/dist/typebox-BdWJv3ND.js +544 -0
- package/dist/typebox.d.ts +21 -0
- package/dist/typebox.js +1 -0
- package/dist/utils-WNBq1HR0.js +1017 -0
- package/dist/valibot-CaJIynCH.js +664 -0
- package/dist/valibot.d.ts +9 -0
- package/dist/valibot.js +1 -0
- package/dist/yup-CM2TSFig.js +92 -0
- package/dist/yup.d.ts +17 -0
- package/dist/yup.js +1 -0
- package/dist/zod-BsXLZfwK.js +777 -0
- package/dist/zod.d.ts +9 -0
- package/dist/zod.js +1 -0
- package/package.json +112 -4
package/README.md
CHANGED
|
@@ -1,3 +1,144 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Schema Truth
|
|
2
2
|
|
|
3
|
-
|
|
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-WNBq1HR0.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-DbV3dEmN.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-DTP2FXRr.js";
|