@express-zod-api/zod-plugin 1.0.0-beta.3
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 +33 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +1 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Anna Bocharova
|
|
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,33 @@
|
|
|
1
|
+
# Zod Plugin from Express Zod API
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
This module extends Zod functionality when it's imported:
|
|
6
|
+
|
|
7
|
+
- Adds `.example()` method to all Zod schemas:
|
|
8
|
+
- shorthand for `.meta({ examples: [...] })`;
|
|
9
|
+
- Adds `.deprecated()` method to all Zod schemas:
|
|
10
|
+
- shorthand for `.meta({ deprecated: true })`;
|
|
11
|
+
- Adds `.label()` method to `ZodDefault`:
|
|
12
|
+
- shorthand for `.meta({ default: ... })`;
|
|
13
|
+
- Adds `.remap()` method to `ZodObject` for renaming object properties:
|
|
14
|
+
- Supports a mapping objects or an object transforming function as an argument;
|
|
15
|
+
- Relies on `R.renameKeys()` from the `ramda` library;
|
|
16
|
+
- Alters the `.brand()` method on all Zod schemas by making the assigned brand available in runtime:
|
|
17
|
+
- The provided `getBrand()` method can retrieve the brand from the schema.
|
|
18
|
+
|
|
19
|
+
## Requirements
|
|
20
|
+
|
|
21
|
+
- Zod `^4.0.0`
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { z } from "zod";
|
|
27
|
+
import { getBrand } from "@express-zod-api/zod-plugin";
|
|
28
|
+
|
|
29
|
+
const schema = z.string().example("test").example("another").brand("custom");
|
|
30
|
+
|
|
31
|
+
getBrand(schema); // "custom"
|
|
32
|
+
schema.meta(); // { examples: ["test", "another"] }
|
|
33
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Mapping utils for Zod Runtime Plugin (remap)
|
|
5
|
+
* @link https://stackoverflow.com/questions/55454125/typescript-remapping-object-properties-in-typesafe
|
|
6
|
+
* @todo try to reuse R.Remap from Ramda (requires to move its types to prod dependencies)
|
|
7
|
+
*/
|
|
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
|
+
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
|
+
declare module "zod" {
|
|
28
|
+
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> {
|
|
29
|
+
/** @desc Alias for .meta({examples}), but argument is typed to ensure the correct placement for transformations */
|
|
30
|
+
example(example: z.output<this>): this;
|
|
31
|
+
deprecated(): this;
|
|
32
|
+
}
|
|
33
|
+
interface ZodDefault<T extends z.core.SomeType = z.core.$ZodType> extends z._ZodType<z.core.$ZodDefaultInternals<T>>, z.core.$ZodDefault<T> {
|
|
34
|
+
/** @desc Change the default value in the generated Documentation to a label, alias for .meta({ default }) */
|
|
35
|
+
label(label: string): this;
|
|
36
|
+
}
|
|
37
|
+
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> {
|
|
38
|
+
remap<V extends string, U extends {
|
|
39
|
+
[P in keyof Shape]?: V;
|
|
40
|
+
}>(mapping: U): z.ZodPipe<z.ZodPipe<this, z.ZodTransform>, // internal type simplified
|
|
41
|
+
z.ZodObject<Remap<Shape, U, V> & Intact<Shape, U>, Config>>;
|
|
42
|
+
remap<U extends z.core.$ZodShape>(mapper: (subject: Shape) => U): z.ZodPipe<z.ZodPipe<this, z.ZodTransform>, z.ZodObject<U>>;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
declare const getBrand: (subject: z.core.$ZodType) => string | number | symbol | undefined;
|
|
47
|
+
|
|
48
|
+
export { getBrand };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as o from"ramda";import{globalRegistry as p,z as n}from"zod";var r="@express-zod-api/zod-plugin";var c=n.core.$constructor("$EZBrandCheck",(e,t)=>{n.core.$ZodCheck.init(e,t),e._zod.onattach.push(s=>s._zod.bag.brand=t.brand),e._zod.check=()=>{}}),u=function(e){let t=p.get(this)?.examples?.slice()||[];return t.push(e),this.meta({examples:t})},l=function(){return this.meta({deprecated:!0})},f=function(e){return this.meta({default:e})},h=function(e){return this.check(new c({brand:e,check:"$EZBrandCheck"}))},m=function(e){let t=typeof e=="function"?e:o.renameKeys(o.reject(o.isNil,e)),s=t(o.map(o.invoker(0,"clone"),this._zod.def.shape)),i=(this._zod.def.catchall instanceof n.ZodUnknown?n.looseObject:n.object)(s);return this.transform(t).pipe(i)},a=Symbol.for(r);if(!(a in globalThis)){globalThis[a]=!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:u,writable:!1},deprecated:{value:l,writable:!1},brand:{set(){},get(){return h.bind(this)}}})}Object.defineProperty(n.ZodDefault.prototype,"label",{value:f,writable:!1}),Object.defineProperty(n.ZodObject.prototype,"remap",{value:m,writable:!1})}var z=e=>{let{brand:t}=e._zod.bag||{};if(typeof t=="symbol"||typeof t=="string"||typeof t=="number")return t};export{z as getBrand};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@express-zod-api/zod-plugin",
|
|
3
|
+
"version": "1.0.0-beta.3",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Zod plugin for express-zod-api",
|
|
6
|
+
"sideEffects": true,
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/RobinTail/express-zod-api.git"
|
|
10
|
+
},
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Anna Bocharova",
|
|
13
|
+
"url": "https://robintail.cz",
|
|
14
|
+
"email": "me@robintail.cz"
|
|
15
|
+
},
|
|
16
|
+
"bugs": "https://github.com/RobinTail/express-zod-api/issues",
|
|
17
|
+
"funding": "https://github.com/sponsors/RobinTail",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "dist/index.js",
|
|
20
|
+
"types": "dist/index.d.ts",
|
|
21
|
+
"module": "dist/index.js",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"*.md"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"ramda": "^0.31.3"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"zod": "^4.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/ramda": "^0.31.0",
|
|
40
|
+
"camelize-ts": "^3.0.0",
|
|
41
|
+
"typescript": "^5.8.3",
|
|
42
|
+
"zod": "^4.0.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"pretest": "tsc --noEmit",
|
|
46
|
+
"test": "vitest run",
|
|
47
|
+
"build": "tsup",
|
|
48
|
+
"postbuild": "attw --pack --profile esm-only"
|
|
49
|
+
}
|
|
50
|
+
}
|