@express-zod-api/zod-plugin 1.1.0-beta.1 → 1.2.0-beta.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.
- package/CHANGELOG.md +5 -1
- package/README.md +2 -2
- package/dist/augmentation.d.ts +48 -0
- package/dist/index.d.ts +12 -60
- package/dist/index.js +1 -1
- package/package.json +2 -3
package/CHANGELOG.md
CHANGED
|
@@ -2,9 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
## Version 1
|
|
4
4
|
|
|
5
|
+
### v1.2.0
|
|
6
|
+
|
|
7
|
+
- Changed bundler from `tsup` to `tsdown`.
|
|
8
|
+
|
|
5
9
|
### v1.1.0
|
|
6
10
|
|
|
7
|
-
- Added low-level helpers `pack()` and `unpack()` to handle inheritable
|
|
11
|
+
- Added low-level helpers `pack()` and `unpack()` to handle inheritable attributes that withstands refinements:
|
|
8
12
|
- `schema.brand()` is a shorthand for `pack(schema, { brand })`;
|
|
9
13
|
- `getBrand()` is a shorthand for `unpack(schema).brand`.
|
|
10
14
|
|
package/README.md
CHANGED
|
@@ -35,5 +35,5 @@ schema.meta(); // { examples: ["test", "another"] }
|
|
|
35
35
|
## Helpers
|
|
36
36
|
|
|
37
37
|
- `getBrand()` — retrieves the brand from the schema that was set by its `.brand()` method;
|
|
38
|
-
- `pack()` — returns a cloned schema having inheritable
|
|
39
|
-
- `unpack()` — retrieves the
|
|
38
|
+
- `pack()` — returns a cloned schema having inheritable attributes assigned (such as brand);
|
|
39
|
+
- `unpack()` — retrieves the attributes from the schema that was set by `pack()` helper.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region remap.d.ts
|
|
4
|
+
type TuplesFromObject<T> = { [P in keyof T]: [P, T[P]] }[keyof T];
|
|
5
|
+
type GetKeyByValue<T, V> = TuplesFromObject<T> extends infer TT ? TT extends [infer P, V] ? P : never : never;
|
|
6
|
+
/**
|
|
7
|
+
* @fileoverview Mapping utils for Zod Runtime Plugin (remap)
|
|
8
|
+
* @link https://stackoverflow.com/questions/55454125/typescript-remapping-object-properties-in-typesafe
|
|
9
|
+
* @todo try to reuse R.Remap from Ramda (requires to move its types to prod dependencies)
|
|
10
|
+
*/
|
|
11
|
+
type Remap<T, U extends { [P in keyof T]?: V }, V extends string> = { [P in NonNullable<U[keyof U]>]: T[GetKeyByValue<U, P>] };
|
|
12
|
+
type Intact<T, U> = { [K in Exclude<keyof T, keyof U>]: T[K] };
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region augmentation.d.ts
|
|
15
|
+
declare module "zod/v4/core" {
|
|
16
|
+
interface GlobalMeta {
|
|
17
|
+
default?: unknown;
|
|
18
|
+
examples?: unknown[];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @fileoverview Zod Runtime Plugin
|
|
23
|
+
* @see https://github.com/colinhacks/zod/blob/90efe7fa6135119224412c7081bd12ef0bccef26/plugin/effect/src/index.ts#L21-L31
|
|
24
|
+
* @desc This code modifies and extends zod's functionality immediately when importing the plugin.
|
|
25
|
+
* @desc Enables .example() and .deprecated() on all schemas (ZodType)
|
|
26
|
+
* @desc Enables .label() on ZodDefault
|
|
27
|
+
* @desc Enables .remap() on ZodObject
|
|
28
|
+
* @desc Stores the argument supplied to .brand() on all schemas (runtime distinguishable branded types)
|
|
29
|
+
* */
|
|
30
|
+
declare module "zod" {
|
|
31
|
+
interface ZodType<out Output = unknown, out Input = unknown, out Internals extends z.core.$ZodTypeInternals<Output, Input> = z.core.$ZodTypeInternals<Output, Input>> extends z.core.$ZodType<Output, Input, Internals> {
|
|
32
|
+
/** @desc Shorthand for .meta({ examples }) */
|
|
33
|
+
example(example: z.output<this>): this;
|
|
34
|
+
deprecated(): this;
|
|
35
|
+
}
|
|
36
|
+
interface ZodDefault<T extends z.core.SomeType = z.core.$ZodType> extends z._ZodType<z.core.$ZodDefaultInternals<T>>, z.core.$ZodDefault<T> {
|
|
37
|
+
/** @desc Shorthand for .meta({ default }) */
|
|
38
|
+
label(label: string): this;
|
|
39
|
+
}
|
|
40
|
+
interface ZodObject<out Shape extends z.core.$ZodShape = z.core.$ZodLooseShape, out Config extends z.core.$ZodObjectConfig = z.core.$strip> extends z._ZodType<z.core.$ZodObjectInternals<Shape, Config>>, z.core.$ZodObject<Shape, Config> {
|
|
41
|
+
remap<V extends string, U extends { [P in keyof Shape]?: V }>(mapping: U): z.ZodPipe<z.ZodPipe<this, z.ZodTransform>,
|
|
42
|
+
// internal type simplified
|
|
43
|
+
z.ZodObject<Remap<Shape, U, V> & Intact<Shape, U>, Config>>;
|
|
44
|
+
remap<U extends z.core.$ZodShape>(mapper: (subject: Shape) => U): z.ZodPipe<z.ZodPipe<this, z.ZodTransform>, z.ZodObject<U>>;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,76 +1,28 @@
|
|
|
1
|
-
import
|
|
1
|
+
import './augmentation.js';
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
[P in keyof T]: [P, T[P]];
|
|
5
|
-
}[keyof T];
|
|
6
|
-
type GetKeyByValue<T, V> = TuplesFromObject<T> extends infer TT ? TT extends [infer P, V] ? P : never : never;
|
|
7
|
-
/**
|
|
8
|
-
* @fileoverview Mapping utils for Zod Runtime Plugin (remap)
|
|
9
|
-
* @link https://stackoverflow.com/questions/55454125/typescript-remapping-object-properties-in-typesafe
|
|
10
|
-
* @todo try to reuse R.Remap from Ramda (requires to move its types to prod dependencies)
|
|
11
|
-
*/
|
|
12
|
-
type Remap<T, U extends {
|
|
13
|
-
[P in keyof T]?: V;
|
|
14
|
-
}, V extends string> = {
|
|
15
|
-
[P in NonNullable<U[keyof U]>]: T[GetKeyByValue<U, P>];
|
|
16
|
-
};
|
|
17
|
-
type Intact<T, U> = {
|
|
18
|
-
[K in Exclude<keyof T, keyof U>]: T[K];
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
declare module "zod/v4/core" {
|
|
22
|
-
interface GlobalMeta {
|
|
23
|
-
default?: unknown;
|
|
24
|
-
examples?: unknown[];
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* @fileoverview Zod Runtime Plugin
|
|
29
|
-
* @see https://github.com/colinhacks/zod/blob/90efe7fa6135119224412c7081bd12ef0bccef26/plugin/effect/src/index.ts#L21-L31
|
|
30
|
-
* @desc This code modifies and extends zod's functionality immediately when importing the plugin.
|
|
31
|
-
* @desc Enables .example() and .deprecated() on all schemas (ZodType)
|
|
32
|
-
* @desc Enables .label() on ZodDefault
|
|
33
|
-
* @desc Enables .remap() on ZodObject
|
|
34
|
-
* @desc Stores the argument supplied to .brand() on all schemas (runtime distinguishable branded types)
|
|
35
|
-
* */
|
|
36
|
-
declare module "zod" {
|
|
37
|
-
interface ZodType<out Output = unknown, out Input = unknown, out Internals extends z.core.$ZodTypeInternals<Output, Input> = z.core.$ZodTypeInternals<Output, Input>> extends z.core.$ZodType<Output, Input, Internals> {
|
|
38
|
-
/** @desc Shorthand for .meta({ examples }) */
|
|
39
|
-
example(example: z.output<this>): this;
|
|
40
|
-
deprecated(): this;
|
|
41
|
-
}
|
|
42
|
-
interface ZodDefault<T extends z.core.SomeType = z.core.$ZodType> extends z._ZodType<z.core.$ZodDefaultInternals<T>>, z.core.$ZodDefault<T> {
|
|
43
|
-
/** @desc Shorthand for .meta({ default }) */
|
|
44
|
-
label(label: string): this;
|
|
45
|
-
}
|
|
46
|
-
interface ZodObject<out Shape extends z.core.$ZodShape = z.core.$ZodLooseShape, out Config extends z.core.$ZodObjectConfig = z.core.$strip> extends z._ZodType<z.core.$ZodObjectInternals<Shape, Config>>, z.core.$ZodObject<Shape, Config> {
|
|
47
|
-
remap<V extends string, U extends {
|
|
48
|
-
[P in keyof Shape]?: V;
|
|
49
|
-
}>(mapping: U): z.ZodPipe<z.ZodPipe<this, z.ZodTransform>, // internal type simplified
|
|
50
|
-
z.ZodObject<Remap<Shape, U, V> & Intact<Shape, U>, Config>>;
|
|
51
|
-
remap<U extends z.core.$ZodShape>(mapper: (subject: Shape) => U): z.ZodPipe<z.ZodPipe<this, z.ZodTransform>, z.ZodObject<U>>;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
4
|
+
//#region packer.d.ts
|
|
54
5
|
|
|
55
6
|
/**
|
|
56
7
|
* @public
|
|
57
|
-
* @desc Attaches an inheritable
|
|
8
|
+
* @desc Attaches an inheritable attributes to the schema (withstands refinements).
|
|
58
9
|
*/
|
|
59
10
|
declare const pack: <T extends z.ZodType, B extends object>(subject: T, bag: B) => T & {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
11
|
+
_zod: {
|
|
12
|
+
bag: T["_zod"]["bag"] & B;
|
|
13
|
+
};
|
|
63
14
|
};
|
|
64
15
|
/**
|
|
65
16
|
* @public
|
|
66
|
-
* @desc Retrieves the
|
|
17
|
+
* @desc Retrieves the attributes attached to the schema by pack() method.
|
|
67
18
|
*/
|
|
68
19
|
declare const unpack: <T extends z.core.$ZodType>(subject: T) => T["_zod"]["bag"];
|
|
69
|
-
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region brand.d.ts
|
|
70
22
|
/**
|
|
71
23
|
* @public
|
|
72
24
|
* @desc Retrieves the brand from the schema set by its .brand() method.
|
|
73
25
|
* */
|
|
74
26
|
declare const getBrand: (subject: z.core.$ZodType) => string | number | symbol | undefined;
|
|
75
|
-
|
|
76
|
-
export { getBrand, pack, unpack };
|
|
27
|
+
//#endregion
|
|
28
|
+
export { getBrand, pack, unpack };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{globalRegistry as
|
|
1
|
+
import{globalRegistry as e,z as t}from"zod";import*as n from"ramda";var r=`@express-zod-api/zod-plugin`;const i=(e,n)=>{let r=t.core.$constructor(`$Packer`,(e,n)=>{t.core.$ZodCheck.init(e,n),e._zod.onattach.push(e=>{Object.assign(e._zod.bag,n.bag)}),e._zod.check=()=>{}});return e.check(new r({check:`$Packer`,bag:n}))},a=e=>e._zod.bag,o=`brand`,s=function(e){return i(this,{[o]:e})},c=e=>{let{[o]:t}=a(e)||{};if(typeof t==`symbol`||typeof t==`string`||typeof t==`number`)return t},l=function(e){let r=typeof e==`function`?e:n.renameKeys(n.reject(n.isNil,e)),i=r(n.map(n.invoker(0,`clone`),this._zod.def.shape)),a=this._zod.def.catchall instanceof t.ZodUnknown,o=(a?t.looseObject:t.object)(i);return this.transform(r).pipe(o)},u=function(t){let n=e.get(this)?.examples?.slice()||[];return n.push(t),this.meta({examples:n})},d=function(){return this.meta({deprecated:!0})},f=function(e){return this.meta({default:e})},p=Symbol.for(r);if(!(p in globalThis)){globalThis[p]=!0;for(let e of Object.keys(t)){if(!e.startsWith(`Zod`)||/(Success|Error|Function)$/.test(e))continue;let n=t[e];if(typeof n!=`function`)continue;Object.defineProperties(n.prototype,{example:{value:u,writable:!1},deprecated:{value:d,writable:!1},brand:{set(){},get(){return s.bind(this)}}})}Object.defineProperty(t.ZodDefault.prototype,`label`,{value:f,writable:!1}),Object.defineProperty(t.ZodObject.prototype,`remap`,{value:l,writable:!1})}export{c as getBrand,i as pack,a as unpack};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@express-zod-api/zod-plugin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0-beta.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Zod plugin for express-zod-api",
|
|
6
6
|
"sideEffects": true,
|
|
@@ -51,7 +51,6 @@
|
|
|
51
51
|
"scripts": {
|
|
52
52
|
"pretest": "tsc --noEmit",
|
|
53
53
|
"test": "vitest run",
|
|
54
|
-
"build": "
|
|
55
|
-
"postbuild": "attw --pack --profile esm-only"
|
|
54
|
+
"build": "tsdown"
|
|
56
55
|
}
|
|
57
56
|
}
|