@zodyac/zod-mongoose 1.1.3 → 1.3.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 +179 -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 +165 -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
|
+
## 1.3.0 - 2024-08-30
|
|
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,164 @@ 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
|
-
|
|
36
|
+
var zod_extended = false;
|
|
37
|
+
function extendZod(z_0) {
|
|
38
|
+
if (zod_extended) return;
|
|
39
|
+
zod_extended = true;
|
|
40
|
+
const _refine = z_0.ZodType.prototype.refine;
|
|
41
|
+
z_0.ZodType.prototype.refine = function(check, opts) {
|
|
42
|
+
const zEffect = _refine.bind(this)(check, opts);
|
|
43
|
+
let message = void 0;
|
|
44
|
+
if (typeof opts === "string") message = opts;
|
|
45
|
+
else if ("message" in opts) message = opts.message;
|
|
46
|
+
zEffect._def.effect.__zm_validation = {
|
|
47
|
+
validator: check,
|
|
48
|
+
message
|
|
49
|
+
};
|
|
50
|
+
return zEffect;
|
|
51
|
+
};
|
|
52
|
+
z_0.ZodString.prototype.unique = function(arg = true) {
|
|
53
|
+
this.__zm_unique = arg;
|
|
54
|
+
return this;
|
|
55
|
+
};
|
|
56
|
+
z_0.ZodNumber.prototype.unique = function(arg = true) {
|
|
57
|
+
this.__zm_unique = arg;
|
|
58
|
+
return this;
|
|
59
|
+
};
|
|
60
|
+
z_0.ZodDate.prototype.unique = function(arg = true) {
|
|
61
|
+
this.__zm_unique = arg;
|
|
62
|
+
return this;
|
|
63
|
+
};
|
|
64
|
+
z_0.objectId = (ref) => {
|
|
65
|
+
const output = import_zod.z.string().refine((v) => (0, import_mongoose.isValidObjectId)(v), {
|
|
66
|
+
message: "Invalid ObjectId"
|
|
67
|
+
}).or(import_zod.z.instanceof(import_mongoose.Types.ObjectId));
|
|
68
|
+
output.__zm_type = "ObjectId";
|
|
69
|
+
output.__zm_ref = ref;
|
|
70
|
+
output.ref = function(ref2) {
|
|
71
|
+
this.__zm_ref = ref2;
|
|
72
|
+
return this;
|
|
73
|
+
};
|
|
74
|
+
output.unique = function(val = true) {
|
|
75
|
+
this.__zm_unique = val;
|
|
76
|
+
return this;
|
|
77
|
+
};
|
|
78
|
+
return output;
|
|
79
|
+
};
|
|
80
|
+
z_0.mongoUUID = () => {
|
|
81
|
+
const output = import_zod.z.string().uuid({
|
|
82
|
+
message: "Invalid UUID"
|
|
83
|
+
}).or(import_zod.z.instanceof(import_mongoose.Types.UUID).describe("UUID"));
|
|
84
|
+
output.__zm_type = "UUID";
|
|
85
|
+
output.unique = function(val = true) {
|
|
86
|
+
this.__zm_unique = val;
|
|
87
|
+
return this;
|
|
88
|
+
};
|
|
89
|
+
return output;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
__name(extendZod, "extendZod");
|
|
93
|
+
|
|
94
|
+
// src/index.ts
|
|
95
|
+
function zodSchema(schema, options) {
|
|
33
96
|
const definition = parseObject(schema);
|
|
34
|
-
return new
|
|
97
|
+
return new import_mongoose2.Schema(definition, options);
|
|
35
98
|
}
|
|
99
|
+
__name(zodSchema, "zodSchema");
|
|
36
100
|
function zodSchemaRaw(schema) {
|
|
37
101
|
return parseObject(schema);
|
|
38
102
|
}
|
|
39
|
-
|
|
40
|
-
var zUUID = import_zod.z.string().uuid({ message: "Invalid UUID" }).or(import_zod.z.instanceof(import_mongoose.Types.UUID).describe("UUID"));
|
|
103
|
+
__name(zodSchemaRaw, "zodSchemaRaw");
|
|
41
104
|
function parseObject(obj) {
|
|
42
105
|
const object = {};
|
|
43
106
|
for (const [key, field] of Object.entries(obj.shape)) {
|
|
44
|
-
if (field instanceof
|
|
107
|
+
if (field instanceof import_zod2.ZodObject) {
|
|
45
108
|
object[key] = parseObject(field);
|
|
46
109
|
} else {
|
|
47
110
|
const f = parseField(field);
|
|
48
|
-
if (f)
|
|
49
|
-
|
|
50
|
-
else
|
|
51
|
-
console.error(
|
|
52
|
-
`Key ${key}: Unsupported field type: ${field.constructor}`
|
|
53
|
-
);
|
|
111
|
+
if (!f) throw new Error(`Unsupported field type: ${field.constructor}`);
|
|
112
|
+
object[key] = f;
|
|
54
113
|
}
|
|
55
114
|
}
|
|
56
115
|
return object;
|
|
57
116
|
}
|
|
58
|
-
|
|
59
|
-
|
|
117
|
+
__name(parseObject, "parseObject");
|
|
118
|
+
function parseField(field, required = true, def, refinement) {
|
|
119
|
+
const field_type = field.constructor.name;
|
|
120
|
+
if ("__zm_type" in field && field.__zm_type === "ObjectId") {
|
|
121
|
+
const ref = field.__zm_ref;
|
|
122
|
+
const unique = field.__zm_unique;
|
|
123
|
+
return parseObjectId(required, ref, unique);
|
|
124
|
+
}
|
|
125
|
+
if ("__zm_type" in field && field.__zm_type === "UUID") {
|
|
126
|
+
const unique = field.__zm_unique;
|
|
127
|
+
return parseUUID(required, unique);
|
|
128
|
+
}
|
|
129
|
+
if (field instanceof import_zod2.ZodObject) {
|
|
60
130
|
return parseObject(field);
|
|
61
131
|
}
|
|
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) {
|
|
132
|
+
if (field instanceof import_zod2.ZodNumber) {
|
|
133
|
+
const isUnique = field.__zm_unique ?? false;
|
|
134
|
+
return parseNumber(field, required, def, isUnique, refinement);
|
|
135
|
+
}
|
|
136
|
+
if (field instanceof import_zod2.ZodString) {
|
|
137
|
+
const isUnique = field.__zm_unique ?? false;
|
|
138
|
+
return parseString(field, required, def, isUnique, refinement);
|
|
139
|
+
}
|
|
140
|
+
if (field instanceof import_zod2.ZodEnum) {
|
|
81
141
|
return parseEnum(Object.keys(field.Values), required, def);
|
|
82
142
|
}
|
|
83
|
-
if (
|
|
143
|
+
if (field_type === "ZodBoolean") {
|
|
84
144
|
return parseBoolean(required, def);
|
|
85
145
|
}
|
|
86
|
-
if (field instanceof
|
|
87
|
-
|
|
146
|
+
if (field instanceof import_zod2.ZodDate) {
|
|
147
|
+
const isUnique = field.__zm_unique ?? false;
|
|
148
|
+
return parseDate(required, def, refinement, isUnique);
|
|
88
149
|
}
|
|
89
|
-
if (field instanceof
|
|
90
|
-
return
|
|
150
|
+
if (field instanceof import_zod2.ZodArray) {
|
|
151
|
+
return parseArray(required, field.element, def);
|
|
91
152
|
}
|
|
92
|
-
if (field instanceof
|
|
93
|
-
return parseField(
|
|
94
|
-
field._def.innerType,
|
|
95
|
-
required,
|
|
96
|
-
field._def.defaultValue()
|
|
97
|
-
);
|
|
153
|
+
if (field instanceof import_zod2.ZodDefault) {
|
|
154
|
+
return parseField(field._def.innerType, required, field._def.defaultValue());
|
|
98
155
|
}
|
|
99
|
-
if (field instanceof
|
|
156
|
+
if (field instanceof import_zod2.ZodOptional) {
|
|
100
157
|
return parseField(field._def.innerType, false, void 0);
|
|
101
158
|
}
|
|
102
|
-
if (field instanceof
|
|
159
|
+
if (field instanceof import_zod2.ZodNullable) {
|
|
103
160
|
return parseField(field._def.innerType, false, def || null);
|
|
104
161
|
}
|
|
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) {
|
|
162
|
+
if (field instanceof import_zod2.ZodUnion) {
|
|
118
163
|
return parseField(field._def.options[0]);
|
|
119
164
|
}
|
|
120
|
-
if (
|
|
165
|
+
if (field_type === "ZodAny") {
|
|
121
166
|
return parseMixed(required, def);
|
|
122
167
|
}
|
|
123
|
-
if (field instanceof
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
);
|
|
168
|
+
if (field instanceof import_zod2.ZodMap || field instanceof import_zod2.ZodRecord) {
|
|
169
|
+
return parseMap(required, field.keySchema, def);
|
|
170
|
+
}
|
|
171
|
+
if (field instanceof import_zod2.ZodEffects) {
|
|
172
|
+
const effect = field._def.effect;
|
|
173
|
+
if (effect.type === "refinement") {
|
|
174
|
+
const validation = effect.__zm_validation;
|
|
175
|
+
return parseField(field._def.schema, required, def, validation);
|
|
131
176
|
}
|
|
132
177
|
}
|
|
133
178
|
return null;
|
|
134
179
|
}
|
|
180
|
+
__name(parseField, "parseField");
|
|
135
181
|
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 {
|
|
182
|
+
const output = {
|
|
150
183
|
type: Number,
|
|
151
184
|
default: def,
|
|
152
185
|
min: field.minValue ?? void 0,
|
|
@@ -154,40 +187,33 @@ function parseNumber(field, required = true, def, unique = false, validate) {
|
|
|
154
187
|
required,
|
|
155
188
|
unique
|
|
156
189
|
};
|
|
190
|
+
if (validate) output.validate = validate;
|
|
191
|
+
return output;
|
|
157
192
|
}
|
|
193
|
+
__name(parseNumber, "parseNumber");
|
|
158
194
|
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 {
|
|
195
|
+
const output = {
|
|
173
196
|
type: String,
|
|
174
197
|
default: def,
|
|
175
|
-
// TODO: match: field.regex(),
|
|
176
198
|
required,
|
|
177
199
|
minLength: field.minLength ?? void 0,
|
|
178
200
|
maxLength: field.maxLength ?? void 0,
|
|
179
201
|
unique
|
|
180
202
|
};
|
|
203
|
+
if (validate) output.validate = validate;
|
|
204
|
+
return output;
|
|
181
205
|
}
|
|
206
|
+
__name(parseString, "parseString");
|
|
182
207
|
function parseEnum(values, required = true, def) {
|
|
183
208
|
return {
|
|
184
209
|
type: String,
|
|
210
|
+
unique: false,
|
|
185
211
|
default: def,
|
|
186
212
|
enum: values,
|
|
187
|
-
required
|
|
188
|
-
unique: false
|
|
213
|
+
required
|
|
189
214
|
};
|
|
190
215
|
}
|
|
216
|
+
__name(parseEnum, "parseEnum");
|
|
191
217
|
function parseBoolean(required = true, def) {
|
|
192
218
|
return {
|
|
193
219
|
type: Boolean,
|
|
@@ -195,51 +221,85 @@ function parseBoolean(required = true, def) {
|
|
|
195
221
|
required
|
|
196
222
|
};
|
|
197
223
|
}
|
|
198
|
-
|
|
199
|
-
|
|
224
|
+
__name(parseBoolean, "parseBoolean");
|
|
225
|
+
function parseDate(required = true, def, validate, unique = false) {
|
|
226
|
+
const output = {
|
|
200
227
|
type: Date,
|
|
201
228
|
default: def,
|
|
202
|
-
required
|
|
229
|
+
required,
|
|
230
|
+
unique
|
|
203
231
|
};
|
|
232
|
+
if (validate) output.validate = validate;
|
|
233
|
+
return output;
|
|
204
234
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
235
|
+
__name(parseDate, "parseDate");
|
|
236
|
+
function parseObjectId(required = true, ref, unique = false) {
|
|
237
|
+
const output = {
|
|
238
|
+
type: import_mongoose2.SchemaTypes.ObjectId,
|
|
239
|
+
required,
|
|
240
|
+
unique
|
|
209
241
|
};
|
|
242
|
+
if (ref) output.ref = ref;
|
|
243
|
+
return output;
|
|
210
244
|
}
|
|
211
|
-
|
|
245
|
+
__name(parseObjectId, "parseObjectId");
|
|
246
|
+
function parseArray(required = true, element, def) {
|
|
247
|
+
const innerType = parseField(element);
|
|
248
|
+
if (!innerType) throw new Error("Unsupported array type");
|
|
212
249
|
return {
|
|
213
|
-
type:
|
|
214
|
-
|
|
250
|
+
type: [
|
|
251
|
+
innerType
|
|
252
|
+
],
|
|
253
|
+
default: def,
|
|
215
254
|
required
|
|
216
255
|
};
|
|
217
256
|
}
|
|
218
|
-
|
|
257
|
+
__name(parseArray, "parseArray");
|
|
258
|
+
function parseMap(required = true, key, def) {
|
|
259
|
+
const pointer = typeConstructor(key);
|
|
219
260
|
return {
|
|
220
|
-
type:
|
|
261
|
+
type: Map,
|
|
262
|
+
of: pointer,
|
|
263
|
+
default: def,
|
|
221
264
|
required
|
|
222
265
|
};
|
|
223
266
|
}
|
|
224
|
-
|
|
267
|
+
__name(parseMap, "parseMap");
|
|
268
|
+
function typeConstructor(t) {
|
|
269
|
+
switch (true) {
|
|
270
|
+
case t instanceof import_zod2.ZodString:
|
|
271
|
+
return String;
|
|
272
|
+
case t instanceof import_zod2.ZodEnum:
|
|
273
|
+
return String;
|
|
274
|
+
case t instanceof import_zod2.ZodNumber:
|
|
275
|
+
return Number;
|
|
276
|
+
case t instanceof import_zod2.ZodDate:
|
|
277
|
+
return Date;
|
|
278
|
+
default:
|
|
279
|
+
return void 0;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
__name(typeConstructor, "typeConstructor");
|
|
283
|
+
function parseUUID(required = true, unique = false) {
|
|
225
284
|
return {
|
|
226
|
-
type:
|
|
227
|
-
|
|
228
|
-
|
|
285
|
+
type: import_mongoose2.SchemaTypes.UUID,
|
|
286
|
+
required,
|
|
287
|
+
unique
|
|
229
288
|
};
|
|
230
289
|
}
|
|
290
|
+
__name(parseUUID, "parseUUID");
|
|
231
291
|
function parseMixed(required = true, def) {
|
|
232
292
|
return {
|
|
233
|
-
type:
|
|
293
|
+
type: import_mongoose2.SchemaTypes.Mixed,
|
|
234
294
|
default: def,
|
|
235
295
|
required
|
|
236
296
|
};
|
|
237
297
|
}
|
|
298
|
+
__name(parseMixed, "parseMixed");
|
|
238
299
|
var src_default = zodSchema;
|
|
239
300
|
// Annotate the CommonJS export names for ESM import in node:
|
|
240
301
|
0 && (module.exports = {
|
|
241
|
-
|
|
242
|
-
zUUID,
|
|
302
|
+
extendZod,
|
|
243
303
|
zodSchema,
|
|
244
304
|
zodSchemaRaw
|
|
245
305
|
});
|