@mirite/zod-to-mongoose 0.3.1 → 0.4.1

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.
@@ -1,44 +1,72 @@
1
- import type { EnumLike } from 'zod';
2
- import * as Mongoose from 'mongoose';
3
- import { z } from 'zod';
4
- import type { ZodArray } from 'zod';
5
- import type { ZodBoolean } from 'zod';
6
- import type { ZodDate } from 'zod';
7
- import type { ZodDefault } from 'zod';
8
- import type { ZodEnum } from 'zod';
9
- import type { ZodNativeEnum } from 'zod';
10
- import type { ZodNullable } from 'zod';
11
- import type { ZodNumber } from 'zod';
1
+ import type { Connection } from 'mongoose';
2
+ import type { Model } from 'mongoose';
3
+ import { Options } from 'tsup';
4
+ import { Schema } from 'mongoose';
5
+ import type { z } from 'zod';
6
+ import { ZodArray } from 'zod';
7
+ import { ZodDefault } from 'zod';
8
+ import { ZodEnum } from 'zod';
9
+ import { ZodNullable } from 'zod';
12
10
  import { ZodObject } from 'zod';
13
- import type { ZodOptional } from 'zod';
14
- import { ZodRawShape } from 'zod';
15
- import type { ZodString } from 'zod';
16
- import type { ZodTypeAny } from 'zod';
17
- import type { ZodUnion } from 'zod';
11
+ import { ZodOptional } from 'zod';
12
+ import type { ZodRawShape } from 'zod';
13
+ import type { ZodType } from 'zod';
18
14
 
19
- export declare type Check = "";
20
-
21
- export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>, modelName: string, connection: Mongoose.Connection): {
22
- model: Mongoose.Model<z.infer<typeof zodObject>>;
23
- schema: Mongoose.Schema;
15
+ export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>, modelName: string, connection: Connection): {
16
+ model: Model<z.infer<typeof zodObject>>;
17
+ schema: Schema;
24
18
  };
25
19
 
26
- export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Mongoose.Schema;
20
+ export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Schema;
27
21
 
28
- export declare type Field = {
29
- _def: FieldDefinition;
30
- checks?: Array<Check>;
31
- defaultValue?: () => unknown;
32
- };
22
+ export declare const default_alias: Options | Options[] | ((overrideOptions: Options) => Options | Options[] | Promise<Options | Options[]>);
33
23
 
34
- export declare type FieldDefinition = {
35
- description: string | undefined;
36
- innerType?: Field;
37
- typeName: string;
38
- };
24
+ /**
25
+ * Check if a Zod definition is an array
26
+ *
27
+ * @param definition The Zod definition to check
28
+ * @returns Whether the definition is an array
29
+ */
30
+ export declare function isZodArray(definition: ZodType): definition is ZodArray<ZodType>;
31
+
32
+ /**
33
+ * Type guard for whether the definition is a default.
34
+ *
35
+ * @param definition The definition to check.
36
+ * @returns True if the definition is a default.
37
+ */
38
+ export declare function isZodDefault(definition: ZodType): definition is ZodDefault;
39
+
40
+ /**
41
+ * Check if a Zod definition is an enum
42
+ *
43
+ * @param definition The Zod definition to check
44
+ * @returns Whether the definition is an enum
45
+ */
46
+ export declare function isZodEnum(definition: ZodType): definition is ZodEnum;
47
+
48
+ /**
49
+ * Type guard for whether the definition is nullable.
50
+ *
51
+ * @param definition The definition to check.
52
+ * @returns True if the definition is nullable.
53
+ */
54
+ export declare function isZodNullable(definition: ZodType): definition is ZodNullable;
39
55
 
40
- declare type SupportedPrimitive = ZodBoolean | ZodDate | ZodNumber | ZodString;
56
+ /**
57
+ * Check if a Zod definition is an object
58
+ *
59
+ * @param definition The Zod definition to check
60
+ * @returns Whether the definition is an object
61
+ */
62
+ export declare function isZodObject(definition: ZodType): definition is ZodObject<ZodRawShape>;
41
63
 
42
- export declare type SupportedType = SupportedPrimitive | ZodArray<ZodTypeAny> | ZodDefault<ZodTypeAny> | ZodEnum<[string, ...string[]]> | ZodNativeEnum<EnumLike> | ZodNullable<ZodTypeAny> | ZodObject<ZodRawShape> | ZodOptional<ZodTypeAny> | ZodUnion<readonly [ZodTypeAny, ...ZodTypeAny[]]>;
64
+ /**
65
+ * Type guard for whether the definition is optional.
66
+ *
67
+ * @param definition The definition to check.
68
+ * @returns True if the definition is optional.
69
+ */
70
+ export declare function isZodOptional(definition: ZodType): definition is ZodOptional;
43
71
 
44
72
  export { }
@@ -1,44 +1,72 @@
1
- import type { EnumLike } from 'zod';
2
- import * as Mongoose from 'mongoose';
3
- import { z } from 'zod';
4
- import type { ZodArray } from 'zod';
5
- import type { ZodBoolean } from 'zod';
6
- import type { ZodDate } from 'zod';
7
- import type { ZodDefault } from 'zod';
8
- import type { ZodEnum } from 'zod';
9
- import type { ZodNativeEnum } from 'zod';
10
- import type { ZodNullable } from 'zod';
11
- import type { ZodNumber } from 'zod';
1
+ import type { Connection } from 'mongoose';
2
+ import type { Model } from 'mongoose';
3
+ import { Options } from 'tsup';
4
+ import { Schema } from 'mongoose';
5
+ import type { z } from 'zod';
6
+ import { ZodArray } from 'zod';
7
+ import { ZodDefault } from 'zod';
8
+ import { ZodEnum } from 'zod';
9
+ import { ZodNullable } from 'zod';
12
10
  import { ZodObject } from 'zod';
13
- import type { ZodOptional } from 'zod';
14
- import { ZodRawShape } from 'zod';
15
- import type { ZodString } from 'zod';
16
- import type { ZodTypeAny } from 'zod';
17
- import type { ZodUnion } from 'zod';
11
+ import { ZodOptional } from 'zod';
12
+ import type { ZodRawShape } from 'zod';
13
+ import type { ZodType } from 'zod';
18
14
 
19
- export declare type Check = "";
20
-
21
- export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>, modelName: string, connection: Mongoose.Connection): {
22
- model: Mongoose.Model<z.infer<typeof zodObject>>;
23
- schema: Mongoose.Schema;
15
+ export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>, modelName: string, connection: Connection): {
16
+ model: Model<z.infer<typeof zodObject>>;
17
+ schema: Schema;
24
18
  };
25
19
 
26
- export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Mongoose.Schema;
20
+ export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Schema;
27
21
 
28
- export declare type Field = {
29
- _def: FieldDefinition;
30
- checks?: Array<Check>;
31
- defaultValue?: () => unknown;
32
- };
22
+ export declare const default_alias: Options | Options[] | ((overrideOptions: Options) => Options | Options[] | Promise<Options | Options[]>);
33
23
 
34
- export declare type FieldDefinition = {
35
- description: string | undefined;
36
- innerType?: Field;
37
- typeName: string;
38
- };
24
+ /**
25
+ * Check if a Zod definition is an array
26
+ *
27
+ * @param definition The Zod definition to check
28
+ * @returns Whether the definition is an array
29
+ */
30
+ export declare function isZodArray(definition: ZodType): definition is ZodArray<ZodType>;
31
+
32
+ /**
33
+ * Type guard for whether the definition is a default.
34
+ *
35
+ * @param definition The definition to check.
36
+ * @returns True if the definition is a default.
37
+ */
38
+ export declare function isZodDefault(definition: ZodType): definition is ZodDefault;
39
+
40
+ /**
41
+ * Check if a Zod definition is an enum
42
+ *
43
+ * @param definition The Zod definition to check
44
+ * @returns Whether the definition is an enum
45
+ */
46
+ export declare function isZodEnum(definition: ZodType): definition is ZodEnum;
47
+
48
+ /**
49
+ * Type guard for whether the definition is nullable.
50
+ *
51
+ * @param definition The definition to check.
52
+ * @returns True if the definition is nullable.
53
+ */
54
+ export declare function isZodNullable(definition: ZodType): definition is ZodNullable;
39
55
 
