@temporary-name/zod 1.9.3-alpha.0d2fa3247b6b23bbc2e3097a95f631b86740e4d8
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/LICENSE +21 -0
- package/README.md +67 -0
- package/dist/index.d.mts +91 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.mjs +955 -0
- package/dist/zod4/index.d.mts +314 -0
- package/dist/zod4/index.d.ts +314 -0
- package/dist/zod4/index.mjs +743 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Stainless
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
## `@temporary-name/zod`
|
|
2
|
+
|
|
3
|
+
More schemas that [Zod](https://zod.dev/) doesn't support yet, and provides `ZodToJsonSchemaConverter` for generating OpenAPI specs.
|
|
4
|
+
|
|
5
|
+
### More Schemas
|
|
6
|
+
|
|
7
|
+
- `oz.url`: Zod schema for [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) instance.
|
|
8
|
+
- `oz.blob`: Zod schema for [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) instance.
|
|
9
|
+
- `oz.file`: Zod schema for [File](https://developer.mozilla.org/en-US/docs/Web/API/File) instance.
|
|
10
|
+
- `oz.regexp`: Zod schema for [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) instance.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { oz } from '@temporary-name/zod'
|
|
14
|
+
import { z } from 'zod/v3'
|
|
15
|
+
|
|
16
|
+
const Example = z.object({
|
|
17
|
+
url: oz.url(),
|
|
18
|
+
blob: oz.blob(),
|
|
19
|
+
file: oz.file().type('image/png'),
|
|
20
|
+
regexp: oz.regexp(),
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Generate OpenAPI Spec
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { OpenAPIGenerator } from '@temporary-name/openapi'
|
|
28
|
+
import { ZodToJsonSchemaConverter } from '@temporary-name/zod/zod4'
|
|
29
|
+
|
|
30
|
+
const openAPIGenerator = new OpenAPIGenerator({
|
|
31
|
+
schemaConverters: [new ZodToJsonSchemaConverter()],
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
const specFromContract = await openAPIGenerator.generate(contract, {
|
|
35
|
+
info: {
|
|
36
|
+
title: 'My App',
|
|
37
|
+
version: '0.0.0',
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const specFromRouter = await openAPIGenerator.generate(router, {
|
|
42
|
+
info: {
|
|
43
|
+
title: 'My App',
|
|
44
|
+
version: '0.0.0',
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Extending the Specification
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { oz } from '@temporary-name/zod'
|
|
53
|
+
import * as z from 'zod'
|
|
54
|
+
|
|
55
|
+
const InputSchema = oz.openapi(
|
|
56
|
+
z.object({
|
|
57
|
+
name: z.string(),
|
|
58
|
+
}),
|
|
59
|
+
{
|
|
60
|
+
examples: [
|
|
61
|
+
{ name: 'Earth' },
|
|
62
|
+
{ name: 'Mars' },
|
|
63
|
+
],
|
|
64
|
+
// additional options...
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { JSONSchema, ConditionalSchemaConverter, SchemaConvertOptions } from '@temporary-name/openapi';
|
|
2
|
+
import { ZodTypeAny, input, output, ZodTypeDef, CustomErrorParams, ZodType, ZodEffects } from 'zod/v3';
|
|
3
|
+
import { Context } from '@temporary-name/server';
|
|
4
|
+
import { StandardHandlerPlugin, StandardHandlerOptions } from '@temporary-name/server/standard';
|
|
5
|
+
import { AnySchema } from '@temporary-name/contract';
|
|
6
|
+
|
|
7
|
+
declare function getCustomJsonSchema(def: ZodTypeDef, options: {
|
|
8
|
+
strategy: 'input' | 'output' | 'both';
|
|
9
|
+
}): Exclude<JSONSchema, boolean> | undefined;
|
|
10
|
+
declare function customJsonSchema<T extends ZodTypeAny, TStrategy extends 'input' | 'output' | 'both' = 'both'>(schema: T, custom: Exclude<JSONSchema<TStrategy extends 'input' ? input<T> : TStrategy extends 'output' ? output<T> : input<T> & output<T>>, boolean>, options?: {
|
|
11
|
+
strategy?: TStrategy;
|
|
12
|
+
}): T;
|
|
13
|
+
|
|
14
|
+
type CustomParams = CustomErrorParams & {
|
|
15
|
+
fatal?: boolean;
|
|
16
|
+
};
|
|
17
|
+
type CustomZodDef = {
|
|
18
|
+
type: 'blob' | 'regexp' | 'url';
|
|
19
|
+
} | {
|
|
20
|
+
type: 'file';
|
|
21
|
+
mimeType?: string;
|
|
22
|
+
};
|
|
23
|
+
declare function setCustomZodDef<T extends ZodTypeDef>(def: T, custom: CustomZodDef): void;
|
|
24
|
+
declare function getCustomZodDef(def: ZodTypeDef): CustomZodDef | undefined;
|
|
25
|
+
declare function composeParams<T = unknown>(defaultMessage: (input: T) => string, params: undefined | string | CustomParams | ((input: T) => CustomParams)): (input: T) => CustomParams;
|
|
26
|
+
|
|
27
|
+
declare function blob(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<Blob, ZodTypeDef, Blob>;
|
|
28
|
+
|
|
29
|
+
declare function file(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<File, ZodTypeDef, File> & {
|
|
30
|
+
type(mimeType: string, params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodEffects<ZodType<File, ZodTypeDef, File>, File, File>;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
declare function regexp(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<RegExp, ZodTypeDef, RegExp>;
|
|
34
|
+
|
|
35
|
+
declare function url(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<URL, ZodTypeDef, URL>;
|
|
36
|
+
|
|
37
|
+
declare class ZodSmartCoercionPlugin<TContext extends Context> implements StandardHandlerPlugin<TContext> {
|
|
38
|
+
init(options: StandardHandlerOptions<TContext>): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface ZodToJsonSchemaOptions {
|
|
42
|
+
/**
|
|
43
|
+
* Max depth of lazy type
|
|
44
|
+
*
|
|
45
|
+
* Used `{}` when exceed max depth
|
|
46
|
+
*
|
|
47
|
+
* @default 3
|
|
48
|
+
*/
|
|
49
|
+
maxLazyDepth?: number;
|
|
50
|
+
/**
|
|
51
|
+
* Max depth of nested types
|
|
52
|
+
*
|
|
53
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
|
54
|
+
*
|
|
55
|
+
* @default 10
|
|
56
|
+
*/
|
|
57
|
+
maxStructureDepth?: number;
|
|
58
|
+
/**
|
|
59
|
+
* The schema to be used to represent the any | unknown type.
|
|
60
|
+
*
|
|
61
|
+
* @default { }
|
|
62
|
+
*/
|
|
63
|
+
anyJsonSchema?: Exclude<JSONSchema, boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* The schema to be used when the Zod schema is unsupported.
|
|
66
|
+
*
|
|
67
|
+
* @default { not: {} }
|
|
68
|
+
*/
|
|
69
|
+
unsupportedJsonSchema?: Exclude<JSONSchema, boolean>;
|
|
70
|
+
}
|
|
71
|
+
declare class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
|
|
72
|
+
#private;
|
|
73
|
+
private readonly maxLazyDepth;
|
|
74
|
+
private readonly maxStructureDepth;
|
|
75
|
+
private readonly unsupportedJsonSchema;
|
|
76
|
+
private readonly anyJsonSchema;
|
|
77
|
+
constructor(options?: ZodToJsonSchemaOptions);
|
|
78
|
+
condition(schema: AnySchema | undefined): boolean;
|
|
79
|
+
convert(schema: AnySchema | undefined, options: SchemaConvertOptions, lazyDepth?: number, isHandledCustomJSONSchema?: boolean, isHandledZodDescription?: boolean, structureDepth?: number): [required: boolean, jsonSchema: Exclude<JSONSchema, boolean>];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
declare const oz: {
|
|
83
|
+
file: typeof file;
|
|
84
|
+
blob: typeof blob;
|
|
85
|
+
url: typeof url;
|
|
86
|
+
regexp: typeof regexp;
|
|
87
|
+
openapi: typeof customJsonSchema;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export { ZodSmartCoercionPlugin, ZodToJsonSchemaConverter, blob, composeParams, customJsonSchema, file, getCustomJsonSchema, getCustomZodDef, oz, regexp, setCustomZodDef, url };
|
|
91
|
+
export type { CustomParams, CustomZodDef, ZodToJsonSchemaOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { JSONSchema, ConditionalSchemaConverter, SchemaConvertOptions } from '@temporary-name/openapi';
|
|
2
|
+
import { ZodTypeAny, input, output, ZodTypeDef, CustomErrorParams, ZodType, ZodEffects } from 'zod/v3';
|
|
3
|
+
import { Context } from '@temporary-name/server';
|
|
4
|
+
import { StandardHandlerPlugin, StandardHandlerOptions } from '@temporary-name/server/standard';
|
|
5
|
+
import { AnySchema } from '@temporary-name/contract';
|
|
6
|
+
|
|
7
|
+
declare function getCustomJsonSchema(def: ZodTypeDef, options: {
|
|
8
|
+
strategy: 'input' | 'output' | 'both';
|
|
9
|
+
}): Exclude<JSONSchema, boolean> | undefined;
|
|
10
|
+
declare function customJsonSchema<T extends ZodTypeAny, TStrategy extends 'input' | 'output' | 'both' = 'both'>(schema: T, custom: Exclude<JSONSchema<TStrategy extends 'input' ? input<T> : TStrategy extends 'output' ? output<T> : input<T> & output<T>>, boolean>, options?: {
|
|
11
|
+
strategy?: TStrategy;
|
|
12
|
+
}): T;
|
|
13
|
+
|
|
14
|
+
type CustomParams = CustomErrorParams & {
|
|
15
|
+
fatal?: boolean;
|
|
16
|
+
};
|
|
17
|
+
type CustomZodDef = {
|
|
18
|
+
type: 'blob' | 'regexp' | 'url';
|
|
19
|
+
} | {
|
|
20
|
+
type: 'file';
|
|
21
|
+
mimeType?: string;
|
|
22
|
+
};
|
|
23
|
+
declare function setCustomZodDef<T extends ZodTypeDef>(def: T, custom: CustomZodDef): void;
|
|
24
|
+
declare function getCustomZodDef(def: ZodTypeDef): CustomZodDef | undefined;
|
|
25
|
+
declare function composeParams<T = unknown>(defaultMessage: (input: T) => string, params: undefined | string | CustomParams | ((input: T) => CustomParams)): (input: T) => CustomParams;
|
|
26
|
+
|
|
27
|
+
declare function blob(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<Blob, ZodTypeDef, Blob>;
|
|
28
|
+
|
|
29
|
+
declare function file(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<File, ZodTypeDef, File> & {
|
|
30
|
+
type(mimeType: string, params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodEffects<ZodType<File, ZodTypeDef, File>, File, File>;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
declare function regexp(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<RegExp, ZodTypeDef, RegExp>;
|
|
34
|
+
|
|
35
|
+
declare function url(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<URL, ZodTypeDef, URL>;
|
|
36
|
+
|
|
37
|
+
declare class ZodSmartCoercionPlugin<TContext extends Context> implements StandardHandlerPlugin<TContext> {
|
|
38
|
+
init(options: StandardHandlerOptions<TContext>): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface ZodToJsonSchemaOptions {
|
|
42
|
+
/**
|
|
43
|
+
* Max depth of lazy type
|
|
44
|
+
*
|
|
45
|
+
* Used `{}` when exceed max depth
|
|
46
|
+
*
|
|
47
|
+
* @default 3
|
|
48
|
+
*/
|
|
49
|
+
maxLazyDepth?: number;
|
|
50
|
+
/**
|
|
51
|
+
* Max depth of nested types
|
|
52
|
+
*
|
|
53
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
|
54
|
+
*
|
|
55
|
+
* @default 10
|
|
56
|
+
*/
|
|
57
|
+
maxStructureDepth?: number;
|
|
58
|
+
/**
|
|
59
|
+
* The schema to be used to represent the any | unknown type.
|
|
60
|
+
*
|
|
61
|
+
* @default { }
|
|
62
|
+
*/
|
|
63
|
+
anyJsonSchema?: Exclude<JSONSchema, boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* The schema to be used when the Zod schema is unsupported.
|
|
66
|
+
*
|
|
67
|
+
* @default { not: {} }
|
|
68
|
+
*/
|
|
69
|
+
unsupportedJsonSchema?: Exclude<JSONSchema, boolean>;
|
|
70
|
+
}
|
|
71
|
+
declare class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
|
|
72
|
+
#private;
|
|
73
|
+
private readonly maxLazyDepth;
|
|
74
|
+
private readonly maxStructureDepth;
|
|
75
|
+
private readonly unsupportedJsonSchema;
|
|
76
|
+
private readonly anyJsonSchema;
|
|
77
|
+
constructor(options?: ZodToJsonSchemaOptions);
|
|
78
|
+
condition(schema: AnySchema | undefined): boolean;
|
|
79
|
+
convert(schema: AnySchema | undefined, options: SchemaConvertOptions, lazyDepth?: number, isHandledCustomJSONSchema?: boolean, isHandledZodDescription?: boolean, structureDepth?: number): [required: boolean, jsonSchema: Exclude<JSONSchema, boolean>];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
declare const oz: {
|
|
83
|
+
file: typeof file;
|
|
84
|
+
blob: typeof blob;
|
|
85
|
+
url: typeof url;
|
|
86
|
+
regexp: typeof regexp;
|
|
87
|
+
openapi: typeof customJsonSchema;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export { ZodSmartCoercionPlugin, ZodToJsonSchemaConverter, blob, composeParams, customJsonSchema, file, getCustomJsonSchema, getCustomZodDef, oz, regexp, setCustomZodDef, url };
|
|
91
|
+
export type { CustomParams, CustomZodDef, ZodToJsonSchemaOptions };
|