@zodyac/zod-mongoose 1.1.3 → 2.0.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 +12 -0
- package/README.md +139 -31
- package/dist/index.cjs +178 -119
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +58 -10
- package/dist/index.d.ts +58 -10
- package/dist/index.js +164 -117
- package/dist/index.js.map +1 -1
- package/package.json +24 -15
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## Unreleased
|
|
9
|
+
|
|
10
|
+
## 2.0.0 - 2024-08-21
|
|
11
|
+
### Added
|
|
12
|
+
- Changelog
|
package/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
# Zod to
|
|
1
|
+
# Zod to Mongoose schema converter
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
6
7
|
|
|
7
8
|
This package provides a function to convert [zod](https://www.npmjs.com/package/zod) object to [mongoose](https://www.npmjs.com/package/mongoose) schema.
|
|
8
9
|
|
|
@@ -10,51 +11,68 @@ This package provides a function to convert [zod](https://www.npmjs.com/package/
|
|
|
10
11
|
|
|
11
12
|
```bash
|
|
12
13
|
npm i @zodyac/zod-mongoose
|
|
14
|
+
|
|
15
|
+
pnpm add @zodyac/zod-mongoose
|
|
16
|
+
|
|
17
|
+
yarn add @zodyac/zod-mongoose
|
|
18
|
+
|
|
19
|
+
bun add @zodyac/zod-mongoose
|
|
13
20
|
```
|
|
14
21
|
|
|
22
|
+
## Breaking changes
|
|
23
|
+
> [!WARNING]
|
|
24
|
+
> If you were using ```zId``` or ```zUUID``` before, please replace those with ```z.objectId()``` and ```z.mongoUUID()```.
|
|
25
|
+
|
|
26
|
+
- ```zId``` is now ```z.objectId()```
|
|
27
|
+
- ```zUUID``` is now ```z.mongoUUID()```
|
|
28
|
+
|
|
15
29
|
## Usage
|
|
16
30
|
|
|
17
|
-
First, create your zod schema:
|
|
31
|
+
First, extend Zod with ```extendZod```, then create your zod schema:
|
|
18
32
|
|
|
19
33
|
```typescript
|
|
20
|
-
import { z } from
|
|
21
|
-
import {
|
|
34
|
+
import { z } from "zod";
|
|
35
|
+
import { extendZod } from "@zodyac/zod-mongoose";
|
|
36
|
+
|
|
37
|
+
extend(z);
|
|
22
38
|
|
|
23
39
|
const zUser = z.object({
|
|
24
40
|
name: z.string().min(3).max(255),
|
|
25
41
|
age: z.number().min(18).max(100),
|
|
26
42
|
active: z.boolean().default(false),
|
|
27
|
-
access: z.enum([
|
|
28
|
-
companyId:
|
|
29
|
-
wearable:
|
|
43
|
+
access: z.enum(["admin", "user"]).default("user"),
|
|
44
|
+
companyId: z.objectId("Company"),
|
|
45
|
+
wearable: z.mongoUUID(),
|
|
30
46
|
address: z.object({
|
|
31
47
|
street: z.string(),
|
|
32
48
|
city: z.string(),
|
|
33
|
-
state: z.enum([
|
|
49
|
+
state: z.enum(["CA", "NY", "TX"]),
|
|
34
50
|
}),
|
|
35
51
|
tags: z.array(z.string()),
|
|
36
52
|
createdAt: z.date(),
|
|
37
53
|
updatedAt: z.date(),
|
|
38
54
|
});
|
|
39
|
-
|
|
40
55
|
```
|
|
41
56
|
|
|
42
57
|
Then, convert it to mongoose schema and connect model:
|
|
43
58
|
|
|
44
59
|
```typescript
|
|
45
|
-
import { zodSchema } from
|
|
46
|
-
import { model } from
|
|
60
|
+
import { zodSchema } from "@zodyac/zod-mongoose";
|
|
61
|
+
import { model } from "mongoose";
|
|
47
62
|
|
|
48
63
|
const schema = zodSchema(zDoc);
|
|
49
|
-
const userModel = model(
|
|
64
|
+
const userModel = model("User", schema);
|
|
50
65
|
```
|
|
51
66
|
|
|
52
67
|
That's it! Now you can use your mongoose model as usual:
|
|
53
68
|
|
|
54
69
|
```typescript
|
|
55
|
-
userModel.find({ name:
|
|
70
|
+
userModel.find({ name: "John" });
|
|
56
71
|
```
|
|
57
72
|
|
|
73
|
+
> [Note]
|
|
74
|
+
```extendZod``` should be called once for the whole application.
|
|
75
|
+
|
|
58
76
|
## Features
|
|
59
77
|
|
|
60
78
|
- ✅ Basic types
|
|
@@ -62,25 +80,27 @@ userModel.find({ name: 'John' });
|
|
|
62
80
|
- ✅ Arrays
|
|
63
81
|
- ✅ Enums (strings only)
|
|
64
82
|
- ✅ Default values
|
|
83
|
+
- ✅ Maps
|
|
65
84
|
- ✅ Dates
|
|
66
85
|
- ✅ ObjectId
|
|
67
86
|
- ✅ ObjectId references
|
|
68
87
|
- ✅ ZodAny as SchemaTypes.Mixed
|
|
69
|
-
-
|
|
70
|
-
-
|
|
71
|
-
|
|
88
|
+
- ✅ Validation using refinement for String, Number, Date
|
|
89
|
+
- ✅ Unique for String, Number, Date, ObjectId and UUID
|
|
90
|
+
|
|
91
|
+
- ⚠️ Record (Being converted to Map)
|
|
92
|
+
- ⚠️ Unions (not supported by mongoose, will pick first inner type)
|
|
93
|
+
|
|
72
94
|
- ❗️ Intersection (not supported by mongoose)
|
|
95
|
+
- ❗️ Set (not supported by mongoose)
|
|
73
96
|
- ❗️ Indexes (not supported by zod)
|
|
74
|
-
|
|
75
|
-
- ⏳ Regex validation (
|
|
76
|
-
- ⏳
|
|
77
|
-
- ⏳ instanceOf (comming soon)
|
|
78
|
-
- ⏳ Transform (comming soon)
|
|
79
|
-
- ⏳ Refine (comming soon)
|
|
97
|
+
|
|
98
|
+
- ⏳ Regex validation (coming soon)
|
|
99
|
+
- ⏳ instanceOf (coming soon)
|
|
80
100
|
|
|
81
101
|
## Checking schemas
|
|
82
102
|
|
|
83
|
-
To make sure nothing is missing, you can use
|
|
103
|
+
To make sure nothing is missing, you can use `Schema.obj`:
|
|
84
104
|
|
|
85
105
|
```typescript
|
|
86
106
|
// schema is mongoose schema
|
|
@@ -89,17 +109,105 @@ console.log(schema.obj);
|
|
|
89
109
|
|
|
90
110
|
## Raw object
|
|
91
111
|
|
|
92
|
-
If you want to get raw object from zod schema to modify it, you can use
|
|
112
|
+
If you want to get raw object from zod schema to modify it, you can use `zodSchemaRaw` function:
|
|
93
113
|
|
|
94
114
|
```typescript
|
|
95
|
-
import { zodSchemaRaw } from
|
|
96
|
-
import { model, Schema } from
|
|
115
|
+
import { extendZod, zodSchemaRaw } from "@zodyac/zod-mongoose";
|
|
116
|
+
import { model, Schema } from "mongoose";
|
|
117
|
+
|
|
118
|
+
extendZod(z);
|
|
97
119
|
|
|
98
120
|
const schema = zodSchemaRaw(zDoc);
|
|
99
|
-
schema.age.
|
|
121
|
+
schema.age.index = true
|
|
100
122
|
|
|
101
|
-
const model = model(
|
|
123
|
+
const model = model("User", new Schema(schema, {
|
|
124
|
+
timestamps: true,
|
|
125
|
+
}));
|
|
102
126
|
```
|
|
103
127
|
|
|
128
|
+
## ObjectID and UUID
|
|
129
|
+
You can use ```z.objectId(ref?: string)``` and ```z.mongoUUID()``` to describe fields as ObjectID and UUID and add reference to another collection:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { extendZod } from "@zodyac/zod-mongoose";
|
|
133
|
+
import { z } from "zod"
|
|
134
|
+
|
|
135
|
+
extendZod(z);
|
|
136
|
+
|
|
137
|
+
const zUser = z.object({
|
|
138
|
+
someId: z.objectId(),
|
|
139
|
+
companyId: z.objectId("Company"),
|
|
140
|
+
facilityId: z.objectId().ref("Facility"),
|
|
141
|
+
wearable: z.mongoUUID(),
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Validation
|
|
146
|
+
You can use zod refinement to validate your mongoose models:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import { z } from "zod";
|
|
150
|
+
import { extendZod, zodSchema } from "@zodyac/zod-mongoose";
|
|
151
|
+
|
|
152
|
+
extendZod(z);
|
|
153
|
+
|
|
154
|
+
const zUser = z.object({
|
|
155
|
+
phone: z.string().refine((v) => v.match(/^\d{3}-\d{3}-\d{4}$/), "Invalid phone number"),
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Unique fields
|
|
160
|
+
To make a String, Number or Date unique, call ```.unique()```:
|
|
161
|
+
```typescript
|
|
162
|
+
import { z } from "zod";
|
|
163
|
+
import { extendZod, zodSchema } from "@zodyac/zod-mongoose";
|
|
164
|
+
|
|
165
|
+
extendZod(z);
|
|
166
|
+
|
|
167
|
+
const zUser = z.object({
|
|
168
|
+
phone: z.string().unique(),
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
//
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Warnings
|
|
175
|
+
|
|
176
|
+
### ZodUnion types
|
|
177
|
+
|
|
178
|
+
Union types are not supported by mongoose. If you have a union type in your zod schema, it will be converted to it's inner type by default.
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
const zUser = z.object({
|
|
182
|
+
access: z.union([z.string(), z.number()]),
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// Will become
|
|
186
|
+
// {
|
|
187
|
+
// access: {
|
|
188
|
+
// type: String,
|
|
189
|
+
// },
|
|
190
|
+
// }
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### ZodAny
|
|
194
|
+
`ZodAny` is converted to `SchemaTypes.Mixed`. It's not recommended to use it, but it's there if you need it.
|
|
195
|
+
|
|
196
|
+
### ZodRecord
|
|
197
|
+
`ZodRecord` is converted to `Map` type. It's not recommended to use it, but it's there if you need it.
|
|
198
|
+
|
|
199
|
+
## Contributing
|
|
200
|
+
|
|
201
|
+
Feel free to open issues and pull requests! Here's a quick guide to get you started:
|
|
202
|
+
- Fork the repository
|
|
203
|
+
- Install linter and formatter for VSCode: Biome
|
|
204
|
+
- Install dependencies: ```npm i```
|
|
205
|
+
- Make changes
|
|
206
|
+
- Run tests: ```npm test```
|
|
207
|
+
- Run linter: ```npm run lint``` (fix with ```npm run lint:fix```)
|
|
208
|
+
- Commit and push your changes
|
|
209
|
+
- Open a pull request
|
|
210
|
+
|
|
104
211
|
## License
|
|
212
|
+
|
|
105
213
|
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -3,6 +3,7 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
7
|
var __export = (target, all) => {
|
|
7
8
|
for (var name in all)
|
|
8
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -21,132 +22,163 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
22
|
var src_exports = {};
|
|
22
23
|
__export(src_exports, {
|
|
23
24
|
default: () => src_default,
|
|
24
|
-
|
|
25
|
-
zUUID: () => zUUID,
|
|
25
|
+
extendZod: () => extendZod,
|
|
26
26
|
zodSchema: () => zodSchema,
|
|
27
27
|
zodSchemaRaw: () => zodSchemaRaw
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(src_exports);
|
|
30
|
+
var import_mongoose2 = require("mongoose");
|
|
31
|
+
var import_zod2 = require("zod");
|
|
32
|
+
|
|
33
|
+
// src/extension.ts
|
|
30
34
|
var import_mongoose = require("mongoose");
|
|
31
35
|
var import_zod = require("zod");
|
|
32
|
-
function
|
|
36
|
+
function extendZod(z_0) {
|
|
37
|
+
if (z_0.__zm_extended) return;
|
|
38
|
+
z_0.__zm_extended = true;
|
|
39
|
+
const _refine = z_0.ZodType.prototype.refine;
|
|
40
|
+
z_0.ZodType.prototype.refine = function(check, opts) {
|
|
41
|
+
const zEffect = _refine.bind(this)(check, opts);
|
|
42
|
+
let message = void 0;
|
|
43
|
+
if (typeof opts === "string") message = opts;
|
|
44
|
+
else if ("message" in opts) message = opts.message;
|
|
45
|
+
zEffect._def.effect.__zm_validation = {
|
|
46
|
+
validator: check,
|
|
47
|
+
message
|
|
48
|
+
};
|
|
49
|
+
return zEffect;
|
|
50
|
+
};
|
|
51
|
+
z_0.ZodString.prototype.unique = function(arg = true) {
|
|
52
|
+
this.__zm_unique = arg;
|
|
53
|
+
return this;
|
|
54
|
+
};
|
|
55
|
+
z_0.ZodNumber.prototype.unique = function(arg = true) {
|
|
56
|
+
this.__zm_unique = arg;
|
|
57
|
+
return this;
|
|
58
|
+
};
|
|
59
|
+
z_0.ZodDate.prototype.unique = function(arg = true) {
|
|
60
|
+
this.__zm_unique = arg;
|
|
61
|
+
return this;
|
|
62
|
+
};
|
|
63
|
+
z_0.objectId = (ref) => {
|
|
64
|
+
const output = import_zod.z.string().refine((v) => (0, import_mongoose.isValidObjectId)(v), {
|
|
65
|
+
message: "Invalid ObjectId"
|
|
66
|
+
}).or(import_zod.z.instanceof(import_mongoose.Types.ObjectId));
|
|
67
|
+
output.__zm_type = "ObjectId";
|
|
68
|
+
output.__zm_ref = ref;
|
|
69
|
+
output.ref = function(ref2) {
|
|
70
|
+
this.__zm_ref = ref2;
|
|
71
|
+
return this;
|
|
72
|
+
};
|
|
73
|
+
output.unique = function(val = true) {
|
|
74
|
+
this.__zm_unique = val;
|
|
75
|
+
return this;
|
|
76
|
+
};
|
|
77
|
+
return output;
|
|
78
|
+
};
|
|
79
|
+
z_0.mongoUUID = () => {
|
|
80
|
+
const output = import_zod.z.string().uuid({
|
|
81
|
+
message: "Invalid UUID"
|
|
82
|
+
}).or(import_zod.z.instanceof(import_mongoose.Types.UUID).describe("UUID"));
|
|
83
|
+
output.__zm_type = "UUID";
|
|
84
|
+
output.unique = function(val = true) {
|
|
85
|
+
this.__zm_unique = val;
|
|
86
|
+
return this;
|
|
87
|
+
};
|
|
88
|
+
return output;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
__name(extendZod, "extendZod");
|
|
92
|
+
|
|
93
|
+
// src/index.ts
|
|
94
|
+
function zodSchema(schema, options) {
|
|
33
95
|
const definition = parseObject(schema);
|
|
34
|
-
return new
|
|
96
|
+
return new import_mongoose2.Schema(definition, options);
|
|
35
97
|
}
|
|
98
|
+
__name(zodSchema, "zodSchema");
|
|
36
99
|
function zodSchemaRaw(schema) {
|
|
37
100
|
return parseObject(schema);
|
|
38
101
|
}
|
|
39
|
-
|
|
40
|
-
var zUUID = import_zod.z.string().uuid({ message: "Invalid UUID" }).or(import_zod.z.instanceof(import_mongoose.Types.UUID).describe("UUID"));
|
|
102
|
+
__name(zodSchemaRaw, "zodSchemaRaw");
|
|
41
103
|
function parseObject(obj) {
|
|
42
104
|
const object = {};
|
|
43
105
|
for (const [key, field] of Object.entries(obj.shape)) {
|
|
44
|
-
if (field instanceof
|
|
106
|
+
if (field instanceof import_zod2.ZodObject) {
|
|
45
107
|
object[key] = parseObject(field);
|
|
46
108
|
} else {
|
|
47
109
|
const f = parseField(field);
|
|
48
|
-
if (f)
|
|
49
|
-
|
|
50
|
-
else
|
|
51
|
-
console.error(
|
|
52
|
-
`Key ${key}: Unsupported field type: ${field.constructor}`
|
|
53
|
-
);
|
|
110
|
+
if (!f) throw new Error(`Unsupported field type: ${field.constructor}`);
|
|
111
|
+
object[key] = f;
|
|
54
112
|
}
|
|
55
113
|
}
|
|
56
114
|
return object;
|
|
57
115
|
}
|
|
58
|
-
|
|
59
|
-
|
|
116
|
+
__name(parseObject, "parseObject");
|
|
117
|
+
function parseField(field, required = true, def, refinement) {
|
|
118
|
+
const field_type = field.constructor.name;
|
|
119
|
+
if ("__zm_type" in field && field.__zm_type === "ObjectId") {
|
|
120
|
+
const ref = field.__zm_ref;
|
|
121
|
+
const unique = field.__zm_unique;
|
|
122
|
+
return parseObjectId(required, ref, unique);
|
|
123
|
+
}
|
|
124
|
+
if ("__zm_type" in field && field.__zm_type === "UUID") {
|
|
125
|
+
const unique = field.__zm_unique;
|
|
126
|
+
return parseUUID(required, unique);
|
|
127
|
+
}
|
|
128
|
+
if (field instanceof import_zod2.ZodObject) {
|
|
60
129
|
return parseObject(field);
|
|
61
130
|
}
|
|
62
|
-
if (field instanceof
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (field instanceof import_zod.ZodString) {
|
|
72
|
-
return parseString(
|
|
73
|
-
field,
|
|
74
|
-
required,
|
|
75
|
-
def,
|
|
76
|
-
field.description?.toLocaleLowerCase() === "unique"
|
|
77
|
-
// validate as (v: string) => boolean,
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
if (field instanceof import_zod.ZodEnum) {
|
|
131
|
+
if (field instanceof import_zod2.ZodNumber) {
|
|
132
|
+
const isUnique = field.__zm_unique ?? false;
|
|
133
|
+
return parseNumber(field, required, def, isUnique, refinement);
|
|
134
|
+
}
|
|
135
|
+
if (field instanceof import_zod2.ZodString) {
|
|
136
|
+
const isUnique = field.__zm_unique ?? false;
|
|
137
|
+
return parseString(field, required, def, isUnique, refinement);
|
|
138
|
+
}
|
|
139
|
+
if (field instanceof import_zod2.ZodEnum) {
|
|
81
140
|
return parseEnum(Object.keys(field.Values), required, def);
|
|
82
141
|
}
|
|
83
|
-
if (
|
|
142
|
+
if (field_type === "ZodBoolean") {
|
|
84
143
|
return parseBoolean(required, def);
|
|
85
144
|
}
|
|
86
|
-
if (field instanceof
|
|
87
|
-
|
|
145
|
+
if (field instanceof import_zod2.ZodDate) {
|
|
146
|
+
const isUnique = field.__zm_unique ?? false;
|
|
147
|
+
return parseDate(required, def, refinement, isUnique);
|
|
88
148
|
}
|
|
89
|
-
if (field instanceof
|
|
90
|
-
return
|
|
149
|
+
if (field instanceof import_zod2.ZodArray) {
|
|
150
|
+
return parseArray(required, field.element, def);
|
|
91
151
|
}
|
|
92
|
-
if (field instanceof
|
|
93
|
-
return parseField(
|
|
94
|
-
field._def.innerType,
|
|
95
|
-
required,
|
|
96
|
-
field._def.defaultValue()
|
|
97
|
-
);
|
|
152
|
+
if (field instanceof import_zod2.ZodDefault) {
|
|
153
|
+
return parseField(field._def.innerType, required, field._def.defaultValue());
|
|
98
154
|
}
|
|
99
|
-
if (field instanceof
|
|
155
|
+
if (field instanceof import_zod2.ZodOptional) {
|
|
100
156
|
return parseField(field._def.innerType, false, void 0);
|
|
101
157
|
}
|
|
102
|
-
if (field instanceof
|
|
158
|
+
if (field instanceof import_zod2.ZodNullable) {
|
|
103
159
|
return parseField(field._def.innerType, false, def || null);
|
|
104
160
|
}
|
|
105
|
-
if (field.
|
|
106
|
-
const ref = field.description.split(":")[1];
|
|
107
|
-
if (ref)
|
|
108
|
-
return parseObjectIdRef(required, ref);
|
|
109
|
-
return parseObjectId(required);
|
|
110
|
-
}
|
|
111
|
-
if (field.description?.startsWith("UUID")) {
|
|
112
|
-
const ref = field.description.split(":")[1];
|
|
113
|
-
if (ref)
|
|
114
|
-
return parseUUIDRef(required, ref);
|
|
115
|
-
return parseUUID(required);
|
|
116
|
-
}
|
|
117
|
-
if (field instanceof import_zod.ZodUnion) {
|
|
161
|
+
if (field instanceof import_zod2.ZodUnion) {
|
|
118
162
|
return parseField(field._def.options[0]);
|
|
119
163
|
}
|
|
120
|
-
if (
|
|
164
|
+
if (field_type === "ZodAny") {
|
|
121
165
|
return parseMixed(required, def);
|
|
122
166
|
}
|
|
123
|
-
if (field instanceof
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
);
|
|
167
|
+
if (field instanceof import_zod2.ZodMap || field instanceof import_zod2.ZodRecord) {
|
|
168
|
+
return parseMap(required, field.keySchema, def);
|
|
169
|
+
}
|
|
170
|
+
if (field instanceof import_zod2.ZodEffects) {
|
|
171
|
+
const effect = field._def.effect;
|
|
172
|
+
if (effect.type === "refinement") {
|
|
173
|
+
const validation = effect.__zm_validation;
|
|
174
|
+
return parseField(field._def.schema, required, def, validation);
|
|
131
175
|
}
|
|
132
176
|
}
|
|
133
177
|
return null;
|
|
134
178
|
}
|
|
179
|
+
__name(parseField, "parseField");
|
|
135
180
|
function parseNumber(field, required = true, def, unique = false, validate) {
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
type: Number,
|
|
139
|
-
default: def,
|
|
140
|
-
min: field.minValue ?? void 0,
|
|
141
|
-
max: field.maxValue ?? void 0,
|
|
142
|
-
validation: {
|
|
143
|
-
validate
|
|
144
|
-
},
|
|
145
|
-
required,
|
|
146
|
-
unique
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
return {
|
|
181
|
+
const output = {
|
|
150
182
|
type: Number,
|
|
151
183
|
default: def,
|
|
152
184
|
min: field.minValue ?? void 0,
|
|
@@ -154,40 +186,33 @@ function parseNumber(field, required = true, def, unique = false, validate) {
|
|
|
154
186
|
required,
|
|
155
187
|
unique
|
|
156
188
|
};
|
|
189
|
+
if (validate) output.validate = validate;
|
|
190
|
+
return output;
|
|
157
191
|
}
|
|
192
|
+
__name(parseNumber, "parseNumber");
|
|
158
193
|
function parseString(field, required = true, def, unique = false, validate) {
|
|
159
|
-
|
|
160
|
-
return {
|
|
161
|
-
type: String,
|
|
162
|
-
default: def,
|
|
163
|
-
required,
|
|
164
|
-
minLength: field.minLength ?? void 0,
|
|
165
|
-
maxLength: field.maxLength ?? void 0,
|
|
166
|
-
validation: {
|
|
167
|
-
validate
|
|
168
|
-
},
|
|
169
|
-
unique
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
return {
|
|
194
|
+
const output = {
|
|
173
195
|
type: String,
|
|
174
196
|
default: def,
|
|
175
|
-
// TODO: match: field.regex(),
|
|
176
197
|
required,
|
|
177
198
|
minLength: field.minLength ?? void 0,
|
|
178
199
|
maxLength: field.maxLength ?? void 0,
|
|
179
200
|
unique
|
|
180
201
|
};
|
|
202
|
+
if (validate) output.validate = validate;
|
|
203
|
+
return output;
|
|
181
204
|
}
|
|
205
|
+
__name(parseString, "parseString");
|
|
182
206
|
function parseEnum(values, required = true, def) {
|
|
183
207
|
return {
|
|
184
208
|
type: String,
|
|
209
|
+
unique: false,
|
|
185
210
|
default: def,
|
|
186
211
|
enum: values,
|
|
187
|
-
required
|
|
188
|
-
unique: false
|
|
212
|
+
required
|
|
189
213
|
};
|
|
190
214
|
}
|
|
215
|
+
__name(parseEnum, "parseEnum");
|
|
191
216
|
function parseBoolean(required = true, def) {
|
|
192
217
|
return {
|
|
193
218
|
type: Boolean,
|
|
@@ -195,51 +220,85 @@ function parseBoolean(required = true, def) {
|
|
|
195
220
|
required
|
|
196
221
|
};
|
|
197
222
|
}
|
|
198
|
-
|
|
199
|
-
|
|
223
|
+
__name(parseBoolean, "parseBoolean");
|
|
224
|
+
function parseDate(required = true, def, validate, unique = false) {
|
|
225
|
+
const output = {
|
|
200
226
|
type: Date,
|
|
201
227
|
default: def,
|
|
202
|
-
required
|
|
228
|
+
required,
|
|
229
|
+
unique
|
|
203
230
|
};
|
|
231
|
+
if (validate) output.validate = validate;
|
|
232
|
+
return output;
|
|
204
233
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
234
|
+
__name(parseDate, "parseDate");
|
|
235
|
+
function parseObjectId(required = true, ref, unique = false) {
|
|
236
|
+
const output = {
|
|
237
|
+
type: import_mongoose2.SchemaTypes.ObjectId,
|
|
238
|
+
required,
|
|
239
|
+
unique
|
|
209
240
|
};
|
|
241
|
+
if (ref) output.ref = ref;
|
|
242
|
+
return output;
|
|
210
243
|
}
|
|
211
|
-
|
|
244
|
+
__name(parseObjectId, "parseObjectId");
|
|
245
|
+
function parseArray(required = true, element, def) {
|
|
246
|
+
const innerType = parseField(element);
|
|
247
|
+
if (!innerType) throw new Error("Unsupported array type");
|
|
212
248
|
return {
|
|
213
|
-
type:
|
|
214
|
-
|
|
249
|
+
type: [
|
|
250
|
+
innerType
|
|
251
|
+
],
|
|
252
|
+
default: def,
|
|
215
253
|
required
|
|
216
254
|
};
|
|
217
255
|
}
|
|
218
|
-
|
|
256
|
+
__name(parseArray, "parseArray");
|
|
257
|
+
function parseMap(required = true, key, def) {
|
|
258
|
+
const pointer = typeConstructor(key);
|
|
219
259
|
return {
|
|
220
|
-
type:
|
|
260
|
+
type: Map,
|
|
261
|
+
of: pointer,
|
|
262
|
+
default: def,
|
|
221
263
|
required
|
|
222
264
|
};
|
|
223
265
|
}
|
|
224
|
-
|
|
266
|
+
__name(parseMap, "parseMap");
|
|
267
|
+
function typeConstructor(t) {
|
|
268
|
+
switch (true) {
|
|
269
|
+
case t instanceof import_zod2.ZodString:
|
|
270
|
+
return String;
|
|
271
|
+
case t instanceof import_zod2.ZodEnum:
|
|
272
|
+
return String;
|
|
273
|
+
case t instanceof import_zod2.ZodNumber:
|
|
274
|
+
return Number;
|
|
275
|
+
case t instanceof import_zod2.ZodDate:
|
|
276
|
+
return Date;
|
|
277
|
+
default:
|
|
278
|
+
return void 0;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
__name(typeConstructor, "typeConstructor");
|
|
282
|
+
function parseUUID(required = true, unique = false) {
|
|
225
283
|
return {
|
|
226
|
-
type:
|
|
227
|
-
|
|
228
|
-
|
|
284
|
+
type: import_mongoose2.SchemaTypes.UUID,
|
|
285
|
+
required,
|
|
286
|
+
unique
|
|
229
287
|
};
|
|
230
288
|
}
|
|
289
|
+
__name(parseUUID, "parseUUID");
|
|
231
290
|
function parseMixed(required = true, def) {
|
|
232
291
|
return {
|
|
233
|
-
type:
|
|
292
|
+
type: import_mongoose2.SchemaTypes.Mixed,
|
|
234
293
|
default: def,
|
|
235
294
|
required
|
|
236
295
|
};
|
|
237
296
|
}
|
|
297
|
+
__name(parseMixed, "parseMixed");
|
|
238
298
|
var src_default = zodSchema;
|
|
239
299
|
// Annotate the CommonJS export names for ESM import in node:
|
|
240
300
|
0 && (module.exports = {
|
|
241
|
-
|
|
242
|
-
zUUID,
|
|
301
|
+
extendZod,
|
|
243
302
|
zodSchema,
|
|
244
303
|
zodSchemaRaw
|
|
245
304
|
});
|