40
- declare type SupportedPrimitive = ZodBoolean | ZodDate | ZodNumber | ZodString;
56
+ /**
57
+ * Check if a Zod definition is an object
58
+ *
59
+ * @param definition The Zod definition to check
60
+ * @returns Whether the definition is an object
61
+ */
62
+ export declare function isZodObject(definition: ZodType): definition is ZodObject<ZodRawShape>;
41
63
 
42
- export declare type SupportedType = SupportedPrimitive | ZodArray<ZodTypeAny> | ZodDefault<ZodTypeAny> | ZodEnum<[string, ...string[]]> | ZodNativeEnum<EnumLike> | ZodNullable<ZodTypeAny> | ZodObject<ZodRawShape> | ZodOptional<ZodTypeAny> | ZodUnion<readonly [ZodTypeAny, ...ZodTypeAny[]]>;
64
+ /**
65
+ * Type guard for whether the definition is optional.
66
+ *
67
+ * @param definition The definition to check.
68
+ * @returns True if the definition is optional.
69
+ */
70
+ export declare function isZodOptional(definition: ZodType): definition is ZodOptional;
43
71
 
44
72
  export { }
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var T=Object.create;var p=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var g=Object.getPrototypeOf,b=Object.prototype.hasOwnProperty;var k=(e,n)=>{for(var t in n)p(e,t,{get:n[t],enumerable:!0})},y=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let a of S(n))!b.call(e,a)&&a!==t&&p(e,a,{get:()=>n[a],enumerable:!(o=h(n,a))||o.enumerable});return e};var w=(e,n,t)=>(t=e!=null?T(g(e)):{},y(n||!e||!e.__esModule?p(t,"default",{value:e,enumerable:!0}):t,e)),M=e=>y(p({},"__esModule",{value:!0}),e);var E={};k(E,{createSchema:()=>Z});module.exports=M(E);var s=w(require("mongoose"),1),i=require("zod");function Z(e,n,t){let o={};for(let r in e.shape){let d=e.shape[r];o[r]=f(r,d)}let a=new s.Schema(o);return!t||!n?a:{model:t.model(n,a),schema:a}}function f(e,n){let t=D(n),o;switch(t.definition._def.typeName){case i.ZodFirstPartyTypeKind.ZodArray:{let d=t.definition._def.type;if(m(d)){let c=d.shape,l={};for(let u in c)l[u]=f(u,c[u]);o=[l]}else o=[f(e,d)];break}case i.ZodFirstPartyTypeKind.ZodBoolean:o=Boolean;break;case i.ZodFirstPartyTypeKind.ZodDate:o=Date;break;case i.ZodFirstPartyTypeKind.ZodDefault:break;case i.ZodFirstPartyTypeKind.ZodEnum:{o={enum:t.definition.options,type:String};break}case i.ZodFirstPartyTypeKind.ZodNativeEnum:{let r=t.definition;o={enum:j(r.enum),type:String};break}case i.ZodFirstPartyTypeKind.ZodNumber:o=Number;break;case i.ZodFirstPartyTypeKind.ZodObject:break;case i.ZodFirstPartyTypeKind.ZodString:o=String;break;case i.ZodFirstPartyTypeKind.ZodUnion:o=s.SchemaTypes.Mixed;break;default:throw new TypeError(`Unsupported type: ${e}`)}if(m(t.definition)&&(o=Z(t.definition)),o===void 0)throw new TypeError(`Could not determine Mongoose type for Zod type: ${e}`);let a=t.defaultValue!==void 0;return t.nullable&&!a?{default:null,type:o}:a?{default:t.defaultValue,type:o}:o}function m(e){return e._def.typeName===i.ZodFirstPartyTypeKind.ZodObject}function O(e){return e._def.typeName===i.ZodFirstPartyTypeKind.ZodDefault}function z(e){return e._def.typeName===i.ZodFirstPartyTypeKind.ZodNullable}function N(e){return e._def.typeName===i.ZodFirstPartyTypeKind.ZodOptional}function j(e){let n=Object.keys(e).filter(o=>typeof e[o]=="number"||typeof e[o]=="string"),t={};for(let o of n)t[o]=e[o];return Object.values(t).filter(o=>typeof o=="string"||typeof o=="number")}function D(e){let n=e,t=!1,o=!1,a;for(;"innerType"in n._def;)z(n)&&(t=!0),N(n)&&(o=!0),O(n)&&(a=n._def.defaultValue()),n=n._def.innerType;return{defaultValue:a,definition:n,nullable:t,optional:o}}0&&(module.exports={createSchema});
1
+ "use strict";var s=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var x=(e,o)=>{for(var t in o)s(e,t,{get:o[t],enumerable:!0})},j=(e,o,t,n)=>{if(o&&typeof o=="object"||typeof o=="function")for(let i of O(o))!k.call(e,i)&&i!==t&&s(e,i,{get:()=>o[i],enumerable:!(n=g(o,i))||n.enumerable});return e};var E=e=>j(s({},"__esModule",{value:!0}),e);var D={};x(D,{createSchema:()=>w});module.exports=E(D);var f=require("mongoose"),c=require("zod");var r=require("zod");function Z(e){return e instanceof r.ZodArray}function h(e){return e instanceof r.ZodDefault}function b(e){return e instanceof r.ZodEnum}function T(e){return e instanceof r.ZodNullable}function u(e){return e instanceof r.ZodObject}function S(e){return e instanceof r.ZodOptional}function w(e,o,t){let n={};for(let a in e.shape){let d=e.shape[a];d instanceof c.ZodType&&(n[a]=l(a,d))}let i=new f.Schema(n);return!t||!o?i:{model:t.model(o,i),schema:i}}function l(e,o){let t=z(o),n;switch(t.definition.type){case"array":{if(!Z(t.definition))throw new TypeError(`Expected ${e} to be of type ZodArray`);let a=t.definition.element;if(u(a)){let d=a.shape,y={};for(let p in d){let m=d[p];m instanceof c.ZodType&&(y[p]=l(p,m))}n=[y]}else n=[l(e,a)];break}case"boolean":n=Boolean;break;case"date":n=Date;break;case"default":break;case"enum":{if(!b(t.definition))throw new TypeError(`Expected ${e} to be of type ZodEnum`);n={enum:t.definition.options,type:String};break}case"number":n=Number;break;case"object":break;case"string":n=String;break;case"union":n=f.SchemaTypes.Mixed;break;default:throw new TypeError(`Unsupported type: ${e}`)}if(u(t.definition)&&(n=w(t.definition)),n===void 0)throw new TypeError(`Could not determine Mongoose type for Zod type: ${e}`);let i=t.defaultValue!==void 0;return t.nullable&&!i?{default:null,type:n}:i?{default:t.defaultValue,type:n}:n}function z(e){let o=e,t=!1,n=!1,i;for(;"innerType"in o.def&&o.def.innerType instanceof c.ZodType;)T(o)&&(t=!0),S(o)&&(n=!0),h(o)&&(i=o.def.defaultValue),o=o.def.innerType;return{defaultValue:i,definition:o,nullable:t,optional:n}}0&&(module.exports={createSchema});
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { SchemaDefinition } from \"mongoose\";\nimport * as Mongoose from \"mongoose\";\nimport {\n\tEnumLike,\n\tZodFirstPartyTypeKind,\n\ttype z,\n\ttype ZodArray,\n\ttype ZodNativeEnum,\n\ttype ZodObject,\n\ttype ZodRawShape,\n} from \"zod\";\n\nimport type { SupportedType } from \"./types\";\n\n/**\n * Represents a valid type for a Mongoose schema field definition. This is a recursive type that handles all possible\n * valid structures for a schema property, including nested objects and arrays.\n *\n * @internal\n */\ntype MongooseSchemaType = Mongoose.SchemaDefinitionProperty;\nexport function createSchema<T extends ZodRawShape>(\n\tzodObject: ZodObject<T>,\n\tmodelName: string,\n\tconnection: Mongoose.Connection,\n): { model: Mongoose.Model<z.infer<typeof zodObject>>; schema: Mongoose.Schema };\nexport function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Mongoose.Schema;\n/**\n * Create a Mongoose schema from a Zod shape\n *\n * @template T The Zod schema shape.\n * @param zodObject The Zod shape to create the schema from\n * @param modelName The unique name to assign to the model\n * @param connection The Mongoose connection to create the model from\n * @returns The Mongoose schema\n */\nexport function createSchema<T extends ZodRawShape>(\n\tzodObject: ZodObject<T>,\n\tmodelName?: string,\n\tconnection?: Mongoose.Connection,\n): Mongoose.Schema | { model: Mongoose.Model<z.infer<typeof zodObject>>; schema: Mongoose.Schema } {\n\tconst convertedShape: Partial<SchemaDefinition> = {};\n\tfor (const key in zodObject.shape) {\n\t\tconst zodField = zodObject.shape[key];\n\t\tconvertedShape[key] = convertField(key, zodField);\n\t}\n\tconst schema = new Mongoose.Schema(convertedShape);\n\tif (!connection || !modelName) return schema;\n\treturn { model: connection.model<z.infer<typeof zodObject>>(modelName, schema), schema };\n}\n\n/**\n * Convert a Zod field to a Mongoose type\n *\n * @template T The Zod schema shape.\n * @param type The key of the field\n * @param zodField The Zod field to convert\n * @returns The Mongoose type\n * @throws TypeError If the type is not supported.\n */\nfunction convertField<T extends ZodRawShape>(type: string, zodField: T[Extract<keyof T, string>]): MongooseSchemaType {\n\tconst unwrappedData = unwrapType(zodField);\n\tlet coreType: MongooseSchemaType | undefined;\n\tswitch (unwrappedData.definition._def.typeName) {\n\t\tcase ZodFirstPartyTypeKind.ZodArray: {\n\t\t\tconst arrayType = unwrappedData.definition as ZodArray<SupportedType>;\n\t\t\tconst elementType = arrayType._def.type;\n\t\t\tif (isZodObject(elementType)) {\n\t\t\t\tconst shape = elementType.shape;\n\t\t\t\tconst convertedShape: { [key: string]: MongooseSchemaType } = {};\n\t\t\t\tfor (const key in shape) {\n\t\t\t\t\tconvertedShape[key] = convertField(key, shape[key]);\n\t\t\t\t}\n\t\t\t\tcoreType = [convertedShape];\n\t\t\t} else {\n\t\t\t\tcoreType = [convertField(type, elementType)];\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase ZodFirstPartyTypeKind.ZodBoolean:\n\t\t\tcoreType = Boolean;\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodDate:\n\t\t\tcoreType = Date;\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodDefault:\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodEnum: {\n\t\t\tconst enumType = unwrappedData.definition as unknown as z.ZodEnum<[string, ...string[]]>;\n\t\t\tcoreType = {\n\t\t\t\tenum: enumType.options,\n\t\t\t\ttype: String,\n\t\t\t};\n\t\t\tbreak;\n\t\t}\n\t\tcase ZodFirstPartyTypeKind.ZodNativeEnum: {\n\t\t\tconst enumType = unwrappedData.definition as unknown as ZodNativeEnum<EnumLike>;\n\t\t\tcoreType = {\n\t\t\t\tenum: getValidEnumValues(enumType.enum),\n\t\t\t\ttype: String,\n\t\t\t};\n\t\t\tbreak;\n\t\t}\n\t\tcase ZodFirstPartyTypeKind.ZodNumber:\n\t\t\tcoreType = Number;\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodObject:\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodString:\n\t\t\tcoreType = String;\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodUnion:\n\t\t\tcoreType = Mongoose.SchemaTypes.Mixed;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tthrow new TypeError(`Unsupported type: ${type}`);\n\t}\n\tif (isZodObject(unwrappedData.definition)) {\n\t\tcoreType = createSchema(unwrappedData.definition);\n\t}\n\tif (coreType === undefined) {\n\t\tthrow new TypeError(`Could not determine Mongoose type for Zod type: ${type}`);\n\t}\n\n\tconst hasDefaultValue = unwrappedData.defaultValue !== undefined;\n\n\tif (unwrappedData.nullable && !hasDefaultValue) {\n\t\treturn {\n\t\t\tdefault: null,\n\t\t\ttype: coreType,\n\t\t};\n\t}\n\n\tif (hasDefaultValue) {\n\t\treturn {\n\t\t\tdefault: unwrappedData.defaultValue,\n\t\t\ttype: coreType,\n\t\t};\n\t}\n\treturn coreType;\n}\n\n/**\n * Check if a Zod definition is an object\n *\n * @param definition The Zod definition to check\n * @returns Whether the definition is an object\n */\nfunction isZodObject(definition: SupportedType): definition is ZodObject<ZodRawShape> {\n\treturn definition._def.typeName === ZodFirstPartyTypeKind.ZodObject;\n}\nfunction isZodDefault(definition: z.ZodTypeAny): definition is z.ZodDefault<SupportedType> {\n\treturn definition._def.typeName === ZodFirstPartyTypeKind.ZodDefault;\n}\n\nfunction isZodNullable(definition: z.ZodTypeAny): definition is z.ZodNullable<SupportedType> {\n\treturn definition._def.typeName === ZodFirstPartyTypeKind.ZodNullable;\n}\n\nfunction isZodOptional(definition: z.ZodTypeAny): definition is z.ZodOptional<SupportedType> {\n\treturn definition._def.typeName === ZodFirstPartyTypeKind.ZodOptional;\n}\n\nfunction getValidEnumValues(obj: { [s: string]: unknown }): (string | number)[] {\n\tconst validKeys = Object.keys(obj).filter((k) => typeof obj[k] === \"number\" || typeof obj[k] === \"string\");\n\tconst filtered: { [s: string]: unknown } = {};\n\tfor (const k of validKeys) {\n\t\tfiltered[k] = obj[k];\n\t}\n\n\treturn Object.values(filtered).filter((v) => typeof v === \"string\" || typeof v === \"number\");\n}\n\n/**\n * Takes a complex type and returns the inner type definition along with the default if present.\n *\n * @param data The type data to unwrap.\n * @returns The inner type data along with the default if present.\n */\nfunction unwrapType(data: SupportedType): {\n\tdefaultValue?: unknown;\n\tdefinition: SupportedType;\n\tnullable: boolean;\n\toptional: boolean;\n} {\n\tlet definition = data;\n\tlet nullable = false;\n\tlet optional = false;\n\tlet defaultValue: unknown | undefined;\n\twhile (\"innerType\" in definition._def) {\n\t\tif (isZodNullable(definition)) {\n\t\t\tnullable = true;\n\t\t}\n\t\tif (isZodOptional(definition)) {\n\t\t\toptional = true;\n\t\t}\n\t\tif (isZodDefault(definition)) {\n\t\t\tdefaultValue = definition._def.defaultValue();\n\t\t}\n\t\tdefinition = definition._def.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AAO,cAAeZ,EAAW,MAC5BE,EAAcF,CAAU,IAC3BU,EAAW,IAERP,EAAcH,CAAU,IAC3BW,EAAW,IAERV,EAAaD,CAAU,IAC1BY,EAAeZ,EAAW,KAAK,aAAa,GAE7CA,EAAaA,EAAW,KAAK,UAE9B,MAAO,CAAE,aAAAY,EAAc,WAAAZ,EAAY,SAAAU,EAAU,SAAAC,CAAS,CACvD","names":["index_exports","__export","createSchema","__toCommonJS","Mongoose","import_zod","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","type","unwrappedData","unwrapType","coreType","elementType","isZodObject","shape","enumType","getValidEnumValues","hasDefaultValue","definition","isZodDefault","isZodNullable","isZodOptional","obj","validKeys","k","filtered","v","data","nullable","optional","defaultValue"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/typeGuards.ts"],"sourcesContent":["import type { Connection, Model, SchemaDefinition, SchemaDefinitionProperty } from \"mongoose\";\nimport { Schema, SchemaTypes } from \"mongoose\";\nimport type { z, ZodObject, ZodRawShape } from \"zod\";\nimport { ZodType } from \"zod\";\n\nimport { isZodArray, isZodDefault, isZodEnum, isZodNullable, isZodObject, isZodOptional } from \"./typeGuards\";\n\n/**\n * Represents a valid type for a Mongoose schema field definition. This is a recursive type that handles all possible\n * valid structures for a schema property, including nested objects and arrays.\n *\n * @internal\n */\ntype MongooseSchemaType = SchemaDefinitionProperty;\nexport function createSchema<T extends ZodRawShape>(\n\tzodObject: ZodObject<T>,\n\tmodelName: string,\n\tconnection: Connection,\n): { model: Model<z.infer<typeof zodObject>>; schema: Schema };\nexport function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Schema;\n/**\n * Create a Mongoose schema from a Zod shape\n *\n * @template T The Zod schema shape.\n * @param zodObject The Zod shape to create the schema from\n * @param modelName The unique name to assign to the model\n * @param connection The Mongoose connection to create the model from\n * @returns The Mongoose schema\n */\nexport function createSchema<T extends ZodRawShape>(\n\tzodObject: ZodObject<T>,\n\tmodelName?: string,\n\tconnection?: Connection,\n): Schema | { model: Model<z.infer<typeof zodObject>>; schema: Schema } {\n\tconst convertedShape: Partial<SchemaDefinition> = {};\n\tfor (const key in zodObject.shape) {\n\t\tconst zodField = zodObject.shape[key];\n\t\tif (!(zodField instanceof ZodType)) {\n\t\t\tcontinue; // There's metadata in the shape that doesn't represent fields.\n\t\t}\n\t\tconvertedShape[key] = convertField(key, zodField);\n\t}\n\tconst schema = new Schema(convertedShape);\n\tif (!connection || !modelName) return schema;\n\treturn { model: connection.model<z.infer<typeof zodObject>>(modelName, schema), schema };\n}\n\n/**\n * Convert a Zod field to a Mongoose type\n *\n * @param fieldName The key of the field\n * @param zodField The Zod field to convert\n * @returns The Mongoose type\n * @throws {TypeError} If the type is not supported.\n */\nfunction convertField(fieldName: string, zodField: ZodType): MongooseSchemaType {\n\tconst unwrappedData = unwrapType(zodField);\n\tlet coreType: MongooseSchemaType | undefined;\n\tswitch (unwrappedData.definition.type) {\n\t\tcase \"array\": {\n\t\t\tif (!isZodArray(unwrappedData.definition))\n\t\t\t\tthrow new TypeError(`Expected ${fieldName} to be of type ZodArray`);\n\t\t\tconst elementType = unwrappedData.definition.element;\n\t\t\tif (isZodObject(elementType)) {\n\t\t\t\tconst shape = elementType.shape;\n\t\t\t\tconst convertedShape: { [key: string]: MongooseSchemaType } = {};\n\t\t\t\tfor (const key in shape) {\n\t\t\t\t\tconst nestedField = shape[key];\n\t\t\t\t\tif (!(nestedField instanceof ZodType)) {\n\t\t\t\t\t\tcontinue; // There's metadata in the shape that doesn't represent fields.\n\t\t\t\t\t}\n\t\t\t\t\tconvertedShape[key] = convertField(key, nestedField);\n\t\t\t\t}\n\t\t\t\tcoreType = [convertedShape];\n\t\t\t} else {\n\t\t\t\tcoreType = [convertField(fieldName, elementType)];\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"boolean\":\n\t\t\tcoreType = Boolean;\n\t\t\tbreak;\n\t\tcase \"date\":\n\t\t\tcoreType = Date;\n\t\t\tbreak;\n\t\tcase \"default\":\n\t\t\tbreak;\n\t\tcase \"enum\": {\n\t\t\tif (!isZodEnum(unwrappedData.definition))\n\t\t\t\tthrow new TypeError(`Expected ${fieldName} to be of type ZodEnum`);\n\t\t\tconst enumType = unwrappedData.definition;\n\t\t\tcoreType = {\n\t\t\t\tenum: enumType.options,\n\t\t\t\ttype: String,\n\t\t\t};\n\t\t\tbreak;\n\t\t}\n\t\tcase \"number\":\n\t\t\tcoreType = Number;\n\t\t\tbreak;\n\t\tcase \"object\":\n\t\t\tbreak;\n\t\tcase \"string\":\n\t\t\tcoreType = String;\n\t\t\tbreak;\n\t\tcase \"union\":\n\t\t\tcoreType = SchemaTypes.Mixed;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tthrow new TypeError(`Unsupported type: ${fieldName}`);\n\t}\n\tif (isZodObject(unwrappedData.definition)) {\n\t\tcoreType = createSchema(unwrappedData.definition);\n\t}\n\tif (coreType === undefined) {\n\t\tthrow new TypeError(`Could not determine Mongoose type for Zod type: ${fieldName}`);\n\t}\n\n\tconst hasDefaultValue = unwrappedData.defaultValue !== undefined;\n\n\tif (unwrappedData.nullable && !hasDefaultValue) {\n\t\treturn {\n\t\t\tdefault: null,\n\t\t\ttype: coreType,\n\t\t};\n\t}\n\n\tif (hasDefaultValue) {\n\t\treturn {\n\t\t\tdefault: unwrappedData.defaultValue,\n\t\t\ttype: coreType,\n\t\t};\n\t}\n\treturn coreType;\n}\n\n/**\n * Gets the valid members of an enum.\n *\n * @param The Enum.\n * @returns The valid members.\n */\nfunction getValidEnumValues(obj: { [s: string]: unknown }): (number | string)[] {\n\tconst validKeys = Object.keys(obj).filter((k) => typeof obj[k] === \"number\" || typeof obj[k] === \"string\");\n\tconst filtered: { [s: string]: unknown } = {};\n\tfor (const k of validKeys) {\n\t\tfiltered[k] = obj[k];\n\t}\n\n\treturn Object.values(filtered).filter((v) => typeof v === \"string\" || typeof v === \"number\");\n}\n\n/**\n * Takes a complex type and returns the inner type definition along with the default if present.\n *\n * @param data The type data to unwrap.\n * @returns The inner type data along with the default if present.\n */\nfunction unwrapType(data: ZodType): {\n\tdefaultValue?: unknown;\n\tdefinition: ZodType;\n\tnullable: boolean;\n\toptional: boolean;\n} {\n\tlet definition = data;\n\tlet nullable = false;\n\tlet optional = false;\n\tlet defaultValue: unknown;\n\twhile (\"innerType\" in definition.def && definition.def.innerType instanceof ZodType) {\n\t\tif (isZodNullable(definition)) {\n\t\t\tnullable = true;\n\t\t}\n\t\tif (isZodOptional(definition)) {\n\t\t\toptional = true;\n\t\t}\n\t\tif (isZodDefault(definition)) {\n\t\t\tdefaultValue = definition.def.defaultValue;\n\t\t}\n\t\tdefinition = definition.def.innerType;\n\t}\n\treturn { defaultValue, definition, nullable, optional };\n}\n","import type { ZodRawShape, ZodType } from \"zod\";\nimport { ZodArray, ZodDefault, ZodEnum, ZodNullable, ZodObject, ZodOptional } from \"zod\";\n\n/**\n * Check if a Zod definition is an array\n *\n * @param definition The Zod definition to check\n * @returns Whether the definition is an array\n */\nexport function isZodArray(definition: ZodType): definition is ZodArray<ZodType> {\n\treturn definition instanceof ZodArray;\n}\n\n/**\n * Type guard for whether the definition is a default.\n *\n * @param definition The definition to check.\n * @returns True if the definition is a default.\n */\nexport function isZodDefault(definition: ZodType): definition is ZodDefault {\n\treturn definition instanceof ZodDefault;\n}\n/**\n * Check if a Zod definition is an enum\n *\n * @param definition The Zod definition to check\n * @returns Whether the definition is an enum\n */\nexport function isZodEnum(definition: ZodType): definition is ZodEnum {\n\treturn definition instanceof ZodEnum;\n}\n\n/**\n * Type guard for whether the definition is nullable.\n *\n * @param definition The definition to check.\n * @returns True if the definition is nullable.\n */\nexport function isZodNullable(definition: ZodType): definition is ZodNullable {\n\treturn definition instanceof ZodNullable;\n}\n\n/**\n * Check if a Zod definition is an object\n *\n * @param definition The Zod definition to check\n * @returns Whether the definition is an object\n */\nexport function isZodObject(definition: ZodType): definition is ZodObject<ZodRawShape> {\n\treturn definition instanceof ZodObject;\n}\n/**\n * Type guard for whether the definition is optional.\n *\n * @param definition The definition to check.\n * @returns True if the definition is optional.\n */\nexport function isZodOptional(definition: ZodType): definition is ZodOptional {\n\treturn definition instanceof ZodOptional;\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,IAAA,eAAAC,EAAAH,GACA,IAAAI,EAAoC,oBAEpCC,EAAwB,eCFxB,IAAAC,EAAmF,eAQ5E,SAASC,EAAWC,EAAsD,CAChF,OAAOA,aAAsB,UAC9B,CAQO,SAASC,EAAaD,EAA+C,CAC3E,OAAOA,aAAsB,YAC9B,CAOO,SAASE,EAAUF,EAA4C,CACrE,OAAOA,aAAsB,SAC9B,CAQO,SAASG,EAAcH,EAAgD,CAC7E,OAAOA,aAAsB,aAC9B,CAQO,SAASI,EAAYJ,EAA2D,CACtF,OAAOA,aAAsB,WAC9B,CAOO,SAASK,EAAcL,EAAgD,CAC7E,OAAOA,aAAsB,aAC9B,CD9BO,SAASM,EACfC,EACAC,EACAC,EACuE,CACvE,IAAMC,EAA4C,CAAC,EACnD,QAAWC,KAAOJ,EAAU,MAAO,CAClC,IAAMK,EAAWL,EAAU,MAAMI,CAAG,EAC9BC,aAAoB,YAG1BF,EAAeC,CAAG,EAAIE,EAAaF,EAAKC,CAAQ,EACjD,CACA,IAAME,EAAS,IAAI,SAAOJ,CAAc,EACxC,MAAI,CAACD,GAAc,CAACD,EAAkBM,EAC/B,CAAE,MAAOL,EAAW,MAAiCD,EAAWM,CAAM,EAAG,OAAAA,CAAO,CACxF,CAUA,SAASD,EAAaE,EAAmBH,EAAuC,CAC/E,IAAMI,EAAgBC,EAAWL,CAAQ,EACrCM,EACJ,OAAQF,EAAc,WAAW,KAAM,CACtC,IAAK,QAAS,CACb,GAAI,CAACG,EAAWH,EAAc,UAAU,EACvC,MAAM,IAAI,UAAU,YAAYD,CAAS,yBAAyB,EACnE,IAAMK,EAAcJ,EAAc,WAAW,QAC7C,GAAIK,EAAYD,CAAW,EAAG,CAC7B,IA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O,CAAE,aAAAG,EAAc,WAAAH,EAAY,SAAAC,EAAU,SAAAC,CAAS,CACvD","names":["index_exports","__export","createSchema","__toCommonJS","import_mongoose","import_zod","import_zod","isZodArray","definition","isZodDefault","isZodEnum","isZodNullable","isZodObject","isZodOptional","createSchema","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","fieldName","unwrappedData","unwrapType","coreType","isZodArray","elementType","isZodObject","shape","nestedField","isZodEnum","hasDefaultValue","unwrapType","data","definition","nullable","optional","defaultValue","isZodNullable","isZodOptional","isZodDefault"]}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import*as p from"mongoose";import{ZodFirstPartyTypeKind as a}from"zod";function y(o,t,n){let e={};for(let r in o.shape){let d=o.shape[r];e[r]=u(r,d)}let i=new p.Schema(e);return!n||!t?i:{model:n.model(t,i),schema:i}}function u(o,t){let n=S(t),e;switch(n.definition._def.typeName){case a.ZodArray:{let d=n.definition._def.type;if(l(d)){let f=d.shape,c={};for(let s in f)c[s]=u(s,f[s]);e=[c]}else e=[u(o,d)];break}case a.ZodBoolean:e=Boolean;break;case a.ZodDate:e=Date;break;case a.ZodDefault:break;case a.ZodEnum:{e={enum:n.definition.options,type:String};break}case a.ZodNativeEnum:{let r=n.definition;e={enum:h(r.enum),type:String};break}case a.ZodNumber:e=Number;break;case a.ZodObject:break;case a.ZodString:e=String;break;case a.ZodUnion:e=p.SchemaTypes.Mixed;break;default:throw new TypeError(`Unsupported type: ${o}`)}if(l(n.definition)&&(e=y(n.definition)),e===void 0)throw new TypeError(`Could not determine Mongoose type for Zod type: ${o}`);let i=n.defaultValue!==void 0;return n.nullable&&!i?{default:null,type:e}:i?{default:n.defaultValue,type:e}:e}function l(o){return o._def.typeName===a.ZodObject}function m(o){return o._def.typeName===a.ZodDefault}function Z(o){return o._def.typeName===a.ZodNullable}function T(o){return o._def.typeName===a.ZodOptional}function h(o){let t=Object.keys(o).filter(e=>typeof o[e]=="number"||typeof o[e]=="string"),n={};for(let e of t)n[e]=o[e];return Object.values(n).filter(e=>typeof e=="string"||typeof e=="number")}function S(o){let t=o,n=!1,e=!1,i;for(;"innerType"in t._def;)Z(t)&&(n=!0),T(t)&&(e=!0),m(t)&&(i=t._def.defaultValue()),t=t._def.innerType;return{defaultValue:i,definition:t,nullable:n,optional:e}}export{y as createSchema};
1
+ import{Schema as k,SchemaTypes as x}from"mongoose";import{ZodType as p}from"zod";import{ZodArray as b,ZodDefault as T,ZodEnum as S,ZodNullable as w,ZodObject as g,ZodOptional as O}from"zod";function l(e){return e instanceof b}function y(e){return e instanceof T}function m(e){return e instanceof S}function Z(e){return e instanceof w}function f(e){return e instanceof g}function h(e){return e instanceof O}function j(e,t,n){let o={};for(let r in e.shape){let a=e.shape[r];a instanceof p&&(o[r]=c(r,a))}let i=new k(o);return!n||!t?i:{model:n.model(t,i),schema:i}}function c(e,t){let n=E(t),o;switch(n.definition.type){case"array":{if(!l(n.definition))throw new TypeError(`Expected ${e} to be of type ZodArray`);let r=n.definition.element;if(f(r)){let a=r.shape,s={};for(let d in a){let u=a[d];u instanceof p&&(s[d]=c(d,u))}o=[s]}else o=[c(e,r)];break}case"boolean":o=Boolean;break;case"date":o=Date;break;case"default":break;case"enum":{if(!m(n.definition))throw new TypeError(`Expected ${e} to be of type ZodEnum`);o={enum:n.definition.options,type:String};break}case"number":o=Number;break;case"object":break;case"string":o=String;break;case"union":o=x.Mixed;break;default:throw new TypeError(`Unsupported type: ${e}`)}if(f(n.definition)&&(o=j(n.definition)),o===void 0)throw new TypeError(`Could not determine Mongoose type for Zod type: ${e}`);let i=n.defaultValue!==void 0;return n.nullable&&!i?{default:null,type:o}:i?{default:n.defaultValue,type:o}:o}function E(e){let t=e,n=!1,o=!1,i;for(;"innerType"in t.def&&t.def.innerType instanceof p;)Z(t)&&(n=!0),h(t)&&(o=!0),y(t)&&(i=t.def.defaultValue),t=t.def.innerType;return{defaultValue:i,definition:t,nullable:n,optional:o}}export{j as createSchema};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { SchemaDefinition } from \"mongoose\";\nimport * as Mongoose from \"mongoose\";\nimport {\n\tEnumLike,\n\tZodFirstPartyTypeKind,\n\ttype z,\n\ttype ZodArray,\n\ttype ZodNativeEnum,\n\ttype ZodObject,\n\ttype ZodRawShape,\n} from \"zod\";\n\nimport type { SupportedType } from \"./types\";\n\n/**\n * Represents a valid type for a Mongoose schema field definition. This is a recursive type that handles all possible\n * valid structures for a schema property, including nested objects and arrays.\n *\n * @internal\n */\ntype MongooseSchemaType = Mongoose.SchemaDefinitionProperty;\nexport function createSchema<T extends ZodRawShape>(\n\tzodObject: ZodObject<T>,\n\tmodelName: string,\n\tconnection: Mongoose.Connection,\n): { model: Mongoose.Model<z.infer<typeof zodObject>>; schema: Mongoose.Schema };\nexport function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Mongoose.Schema;\n/**\n * Create a Mongoose schema from a Zod shape\n *\n * @template T The Zod schema shape.\n * @param zodObject The Zod shape to create the schema from\n * @param modelName The unique name to assign to the model\n * @param connection The Mongoose connection to create the model from\n * @returns The Mongoose schema\n */\nexport function createSchema<T extends ZodRawShape>(\n\tzodObject: ZodObject<T>,\n\tmodelName?: string,\n\tconnection?: Mongoose.Connection,\n): Mongoose.Schema | { model: Mongoose.Model<z.infer<typeof zodObject>>; schema: Mongoose.Schema } {\n\tconst convertedShape: Partial<SchemaDefinition> = {};\n\tfor (const key in zodObject.shape) {\n\t\tconst zodField = zodObject.shape[key];\n\t\tconvertedShape[key] = convertField(key, zodField);\n\t}\n\tconst schema = new Mongoose.Schema(convertedShape);\n\tif (!connection || !modelName) return schema;\n\treturn { model: connection.model<z.infer<typeof zodObject>>(modelName, schema), schema };\n}\n\n/**\n * Convert a Zod field to a Mongoose type\n *\n * @template T The Zod schema shape.\n * @param type The key of the field\n * @param zodField The Zod field to convert\n * @returns The Mongoose type\n * @throws TypeError If the type is not supported.\n */\nfunction convertField<T extends ZodRawShape>(type: string, zodField: T[Extract<keyof T, string>]): MongooseSchemaType {\n\tconst unwrappedData = unwrapType(zodField);\n\tlet coreType: MongooseSchemaType | undefined;\n\tswitch (unwrappedData.definition._def.typeName) {\n\t\tcase ZodFirstPartyTypeKind.ZodArray: {\n\t\t\tconst arrayType = unwrappedData.definition as ZodArray<SupportedType>;\n\t\t\tconst elementType = arrayType._def.type;\n\t\t\tif (isZodObject(elementType)) {\n\t\t\t\tconst shape = elementType.shape;\n\t\t\t\tconst convertedShape: { [key: string]: MongooseSchemaType } = {};\n\t\t\t\tfor (const key in shape) {\n\t\t\t\t\tconvertedShape[key] = convertField(key, shape[key]);\n\t\t\t\t}\n\t\t\t\tcoreType = [convertedShape];\n\t\t\t} else {\n\t\t\t\tcoreType = [convertField(type, elementType)];\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase ZodFirstPartyTypeKind.ZodBoolean:\n\t\t\tcoreType = Boolean;\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodDate:\n\t\t\tcoreType = Date;\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodDefault:\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodEnum: {\n\t\t\tconst enumType = unwrappedData.definition as unknown as z.ZodEnum<[string, ...string[]]>;\n\t\t\tcoreType = {\n\t\t\t\tenum: enumType.options,\n\t\t\t\ttype: String,\n\t\t\t};\n\t\t\tbreak;\n\t\t}\n\t\tcase ZodFirstPartyTypeKind.ZodNativeEnum: {\n\t\t\tconst enumType = unwrappedData.definition as unknown as ZodNativeEnum<EnumLike>;\n\t\t\tcoreType = {\n\t\t\t\tenum: getValidEnumValues(enumType.enum),\n\t\t\t\ttype: String,\n\t\t\t};\n\t\t\tbreak;\n\t\t}\n\t\tcase ZodFirstPartyTypeKind.ZodNumber:\n\t\t\tcoreType = Number;\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodObject:\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodString:\n\t\t\tcoreType = String;\n\t\t\tbreak;\n\t\tcase ZodFirstPartyTypeKind.ZodUnion:\n\t\t\tcoreType = Mongoose.SchemaTypes.Mixed;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tthrow new TypeError(`Unsupported type: ${type}`);\n\t}\n\tif (isZodObject(unwrappedData.definition)) {\n\t\tcoreType = createSchema(unwrappedData.definition);\n\t}\n\tif (coreType === undefined) {\n\t\tthrow new TypeError(`Could not determine Mongoose type for Zod type: ${type}`);\n\t}\n\n\tconst hasDefaultValue = unwrappedData.defaultValue !== undefined;\n\n\tif (unwrappedData.nullable && !hasDefaultValue) {\n\t\treturn {\n\t\t\tdefault: null,\n\t\t\ttype: coreType,\n\t\t};\n\t}\n\n\tif (hasDefaultValue) {\n\t\treturn {\n\t\t\tdefault: unwrappedData.defaultValue,\n\t\t\ttype: coreType,\n\t\t};\n\t}\n\treturn coreType;\n}\n\n/**\n * Check if a Zod definition is an object\n *\n * @param definition The Zod definition to check\n * @returns Whether the definition is an object\n */\nfunction isZodObject(definition: SupportedType): definition is ZodObject<ZodRawShape> {\n\treturn definition._def.typeName === ZodFirstPartyTypeKind.ZodObject;\n}\nfunction isZodDefault(definition: z.ZodTypeAny): definition is z.ZodDefault<SupportedType> {\n\treturn definition._def.typeName === ZodFirstPartyTypeKind.ZodDefault;\n}\n\nfunction isZodNullable(definition: z.ZodTypeAny): definition is z.ZodNullable<SupportedType> {\n\treturn definition._def.typeName === ZodFirstPartyTypeKind.ZodNullable;\n}\n\nfunction isZodOptional(definition: z.ZodTypeAny): definition is z.ZodOptional<SupportedType> {\n\treturn definition._def.typeName === ZodFirstPartyTypeKind.ZodOptional;\n}\n\nfunction getValidEnumValues(obj: { [s: string]: unknown }): (string | number)[] {\n\tconst validKeys = Object.keys(obj).filter((k) => typeof obj[k] === \"number\" || typeof obj[k] === \"string\");\n\tconst filtered: { [s: string]: unknown } = {};\n\tfor (const k of validKeys) {\n\t\tfiltered[k] = obj[k];\n\t}\n\n\treturn Object.values(filtered).filter((v) => typeof v === \"string\" || typeof v === \"number\");\n}\n\n/**\n * Takes a complex type and returns the inner type definition along with the default if present.\n *\n * @param data The type data to unwrap.\n * @returns The inner type data along with the default if present.\n */\nfunction unwrapType(data: SupportedType): {\n\tdefaultValue?: unknown;\n\tdefinition: SupportedType;\n\tnullable: boolean;\n\toptional: boolean;\n} {\n\tlet definition = data;\n\tlet nullable = false;\n\tlet optional = false;\n\tlet defaultValue: unknown | undefined;\n\twhile (\"innerType\" in definition._def) {\n\t\tif (isZodNullable(definition)) {\n\t\t\tnullable = true;\n\t\t}\n\t\tif (isZodOptional(definition)) {\n\t\t\toptional = true;\n\t\t}\n\t\tif (isZodDefault(definition)) {\n\t\t\tdefaultValue = definition._def.defaultValue();\n\t\t}\n\t\tdefinition = definition._def.innerType;\n\t}\n\treturn { defaultValue, definition, nullable, optional };\n}\n"],"mappings":"AACA,UAAYA,MAAc,WAC1B,OAEC,yBAAAC,MAMM,MA0BA,SAASC,EACfC,EACAC,EACAC,EACkG,CAClG,IAAMC,EAA4C,CAAC,EACnD,QAAWC,KAAOJ,EAAU,MAAO,CAClC,IAAMK,EAAWL,EAAU,MAAMI,CAAG,EACpCD,EAAeC,CAAG,EAAIE,EAAaF,EAAKC,CAAQ,CACjD,CACA,IAAME,EAAS,IAAa,SAAOJ,CAAc,EACjD,MAAI,CAACD,GAAc,CAACD,EAAkBM,EAC/B,CAAE,MAAOL,EAAW,MAAiCD,EAAWM,CAAM,EAAG,OAAAA,CAAO,CACxF,CAWA,SAASD,EAAoCE,EAAcH,EAA2D,CACrH,IAAMI,EAAgBC,EAAWL,CAAQ,EACrCM,EACJ,OAAQF,EAAc,WAAW,KAAK,SAAU,CAC/C,KAAKX,EAAsB,SAAU,CAEpC,IAAMc,EADYH,EAAc,WACF,KAAK,KACnC,GAAII,EAAYD,CAAW,EAAG,CAC7B,IAAME,EAAQF,EA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AAsB,WAC3D,CAEA,SAASuB,EAAcH,EAAsE,CAC5F,OAAOA,EAAW,KAAK,WAAapB,EAAsB,WAC3D,CAEA,SAASkB,EAAmBM,EAAoD,CAC/E,IAAMC,EAAY,OAAO,KAAKD,CAAG,EAAE,OAAQE,GAAM,OAAOF,EAAIE,CAAC,GAAM,UAAY,OAAOF,EAAIE,CAAC,GAAM,QAAQ,EACnGC,EAAqC,CAAC,EAC5C,QAAWD,KAAKD,EACfE,EAASD,CAAC,EAAIF,EAAIE,CAAC,EAGpB,OAAO,OAAO,OAAOC,CAAQ,EAAE,OAAQC,GAAM,OAAOA,GAAM,UAAY,OAAOA,GAAM,QAAQ,CAC5F,CAQA,SAAShB,EAAWiB,EAKlB,CACD,IAAIT,EAAaS,EACbC,EAAW,GACXC,EAAW,GACXC,EACJ,KAAO,cAAeZ,EAAW,MAC5BE,EAAcF,CAAU,IAC3BU,EAAW,IAERP,EAAcH,CAAU,IAC3BW,EAAW,IAERV,EAAaD,CAAU,IAC1BY,EAAeZ,EAAW,KAAK,aAAa,GAE7CA,EAAaA,EAAW,KAAK,UAE9B,MAAO,CAAE,aAAAY,EAAc,WAAAZ,EAAY,SAAAU,EAAU,SAAAC,CAAS,CACvD","names":["Mongoose","ZodFirstPartyTypeKind","createSchema","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","type","unwrappedData","unwrapType","coreType","elementType","isZodObject","shape","enumType","getValidEnumValues","hasDefaultValue","definition","isZodDefault","isZodNullable","isZodOptional","obj","validKeys","k","filtered","v","data","nullable","optional","defaultValue"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/typeGuards.ts"],"sourcesContent":["import type { Connection, Model, SchemaDefinition, SchemaDefinitionProperty } from \"mongoose\";\nimport { Schema, SchemaTypes } from \"mongoose\";\nimport type { z, ZodObject, ZodRawShape } from \"zod\";\nimport { ZodType } from \"zod\";\n\nimport { isZodArray, isZodDefault, isZodEnum, isZodNullable, isZodObject, isZodOptional } from \"./typeGuards\";\n\n/**\n * Represents a valid type for a Mongoose schema field definition. This is a recursive type that handles all possible\n * valid structures for a schema property, including nested objects and arrays.\n *\n * @internal\n */\ntype MongooseSchemaType = SchemaDefinitionProperty;\nexport function createSchema<T extends ZodRawShape>(\n\tzodObject: ZodObject<T>,\n\tmodelName: string,\n\tconnection: Connection,\n): { model: Model<z.infer<typeof zodObject>>; schema: Schema };\nexport function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Schema;\n/**\n * Create a Mongoose schema from a Zod shape\n *\n * @template T The Zod schema shape.\n * @param zodObject The Zod shape to create the schema from\n * @param modelName The unique name to assign to the model\n * @param connection The Mongoose connection to create the model from\n * @returns The Mongoose schema\n */\nexport function createSchema<T extends ZodRawShape>(\n\tzodObject: ZodObject<T>,\n\tmodelName?: string,\n\tconnection?: Connection,\n): Schema | { model: Model<z.infer<typeof zodObject>>; schema: Schema } {\n\tconst convertedShape: Partial<SchemaDefinition> = {};\n\tfor (const key in zodObject.shape) {\n\t\tconst zodField = zodObject.shape[key];\n\t\tif (!(zodField instanceof ZodType)) {\n\t\t\tcontinue; // There's metadata in the shape that doesn't represent fields.\n\t\t}\n\t\tconvertedShape[key] = convertField(key, zodField);\n\t}\n\tconst schema = new Schema(convertedShape);\n\tif (!connection || !modelName) return schema;\n\treturn { model: connection.model<z.infer<typeof zodObject>>(modelName, schema), schema };\n}\n\n/**\n * Convert a Zod field to a Mongoose type\n *\n * @param fieldName The key of the field\n * @param zodField The Zod field to convert\n * @returns The Mongoose type\n * @throws {TypeError} If the type is not supported.\n */\nfunction convertField(fieldName: string, zodField: ZodType): MongooseSchemaType {\n\tconst unwrappedData = unwrapType(zodField);\n\tlet coreType: MongooseSchemaType | undefined;\n\tswitch (unwrappedData.definition.type) {\n\t\tcase \"array\": {\n\t\t\tif (!isZodArray(unwrappedData.definition))\n\t\t\t\tthrow new TypeError(`Expected ${fieldName} to be of type ZodArray`);\n\t\t\tconst elementType = unwrappedData.definition.element;\n\t\t\tif (isZodObject(elementType)) {\n\t\t\t\tconst shape = elementType.shape;\n\t\t\t\tconst convertedShape: { [key: string]: MongooseSchemaType } = {};\n\t\t\t\tfor (const key in shape) {\n\t\t\t\t\tconst nestedField = shape[key];\n\t\t\t\t\tif (!(nestedField instanceof ZodType)) {\n\t\t\t\t\t\tcontinue; // There's metadata in the shape that doesn't represent fields.\n\t\t\t\t\t}\n\t\t\t\t\tconvertedShape[key] = convertField(key, nestedField);\n\t\t\t\t}\n\t\t\t\tcoreType = [convertedShape];\n\t\t\t} else {\n\t\t\t\tcoreType = [convertField(fieldName, elementType)];\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"boolean\":\n\t\t\tcoreType = Boolean;\n\t\t\tbreak;\n\t\tcase \"date\":\n\t\t\tcoreType = Date;\n\t\t\tbreak;\n\t\tcase \"default\":\n\t\t\tbreak;\n\t\tcase \"enum\": {\n\t\t\tif (!isZodEnum(unwrappedData.definition))\n\t\t\t\tthrow new TypeError(`Expected ${fieldName} to be of type ZodEnum`);\n\t\t\tconst enumType = unwrappedData.definition;\n\t\t\tcoreType = {\n\t\t\t\tenum: enumType.options,\n\t\t\t\ttype: String,\n\t\t\t};\n\t\t\tbreak;\n\t\t}\n\t\tcase \"number\":\n\t\t\tcoreType = Number;\n\t\t\tbreak;\n\t\tcase \"object\":\n\t\t\tbreak;\n\t\tcase \"string\":\n\t\t\tcoreType = String;\n\t\t\tbreak;\n\t\tcase \"union\":\n\t\t\tcoreType = SchemaTypes.Mixed;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tthrow new TypeError(`Unsupported type: ${fieldName}`);\n\t}\n\tif (isZodObject(unwrappedData.definition)) {\n\t\tcoreType = createSchema(unwrappedData.definition);\n\t}\n\tif (coreType === undefined) {\n\t\tthrow new TypeError(`Could not determine Mongoose type for Zod type: ${fieldName}`);\n\t}\n\n\tconst hasDefaultValue = unwrappedData.defaultValue !== undefined;\n\n\tif (unwrappedData.nullable && !hasDefaultValue) {\n\t\treturn {\n\t\t\tdefault: null,\n\t\t\ttype: coreType,\n\t\t};\n\t}\n\n\tif (hasDefaultValue) {\n\t\treturn {\n\t\t\tdefault: unwrappedData.defaultValue,\n\t\t\ttype: coreType,\n\t\t};\n\t}\n\treturn coreType;\n}\n\n/**\n * Gets the valid members of an enum.\n *\n * @param The Enum.\n * @returns The valid members.\n */\nfunction getValidEnumValues(obj: { [s: string]: unknown }): (number | string)[] {\n\tconst validKeys = Object.keys(obj).filter((k) => typeof obj[k] === \"number\" || typeof obj[k] === \"string\");\n\tconst filtered: { [s: string]: unknown } = {};\n\tfor (const k of validKeys) {\n\t\tfiltered[k] = obj[k];\n\t}\n\n\treturn Object.values(filtered).filter((v) => typeof v === \"string\" || typeof v === \"number\");\n}\n\n/**\n * Takes a complex type and returns the inner type definition along with the default if present.\n *\n * @param data The type data to unwrap.\n * @returns The inner type data along with the default if present.\n */\nfunction unwrapType(data: ZodType): {\n\tdefaultValue?: unknown;\n\tdefinition: ZodType;\n\tnullable: boolean;\n\toptional: boolean;\n} {\n\tlet definition = data;\n\tlet nullable = false;\n\tlet optional = false;\n\tlet defaultValue: unknown;\n\twhile (\"innerType\" in definition.def && definition.def.innerType instanceof ZodType) {\n\t\tif (isZodNullable(definition)) {\n\t\t\tnullable = true;\n\t\t}\n\t\tif (isZodOptional(definition)) {\n\t\t\toptional = true;\n\t\t}\n\t\tif (isZodDefault(definition)) {\n\t\t\tdefaultValue = definition.def.defaultValue;\n\t\t}\n\t\tdefinition = definition.def.innerType;\n\t}\n\treturn { defaultValue, definition, nullable, optional };\n}\n","import type { ZodRawShape, ZodType } from \"zod\";\nimport { ZodArray, ZodDefault, ZodEnum, ZodNullable, ZodObject, ZodOptional } from \"zod\";\n\n/**\n * Check if a Zod definition is an array\n *\n * @param definition The Zod definition to check\n * @returns Whether the definition is an array\n */\nexport function isZodArray(definition: ZodType): definition is ZodArray<ZodType> {\n\treturn definition instanceof ZodArray;\n}\n\n/**\n * Type guard for whether the definition is a default.\n *\n * @param definition The definition to check.\n * @returns True if the definition is a default.\n */\nexport function isZodDefault(definition: ZodType): definition is ZodDefault {\n\treturn definition instanceof ZodDefault;\n}\n/**\n * Check if a Zod definition is an enum\n *\n * @param definition The Zod definition to check\n * @returns Whether the definition is an enum\n */\nexport function isZodEnum(definition: ZodType): definition is ZodEnum {\n\treturn definition instanceof ZodEnum;\n}\n\n/**\n * Type guard for whether the definition is nullable.\n *\n * @param definition The definition to check.\n * @returns True if the definition is nullable.\n */\nexport function isZodNullable(definition: ZodType): definition is ZodNullable {\n\treturn definition instanceof ZodNullable;\n}\n\n/**\n * Check if a Zod definition is an object\n *\n * @param definition The Zod definition to check\n * @returns Whether the definition is an object\n */\nexport function isZodObject(definition: ZodType): definition is ZodObject<ZodRawShape> {\n\treturn definition instanceof ZodObject;\n}\n/**\n * Type guard for whether the definition is optional.\n *\n * @param definition The definition to check.\n * @returns True if the definition is optional.\n */\nexport function isZodOptional(definition: ZodType): definition is ZodOptional {\n\treturn definition instanceof ZodOptional;\n}\n"],"mappings":"AACA,OAAS,UAAAA,EAAQ,eAAAC,MAAmB,WAEpC,OAAS,WAAAC,MAAe,MCFxB,OAAS,YAAAC,EAAU,cAAAC,EAAY,WAAAC,EAAS,eAAAC,EAAa,aAAAC,EAAW,eAAAC,MAAmB,MAQ5E,SAASC,EAAWC,EAAsD,CAChF,OAAOA,aAAsBP,CAC9B,CAQO,SAASQ,EAAaD,EAA+C,CAC3E,OAAOA,aAAsBN,CAC9B,CAOO,SAASQ,EAAUF,EAA4C,CACrE,OAAOA,aAAsBL,CAC9B,CAQO,SAASQ,EAAcH,EAAgD,CAC7E,OAAOA,aAAsBJ,CAC9B,CAQO,SAASQ,EAAYJ,EAA2D,CACtF,OAAOA,aAAsBH,CAC9B,CAOO,SAASQ,EAAcL,EAAgD,CAC7E,OAAOA,aAAsBF,CAC9B,CD9BO,SAASQ,EACfC,EACAC,EACAC,EACuE,CACvE,IAAMC,EAA4C,CAAC,EACnD,QAAWC,KAAOJ,EAAU,MAAO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EAAc,UAAU,IACvCE,EAAWd,EAAaY,EAAc,UAAU,GAE7CE,IAAa,OAChB,MAAM,IAAI,UAAU,mDAAmDH,CAAS,EAAE,EAGnF,IAAMW,EAAkBV,EAAc,eAAiB,OAEvD,OAAIA,EAAc,UAAY,CAACU,EACvB,CACN,QAAS,KACT,KAAMR,CACP,EAGGQ,EACI,CACN,QAASV,EAAc,aACvB,KAAME,CACP,EAEMA,CACR,CAwBA,SAASS,EAAWC,EAKlB,CACD,IAAIC,EAAaD,EACbE,EAAW,GACXC,EAAW,GACXC,EACJ,KAAO,cAAeH,EAAW,KAAOA,EAAW,IAAI,qBAAqBI,GACvEC,EAAcL,CAAU,IAC3BC,EAAW,IAERK,EAAcN,CAAU,IAC3BE,EAAW,IAERK,EAAaP,CAAU,IAC1BG,EAAeH,EAAW,IAAI,cAE/BA,EAAaA,EAAW,IAAI,UAE7B,MAAO,CAAE,aAAAG,EAAc,WAAAH,EAAY,SAAAC,EAAU,SAAAC,CAAS,CACvD","names":["Schema","SchemaTypes","ZodType","ZodArray","ZodDefault","ZodEnum","ZodNullable","ZodObject","ZodOptional","isZodArray","definition","isZodDefault","isZodEnum","isZodNullable","isZodObject","isZodOptional","createSchema","zodObject","modelName","connection","convertedShape","key","zodField","ZodType","convertField","schema","Schema","fieldName","unwrappedData","unwrapType","coreType","isZodArray","elementType","isZodObject","shape","nestedField","isZodEnum","SchemaTypes","hasDefaultValue","unwrapType","data","definition","nullable","optional","defaultValue","ZodType","isZodNullable","isZodOptional","isZodDefault"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mirite/zod-to-mongoose",
3
- "version": "0.3.1",
3
+ "version": "0.4.1",
4
4
  "description": "Package for creating mongoose schemas out of Zod schemas for type safety, and removing duplicate effort.",
5
5
  "keywords": [
6
6
  "zod",
@@ -40,22 +40,22 @@
40
40
  "test:watch": "vitest --ui --coverage"
41
41
  },
42
42
  "devDependencies": {
43
- "@microsoft/api-extractor": "^7.55.0",
44
- "@mirite/eslint-config-mirite": "^1.3.3",
45
- "@vitest/coverage-v8": "^4.0.10",
46
- "@vitest/ui": "^4.0.10",
47
- "eslint": "^9.39.1",
48
- "mongoose": "^8.20.0",
49
- "prettier": "^3.6.2",
43
+ "@microsoft/api-extractor": "^7.57.6",
44
+ "@mirite/eslint-config-mirite": "^2.0.1",
45
+ "@vitest/coverage-v8": "^4.0.18",
46
+ "@vitest/ui": "^4.0.18",
47
+ "eslint": "^9.39.3",
48
+ "mongoose": "^9.2.3",
49
+ "prettier": "^3.8.1",
50
50
  "tsup": "^8.5.1",
51
51
  "typescript": "^5.9.3",
52
- "vitest": "^4.0.10",
53
- "zod": "^3.25.76"
52
+ "vitest": "^4.0.18",
53
+ "zod": "^4.3.6"
54
54
  },
55
55
  "peerDependencies": {
56
56
  "mongoose": "^8",
57
- "zod": "^3"
57
+ "zod": "^4"
58
58
  },
59
- "packageManager": "yarn@4.11.0",
59
+ "packageManager": "yarn@4.12.0",
60
60
  "issues": "https://github.com/mirite/zod-to-mongoose/issues"
61
61
  }