@express-zod-api/zod-plugin 1.0.0 → 1.1.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/CHANGELOG.md +6 -0
- package/README.md +8 -2
- package/dist/index.d.ts +24 -5
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## Version 1
|
|
4
4
|
|
|
5
|
+
### v1.1.0
|
|
6
|
+
|
|
7
|
+
- Added low-level helpers `pack()` and `unpack()` to handle inheritable attributes that withstands refinements:
|
|
8
|
+
- `schema.brand()` is a shorthand for `pack(schema, { brand })`;
|
|
9
|
+
- `getBrand()` is a shorthand for `unpack(schema).brand`.
|
|
10
|
+
|
|
5
11
|
### v1.0.0
|
|
6
12
|
|
|
7
13
|
- First release: almost exact copy of Zod plugin from `express-zod-api`.
|
package/README.md
CHANGED
|
@@ -14,13 +14,13 @@ This module extends Zod functionality when it's imported:
|
|
|
14
14
|
- Supports a mapping object or an object transforming function as an argument;
|
|
15
15
|
- Relies on `R.renameKeys()` from the `ramda` library;
|
|
16
16
|
- Alters the `.brand()` method on all Zod schemas by making the assigned brand available in runtime:
|
|
17
|
-
- The
|
|
17
|
+
- The brand set this way will withstand refinements, unlike the metadata set by `.meta()`.
|
|
18
18
|
|
|
19
19
|
## Requirements
|
|
20
20
|
|
|
21
21
|
- Zod `^4.0.0`
|
|
22
22
|
|
|
23
|
-
##
|
|
23
|
+
## Basic usage
|
|
24
24
|
|
|
25
25
|
```ts
|
|
26
26
|
import { z } from "zod";
|
|
@@ -31,3 +31,9 @@ const schema = z.string().example("test").example("another").brand("custom");
|
|
|
31
31
|
getBrand(schema); // "custom"
|
|
32
32
|
schema.meta(); // { examples: ["test", "another"] }
|
|
33
33
|
```
|
|
34
|
+
|
|
35
|
+
## Helpers
|
|
36
|
+
|
|
37
|
+
- `getBrand()` — retrieves the brand from the schema that was set by its `.brand()` method;
|
|
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.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
|
|
3
|
+
type TuplesFromObject<T> = {
|
|
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;
|
|
3
7
|
/**
|
|
4
8
|
* @fileoverview Mapping utils for Zod Runtime Plugin (remap)
|
|
5
9
|
* @link https://stackoverflow.com/questions/55454125/typescript-remapping-object-properties-in-typesafe
|
|
6
10
|
* @todo try to reuse R.Remap from Ramda (requires to move its types to prod dependencies)
|
|
7
11
|
*/
|
|
8
|
-
type TuplesFromObject<T> = {
|
|
9
|
-
[P in keyof T]: [P, T[P]];
|
|
10
|
-
}[keyof T];
|
|
11
|
-
type GetKeyByValue<T, V> = TuplesFromObject<T> extends infer TT ? TT extends [infer P, V] ? P : never : never;
|
|
12
12
|
type Remap<T, U extends {
|
|
13
13
|
[P in keyof T]?: V;
|
|
14
14
|
}, V extends string> = {
|
|
@@ -52,6 +52,25 @@ declare module "zod" {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* @public
|
|
57
|
+
* @desc Attaches an inheritable attributes to the schema (withstands refinements).
|
|
58
|
+
*/
|
|
59
|
+
declare const pack: <T extends z.ZodType, B extends object>(subject: T, bag: B) => T & {
|
|
60
|
+
_zod: {
|
|
61
|
+
bag: T["_zod"]["bag"] & B;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* @public
|
|
66
|
+
* @desc Retrieves the attributes attached to the schema by pack() method.
|
|
67
|
+
*/
|
|
68
|
+
declare const unpack: <T extends z.core.$ZodType>(subject: T) => T["_zod"]["bag"];
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @public
|
|
72
|
+
* @desc Retrieves the brand from the schema set by its .brand() method.
|
|
73
|
+
* */
|
|
55
74
|
declare const getBrand: (subject: z.core.$ZodType) => string | number | symbol | undefined;
|
|
56
75
|
|
|
57
|
-
export { getBrand };
|
|
76
|
+
export { getBrand, pack, unpack };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import{globalRegistry as k,z as n}from"zod";var d="@express-zod-api/zod-plugin";import{z as f}from"zod";var a=(e,t)=>{let i=f.core.$constructor("$Packer",(s,r)=>{f.core.$ZodCheck.init(s,r),s._zod.onattach.push(b=>{Object.assign(b._zod.bag,r.bag)}),s._zod.check=()=>{}});return e.check(new i({check:"$Packer",bag:t}))},p=e=>e._zod.bag;var l="brand",u=function(e){return a(this,{[l]:e})},T=e=>{let{[l]:t}=p(e)||{};if(typeof t=="symbol"||typeof t=="string"||typeof t=="number")return t};import{z as c}from"zod";import*as o from"ramda";var y=function(e){let t=typeof e=="function"?e:o.renameKeys(o.reject(o.isNil,e)),i=t(o.map(o.invoker(0,"clone"),this._zod.def.shape)),r=(this._zod.def.catchall instanceof c.ZodUnknown?c.looseObject:c.object)(i);return this.transform(t).pipe(r)};var z=function(e){let t=k.get(this)?.examples?.slice()||[];return t.push(e),this.meta({examples:t})},g=function(){return this.meta({deprecated:!0})},x=function(e){return this.meta({default:e})},m=Symbol.for(d);if(!(m in globalThis)){globalThis[m]=!0;for(let e of Object.keys(n)){if(!e.startsWith("Zod")||/(Success|Error|Function)$/.test(e))continue;let t=n[e];typeof t=="function"&&Object.defineProperties(t.prototype,{example:{value:z,writable:!1},deprecated:{value:g,writable:!1},brand:{set(){},get(){return u.bind(this)}}})}Object.defineProperty(n.ZodDefault.prototype,"label",{value:x,writable:!1}),Object.defineProperty(n.ZodObject.prototype,"remap",{value:y,writable:!1})}export{T as getBrand,a as pack,p as unpack};
|