@zodyac/zod-mongoose 1.1.2 → 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 +185 -115
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +60 -10
- package/dist/index.d.ts +60 -10
- package/dist/index.js +172 -113
- package/dist/index.js.map +1 -1
- package/package.json +25 -16
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,163 +22,198 @@ 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
|
-
if (field instanceof
|
|
71
|
-
return parseString(
|
|
72
|
-
field,
|
|
73
|
-
required,
|
|
74
|
-
def
|
|
75
|
-
// validate as (v: string) => boolean,
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
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) {
|
|
79
141
|
return parseEnum(Object.keys(field.Values), required, def);
|
|
80
142
|
}
|
|
81
|
-
if (
|
|
143
|
+
if (field_type === "ZodBoolean") {
|
|
82
144
|
return parseBoolean(required, def);
|
|
83
145
|
}
|
|
84
|
-
if (field instanceof
|
|
85
|
-
|
|
146
|
+
if (field instanceof import_zod2.ZodDate) {
|
|
147
|
+
const isUnique = field.__zm_unique ?? false;
|
|
148
|
+
return parseDate(required, def, refinement, isUnique);
|
|
86
149
|
}
|
|
87
|
-
if (field instanceof
|
|
88
|
-
return
|
|
150
|
+
if (field instanceof import_zod2.ZodArray) {
|
|
151
|
+
return parseArray(required, field.element, def);
|
|
89
152
|
}
|
|
90
|
-
if (field instanceof
|
|
91
|
-
return parseField(
|
|
92
|
-
field._def.innerType,
|
|
93
|
-
required,
|
|
94
|
-
field._def.defaultValue()
|
|
95
|
-
);
|
|
153
|
+
if (field instanceof import_zod2.ZodDefault) {
|
|
154
|
+
return parseField(field._def.innerType, required, field._def.defaultValue());
|
|
96
155
|
}
|
|
97
|
-
if (field instanceof
|
|
156
|
+
if (field instanceof import_zod2.ZodOptional) {
|
|
98
157
|
return parseField(field._def.innerType, false, void 0);
|
|
99
158
|
}
|
|
100
|
-
if (field.
|
|
101
|
-
|
|
102
|
-
if (ref)
|
|
103
|
-
return parseObjectIdRef(required, ref);
|
|
104
|
-
return parseObjectId(required);
|
|
159
|
+
if (field instanceof import_zod2.ZodNullable) {
|
|
160
|
+
return parseField(field._def.innerType, false, def || null);
|
|
105
161
|
}
|
|
106
|
-
if (field.
|
|
107
|
-
const ref = field.description.split(":")[1];
|
|
108
|
-
if (ref)
|
|
109
|
-
return parseUUIDRef(required, ref);
|
|
110
|
-
return parseUUID(required);
|
|
111
|
-
}
|
|
112
|
-
if (field instanceof import_zod.ZodUnion) {
|
|
162
|
+
if (field instanceof import_zod2.ZodUnion) {
|
|
113
163
|
return parseField(field._def.options[0]);
|
|
114
164
|
}
|
|
115
|
-
if (
|
|
165
|
+
if (field_type === "ZodAny") {
|
|
116
166
|
return parseMixed(required, def);
|
|
117
167
|
}
|
|
118
|
-
if (field instanceof
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
);
|
|
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);
|
|
126
176
|
}
|
|
127
177
|
}
|
|
128
178
|
return null;
|
|
129
179
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
type: Number,
|
|
134
|
-
default: def,
|
|
135
|
-
min: field.minValue ?? void 0,
|
|
136
|
-
max: field.maxValue ?? void 0,
|
|
137
|
-
validation: {
|
|
138
|
-
validate
|
|
139
|
-
},
|
|
140
|
-
required
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
return {
|
|
180
|
+
__name(parseField, "parseField");
|
|
181
|
+
function parseNumber(field, required = true, def, unique = false, validate) {
|
|
182
|
+
const output = {
|
|
144
183
|
type: Number,
|
|
145
184
|
default: def,
|
|
146
185
|
min: field.minValue ?? void 0,
|
|
147
186
|
max: field.maxValue ?? void 0,
|
|
148
|
-
required
|
|
187
|
+
required,
|
|
188
|
+
unique
|
|
149
189
|
};
|
|
190
|
+
if (validate) output.validate = validate;
|
|
191
|
+
return output;
|
|
150
192
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
type: String,
|
|
155
|
-
default: def,
|
|
156
|
-
required,
|
|
157
|
-
minLength: field.minLength ?? void 0,
|
|
158
|
-
maxLength: field.maxLength ?? void 0,
|
|
159
|
-
validation: {
|
|
160
|
-
validate
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
return {
|
|
193
|
+
__name(parseNumber, "parseNumber");
|
|
194
|
+
function parseString(field, required = true, def, unique = false, validate) {
|
|
195
|
+
const output = {
|
|
165
196
|
type: String,
|
|
166
197
|
default: def,
|
|
167
|
-
// TODO: match: field.regex(),
|
|
168
198
|
required,
|
|
169
199
|
minLength: field.minLength ?? void 0,
|
|
170
|
-
maxLength: field.maxLength ?? void 0
|
|
200
|
+
maxLength: field.maxLength ?? void 0,
|
|
201
|
+
unique
|
|
171
202
|
};
|
|
203
|
+
if (validate) output.validate = validate;
|
|
204
|
+
return output;
|
|
172
205
|
}
|
|
206
|
+
__name(parseString, "parseString");
|
|
173
207
|
function parseEnum(values, required = true, def) {
|
|
174
208
|
return {
|
|
175
209
|
type: String,
|
|
210
|
+
unique: false,
|
|
176
211
|
default: def,
|
|
177
212
|
enum: values,
|
|
178
213
|
required
|
|
179
214
|
};
|
|
180
215
|
}
|
|
216
|
+
__name(parseEnum, "parseEnum");
|
|
181
217
|
function parseBoolean(required = true, def) {
|
|
182
218
|
return {
|
|
183
219
|
type: Boolean,
|
|
@@ -185,51 +221,85 @@ function parseBoolean(required = true, def) {
|
|
|
185
221
|
required
|
|
186
222
|
};
|
|
187
223
|
}
|
|
188
|
-
|
|
189
|
-
|
|
224
|
+
__name(parseBoolean, "parseBoolean");
|
|
225
|
+
function parseDate(required = true, def, validate, unique = false) {
|
|
226
|
+
const output = {
|
|
190
227
|
type: Date,
|
|
191
228
|
default: def,
|
|
192
|
-
required
|
|
229
|
+
required,
|
|
230
|
+
unique
|
|
193
231
|
};
|
|
232
|
+
if (validate) output.validate = validate;
|
|
233
|
+
return output;
|
|
194
234
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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
|
|
199
241
|
};
|
|
242
|
+
if (ref) output.ref = ref;
|
|
243
|
+
return output;
|
|
200
244
|
}
|
|
201
|
-
|
|
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");
|
|
202
249
|
return {
|
|
203
|
-
type:
|
|
204
|
-
|
|
250
|
+
type: [
|
|
251
|
+
innerType
|
|
252
|
+
],
|
|
253
|
+
default: def,
|
|
205
254
|
required
|
|
206
255
|
};
|
|
207
256
|
}
|
|
208
|
-
|
|
257
|
+
__name(parseArray, "parseArray");
|
|
258
|
+
function parseMap(required = true, key, def) {
|
|
259
|
+
const pointer = typeConstructor(key);
|
|
209
260
|
return {
|
|
210
|
-
type:
|
|
261
|
+
type: Map,
|
|
262
|
+
of: pointer,
|
|
263
|
+
default: def,
|
|
211
264
|
required
|
|
212
265
|
};
|
|
213
266
|
}
|
|
214
|
-
|
|
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) {
|
|
215
284
|
return {
|
|
216
|
-
type:
|
|
217
|
-
|
|
218
|
-
|
|
285
|
+
type: import_mongoose2.SchemaTypes.UUID,
|
|
286
|
+
required,
|
|
287
|
+
unique
|
|
219
288
|
};
|
|
220
289
|
}
|
|
290
|
+
__name(parseUUID, "parseUUID");
|
|
221
291
|
function parseMixed(required = true, def) {
|
|
222
292
|
return {
|
|
223
|
-
type:
|
|
293
|
+
type: import_mongoose2.SchemaTypes.Mixed,
|
|
224
294
|
default: def,
|
|
225
295
|
required
|
|
226
296
|
};
|
|
227
297
|
}
|
|
298
|
+
__name(parseMixed, "parseMixed");
|
|
228
299
|
var src_default = zodSchema;
|
|
229
300
|
// Annotate the CommonJS export names for ESM import in node:
|
|
230
301
|
0 && (module.exports = {
|
|
231
|
-
|
|
232
|
-
zUUID,
|
|
302
|
+
extendZod,
|
|
233
303
|
zodSchema,
|
|
234
304
|
zodSchemaRaw
|
|
235
305
|
});
|