@mirite/zod-to-mongoose 0.0.4 → 0.0.5

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.
@@ -0,0 +1,38 @@
1
+ import * as Mongoose from 'mongoose';
2
+ import type { z } from 'zod';
3
+ import type { ZodBoolean } from 'zod';
4
+ import type { ZodDate } from 'zod';
5
+ import type { ZodDefault } from 'zod';
6
+ import type { ZodNumber } from 'zod';
7
+ import type { ZodObject } from 'zod';
8
+ import type { ZodRawShape } from 'zod';
9
+ import type { ZodString } from 'zod';
10
+ import type { ZodTypeAny } from 'zod';
11
+ import type { ZodUnion } from 'zod';
12
+
13
+ export declare type Check = "";
14
+
15
+ export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>, modelName: string, connection: Mongoose.Connection): {
16
+ schema: Mongoose.Schema;
17
+ model: Mongoose.Model<z.infer<typeof zodObject>>;
18
+ };
19
+
20
+ export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Mongoose.Schema;
21
+
22
+ export declare type Field = {
23
+ _def: FieldDefinition;
24
+ checks?: Array<Check>;
25
+ defaultValue?: () => unknown;
26
+ };
27
+
28
+ export declare type FieldDefinition = {
29
+ typeName: string;
30
+ innerType?: Field;
31
+ description: string | undefined;
32
+ };
33
+
34
+ declare type SupportedPrimitive = ZodString | ZodNumber | ZodBoolean | ZodDate;
35
+
36
+ export declare type SupportedType = SupportedPrimitive | ZodDefault<ZodTypeAny> | ZodObject<ZodRawShape> | ZodUnion<readonly [ZodTypeAny, ...ZodTypeAny[]]>;
37
+
38
+ export { }
@@ -0,0 +1,38 @@
1
+ import * as Mongoose from 'mongoose';
2
+ import type { z } from 'zod';
3
+ import type { ZodBoolean } from 'zod';
4
+ import type { ZodDate } from 'zod';
5
+ import type { ZodDefault } from 'zod';
6
+ import type { ZodNumber } from 'zod';
7
+ import type { ZodObject } from 'zod';
8
+ import type { ZodRawShape } from 'zod';
9
+ import type { ZodString } from 'zod';
10
+ import type { ZodTypeAny } from 'zod';
11
+ import type { ZodUnion } from 'zod';
12
+
13
+ export declare type Check = "";
14
+
15
+ export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>, modelName: string, connection: Mongoose.Connection): {
16
+ schema: Mongoose.Schema;
17
+ model: Mongoose.Model<z.infer<typeof zodObject>>;
18
+ };
19
+
20
+ export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Mongoose.Schema;
21
+
22
+ export declare type Field = {
23
+ _def: FieldDefinition;
24
+ checks?: Array<Check>;
25
+ defaultValue?: () => unknown;
26
+ };
27
+
28
+ export declare type FieldDefinition = {
29
+ typeName: string;
30
+ innerType?: Field;
31
+ description: string | undefined;
32
+ };
33
+
34
+ declare type SupportedPrimitive = ZodString | ZodNumber | ZodBoolean | ZodDate;
35
+
36
+ export declare type SupportedType = SupportedPrimitive | ZodDefault<ZodTypeAny> | ZodObject<ZodRawShape> | ZodUnion<readonly [ZodTypeAny, ...ZodTypeAny[]]>;
37
+
38
+ export { }
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var s=Object.create;var d=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var m=Object.getPrototypeOf,h=Object.prototype.hasOwnProperty;var S=(e,o)=>{for(var n in o)d(e,n,{get:o[n],enumerable:!0})},r=(e,o,n,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let a of l(o))!h.call(e,a)&&a!==n&&d(e,a,{get:()=>o[a],enumerable:!(t=u(o,a))||t.enumerable});return e};var b=(e,o,n)=>(n=e!=null?s(m(e)):{},r(o||!e||!e.__esModule?d(n,"default",{value:e,enumerable:!0}):n,e)),Z=e=>r(d({},"__esModule",{value:!0}),e);var w={};S(w,{createSchema:()=>f});module.exports=Z(w);var c=b(require("mongoose"),1);function f(e,o,n){let t={};for(let i in e.shape){let p=e.shape[i];t[i]=T(i,p)}let a=new c.Schema(t);return!n||!o?a:{schema:a,model:n.model(o,a)}}function y(e){return e._def.typeName==="ZodObject"}function T(e,o){let n=g(o),t;switch(n.definition._def.typeName){case"ZodString":t=String;break;case"ZodNumber":t=Number;break;case"ZodBoolean":t=Boolean;break;case"ZodDate":t=Date;break;case"ZodObject":break;case"ZodUnion":t={};break;default:throw new Error(`Unsupported type: ${e}`)}return y(n.definition)&&(t=f(n.definition)),n.defaultValue?{type:t,default:n.defaultValue}:t}function g(e){let o=e,n=!1,t;for(;"innerType"in o._def;)"defaultValue"in o._def&&(t=o._def.defaultValue()),o=o._def.innerType;return{definition:o,optional:n,defaultValue:t}}0&&(module.exports={createSchema});
1
+ "use strict";var s=Object.create;var d=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var m=Object.getPrototypeOf,h=Object.prototype.hasOwnProperty;var S=(e,o)=>{for(var t in o)d(e,t,{get:o[t],enumerable:!0})},r=(e,o,t,n)=>{if(o&&typeof o=="object"||typeof o=="function")for(let a of l(o))!h.call(e,a)&&a!==t&&d(e,a,{get:()=>o[a],enumerable:!(n=u(o,a))||n.enumerable});return e};var y=(e,o,t)=>(t=e!=null?s(m(e)):{},r(o||!e||!e.__esModule?d(t,"default",{value:e,enumerable:!0}):t,e)),b=e=>r(d({},"__esModule",{value:!0}),e);var w={};S(w,{createSchema:()=>p});module.exports=b(w);var c=y(require("mongoose"),1);function p(e,o,t){let n={};for(let i in e.shape){let f=e.shape[i];n[i]=T(i,f)}let a=new c.Schema(n);return!t||!o?a:{schema:a,model:t.model(o,a)}}function Z(e){return e._def.typeName==="ZodObject"}function T(e,o){let t=g(o),n;switch(t.definition._def.typeName){case"ZodString":n=String;break;case"ZodNumber":n=Number;break;case"ZodBoolean":n=Boolean;break;case"ZodDate":n=Date;break;case"ZodObject":break;case"ZodUnion":n={};break;default:throw new TypeError(`Unsupported type: ${e}`)}return Z(t.definition)&&(n=p(t.definition)),t.defaultValue?{type:n,default:t.defaultValue}:n}function g(e){let o=e,t=!1,n;for(;"innerType"in o._def;)"defaultValue"in o._def&&(n=o._def.defaultValue()),o=o._def.innerType;return{definition:o,optional:t,defaultValue:n}}0&&(module.exports={createSchema});
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { z, ZodObject, ZodRawShape } from \"zod\";\nimport * as Mongoose from \"mongoose\";\nimport { SchemaDefinition } from \"mongoose\";\nimport { SupportedType } from \"./types\";\n\nexport function createSchema<T extends ZodRawShape>(\n zodObject: ZodObject<T>,\n modelName: string,\n connection: Mongoose.Connection,\n): { schema: Mongoose.Schema; model: Mongoose.Model<z.infer<typeof zodObject>> };\nexport function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Mongoose.Schema;\n/**\n * Create a Mongoose schema from a Zod shape\n *\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 zodObject: ZodObject<T>,\n modelName?: string,\n connection?: Mongoose.Connection,\n): Mongoose.Schema | { schema: Mongoose.Schema; model: Mongoose.Model<z.infer<typeof zodObject>> } {\n const convertedShape: Partial<SchemaDefinition> = {};\n for (const key in zodObject.shape) {\n const zodField = zodObject.shape[key];\n convertedShape[key] = convertField(key, zodField);\n }\n const schema = new Mongoose.Schema(convertedShape);\n if (!connection || !modelName) return schema;\n return { schema, model: connection.model<z.infer<typeof zodObject>>(modelName, schema) };\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 return definition._def.typeName === \"ZodObject\";\n}\n\n/**\n * Convert a Zod field to a Mongoose type\n *\n * @param type The key of the field\n * @param zodField The Zod field to convert\n * @returns The Mongoose type\n */\nfunction convertField<T extends ZodRawShape>(type: string, zodField: T[Extract<keyof T, string>]) {\n const unwrappedData = unwrapType(zodField);\n let coreType;\n switch (unwrappedData.definition._def.typeName) {\n case \"ZodString\":\n coreType = String;\n break;\n case \"ZodNumber\":\n coreType = Number;\n break;\n case \"ZodBoolean\":\n coreType = Boolean;\n break;\n case \"ZodDate\":\n coreType = Date;\n break;\n case \"ZodObject\":\n break;\n case \"ZodUnion\":\n coreType = {};\n break;\n default:\n throw new Error(`Unsupported type: ${type}`);\n }\n if (isZodObject(unwrappedData.definition)) {\n coreType = createSchema(unwrappedData.definition);\n }\n\n if (!unwrappedData.defaultValue) {\n return coreType;\n }\n return {\n type: coreType,\n default: unwrappedData.defaultValue,\n };\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): { definition: SupportedType; optional: boolean; defaultValue?: unknown } {\n let definition = data;\n const optional = false;\n let defaultValue = undefined;\n while (\"innerType\" in definition._def) {\n if (\"defaultValue\" in definition._def) {\n defaultValue = definition._def.defaultValue();\n }\n definition = definition._def.innerType;\n }\n return { definition, optional, defaultValue };\n}\n"],"mappings":"0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,IAAA,eAAAC,EAAAH,GACA,IAAAI,EAA0B,yBAkBnB,SAASF,EACZG,EACAC,EACAC,EAC+F,CAC/F,IAAMC,EAA4C,CAAC,EACnD,QAAWC,KAAOJ,EAAU,MAAO,CAC/B,IAAMK,EAAWL,EAAU,MAAMI,CAAG,EACpCD,EAAeC,CAAG,EAAIE,EAAaF,EAAKC,CAAQ,CACpD,CACA,IAAME,EAAS,IAAa,SAAOJ,CAAc,EACjD,MAAI,CAACD,GAAc,CAACD,EAAkBM,EAC/B,CAAE,OAAAA,EAAQ,MAAOL,EAAW,MAAiCD,EAAWM,CAAM,CAAE,CAC3F,CAQA,SAASC,EAAYC,EAAiE,CAClF,OAAOA,EAAW,KAAK,WAAa,WACxC,CASA,SAASH,EAAoCI,EAAcL,EAAuC,CAC9F,IAAMM,EAAgBC,EAAWP,CAAQ,EACrCQ,EACJ,OAAQF,EAAc,WAAW,KAAK,SAAU,CAC5C,IAAK,YACDE,EAAW,OACX,MACJ,IAAK,YACDA,EAAW,OACX,MACJ,IAAK,aACDA,EAAW,QACX,MACJ,IAAK,UACDA,EAAW,KACX,MACJ,IAAK,YACD,MACJ,IAAK,WACDA,EAAW,CAAC,EACZ,MACJ,QACI,MAAM,IAAI,MAAM,qBAAqBH,CAAI,EAAE,CACnD,CAKA,OAJIF,EAAYG,EAAc,UAAU,IACpCE,EAAWhB,EAAac,EAAc,UAAU,GAG/CA,EAAc,aAGZ,CACH,KAAME,EACN,QAASF,EAAc,YAC3B,EALWE,CAMf,CAQA,SAASD,EAAWE,EAA+F,CAC/G,IAAIL,EAAaK,EACXC,EAAW,GACbC,EACJ,KAAO,cAAeP,EAAW,MACzB,iBAAkBA,EAAW,OAC7BO,EAAeP,EAAW,KAAK,aAAa,GAEhDA,EAAaA,EAAW,KAAK,UAEjC,MAAO,CAAE,WAAAA,EAAY,SAAAM,EAAU,aAAAC,CAAa,CAChD","names":["src_exports","__export","createSchema","__toCommonJS","Mongoose","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","isZodObject","definition","type","unwrappedData","unwrapType","coreType","data","optional","defaultValue"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import * as Mongoose from \"mongoose\";\nimport type { SchemaDefinition } from \"mongoose\";\nimport type { z, ZodObject, ZodRawShape } from \"zod\";\n\nimport type { SupportedType } from \"./types\";\n\nexport function createSchema<T extends ZodRawShape>(\n zodObject: ZodObject<T>,\n modelName: string,\n connection: Mongoose.Connection,\n): { schema: Mongoose.Schema; model: Mongoose.Model<z.infer<typeof zodObject>> };\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 zodObject: ZodObject<T>,\n modelName?: string,\n connection?: Mongoose.Connection,\n): Mongoose.Schema | { schema: Mongoose.Schema; model: Mongoose.Model<z.infer<typeof zodObject>> } {\n const convertedShape: Partial<SchemaDefinition> = {};\n for (const key in zodObject.shape) {\n const zodField = zodObject.shape[key];\n convertedShape[key] = convertField(key, zodField);\n }\n const schema = new Mongoose.Schema(convertedShape);\n if (!connection || !modelName) return schema;\n return { schema, model: connection.model<z.infer<typeof zodObject>>(modelName, schema) };\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 return definition._def.typeName === \"ZodObject\";\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>]) {\n const unwrappedData = unwrapType(zodField);\n let coreType;\n switch (unwrappedData.definition._def.typeName) {\n case \"ZodString\":\n coreType = String;\n break;\n case \"ZodNumber\":\n coreType = Number;\n break;\n case \"ZodBoolean\":\n coreType = Boolean;\n break;\n case \"ZodDate\":\n coreType = Date;\n break;\n case \"ZodObject\":\n break;\n case \"ZodUnion\":\n coreType = {};\n break;\n default:\n throw new TypeError(`Unsupported type: ${type}`);\n }\n if (isZodObject(unwrappedData.definition)) {\n coreType = createSchema(unwrappedData.definition);\n }\n\n if (!unwrappedData.defaultValue) {\n return coreType;\n }\n return {\n type: coreType,\n default: unwrappedData.defaultValue,\n };\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): { definition: SupportedType; optional: boolean; defaultValue?: unknown } {\n let definition = data;\n const optional = false;\n let defaultValue = undefined;\n while (\"innerType\" in definition._def) {\n if (\"defaultValue\" in definition._def) {\n defaultValue = definition._def.defaultValue();\n }\n definition = definition._def.innerType;\n }\n return { definition, optional, defaultValue };\n}\n"],"mappings":"0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAA0B,yBAqBnB,SAASF,EACZG,EACAC,EACAC,EAC+F,CAC/F,IAAMC,EAA4C,CAAC,EACnD,QAAWC,KAAOJ,EAAU,MAAO,CAC/B,IAAMK,EAAWL,EAAU,MAAMI,CAAG,EACpCD,EAAeC,CAAG,EAAIE,EAAaF,EAAKC,CAAQ,CACpD,CACA,IAAME,EAAS,IAAa,SAAOJ,CAAc,EACjD,MAAI,CAACD,GAAc,CAACD,EAAkBM,EAC/B,CAAE,OAAAA,EAAQ,MAAOL,EAAW,MAAiCD,EAAWM,CAAM,CAAE,CAC3F,CAQA,SAASC,EAAYC,EAAiE,CAClF,OAAOA,EAAW,KAAK,WAAa,WACxC,CAWA,SAASH,EAAoCI,EAAcL,EAAuC,CAC9F,IAAMM,EAAgBC,EAAWP,CAAQ,EACrCQ,EACJ,OAAQF,EAAc,WAAW,KAAK,SAAU,CAC5C,IAAK,YACDE,EAAW,OACX,MACJ,IAAK,YACDA,EAAW,OACX,MACJ,IAAK,aACDA,EAAW,QACX,MACJ,IAAK,UACDA,EAAW,KACX,MACJ,IAAK,YACD,MACJ,IAAK,WACDA,EAAW,CAAC,EACZ,MACJ,QACI,MAAM,IAAI,UAAU,qBAAqBH,CAAI,EAAE,CACvD,CAKA,OAJIF,EAAYG,EAAc,UAAU,IACpCE,EAAWhB,EAAac,EAAc,UAAU,GAG/CA,EAAc,aAGZ,CACH,KAAME,EACN,QAASF,EAAc,YAC3B,EALWE,CAMf,CAQA,SAASD,EAAWE,EAA+F,CAC/G,IAAIL,EAAaK,EACXC,EAAW,GACbC,EACJ,KAAO,cAAeP,EAAW,MACzB,iBAAkBA,EAAW,OAC7BO,EAAeP,EAAW,KAAK,aAAa,GAEhDA,EAAaA,EAAW,KAAK,UAEjC,MAAO,CAAE,WAAAA,EAAY,SAAAM,EAAU,aAAAC,CAAa,CAChD","names":["src_exports","__export","createSchema","__toCommonJS","Mongoose","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","isZodObject","definition","type","unwrappedData","unwrapType","coreType","data","optional","defaultValue"]}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import*as i from"mongoose";function c(t,o,n){let e={};for(let d in t.shape){let r=t.shape[d];e[d]=p(d,r)}let a=new i.Schema(e);return!n||!o?a:{schema:a,model:n.model(o,a)}}function f(t){return t._def.typeName==="ZodObject"}function p(t,o){let n=s(o),e;switch(n.definition._def.typeName){case"ZodString":e=String;break;case"ZodNumber":e=Number;break;case"ZodBoolean":e=Boolean;break;case"ZodDate":e=Date;break;case"ZodObject":break;case"ZodUnion":e={};break;default:throw new Error(`Unsupported type: ${t}`)}return f(n.definition)&&(e=c(n.definition)),n.defaultValue?{type:e,default:n.defaultValue}:e}function s(t){let o=t,n=!1,e;for(;"innerType"in o._def;)"defaultValue"in o._def&&(e=o._def.defaultValue()),o=o._def.innerType;return{definition:o,optional:n,defaultValue:e}}export{c as createSchema};
1
+ import*as i from"mongoose";function c(n,o,t){let e={};for(let d in n.shape){let r=n.shape[d];e[d]=f(d,r)}let a=new i.Schema(e);return!t||!o?a:{schema:a,model:t.model(o,a)}}function p(n){return n._def.typeName==="ZodObject"}function f(n,o){let t=s(o),e;switch(t.definition._def.typeName){case"ZodString":e=String;break;case"ZodNumber":e=Number;break;case"ZodBoolean":e=Boolean;break;case"ZodDate":e=Date;break;case"ZodObject":break;case"ZodUnion":e={};break;default:throw new TypeError(`Unsupported type: ${n}`)}return p(t.definition)&&(e=c(t.definition)),t.defaultValue?{type:e,default:t.defaultValue}:e}function s(n){let o=n,t=!1,e;for(;"innerType"in o._def;)"defaultValue"in o._def&&(e=o._def.defaultValue()),o=o._def.innerType;return{definition:o,optional:t,defaultValue:e}}export{c 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 { z, ZodObject, ZodRawShape } from \"zod\";\nimport * as Mongoose from \"mongoose\";\nimport { SchemaDefinition } from \"mongoose\";\nimport { SupportedType } from \"./types\";\n\nexport function createSchema<T extends ZodRawShape>(\n zodObject: ZodObject<T>,\n modelName: string,\n connection: Mongoose.Connection,\n): { schema: Mongoose.Schema; model: Mongoose.Model<z.infer<typeof zodObject>> };\nexport function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Mongoose.Schema;\n/**\n * Create a Mongoose schema from a Zod shape\n *\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 zodObject: ZodObject<T>,\n modelName?: string,\n connection?: Mongoose.Connection,\n): Mongoose.Schema | { schema: Mongoose.Schema; model: Mongoose.Model<z.infer<typeof zodObject>> } {\n const convertedShape: Partial<SchemaDefinition> = {};\n for (const key in zodObject.shape) {\n const zodField = zodObject.shape[key];\n convertedShape[key] = convertField(key, zodField);\n }\n const schema = new Mongoose.Schema(convertedShape);\n if (!connection || !modelName) return schema;\n return { schema, model: connection.model<z.infer<typeof zodObject>>(modelName, schema) };\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 return definition._def.typeName === \"ZodObject\";\n}\n\n/**\n * Convert a Zod field to a Mongoose type\n *\n * @param type The key of the field\n * @param zodField The Zod field to convert\n * @returns The Mongoose type\n */\nfunction convertField<T extends ZodRawShape>(type: string, zodField: T[Extract<keyof T, string>]) {\n const unwrappedData = unwrapType(zodField);\n let coreType;\n switch (unwrappedData.definition._def.typeName) {\n case \"ZodString\":\n coreType = String;\n break;\n case \"ZodNumber\":\n coreType = Number;\n break;\n case \"ZodBoolean\":\n coreType = Boolean;\n break;\n case \"ZodDate\":\n coreType = Date;\n break;\n case \"ZodObject\":\n break;\n case \"ZodUnion\":\n coreType = {};\n break;\n default:\n throw new Error(`Unsupported type: ${type}`);\n }\n if (isZodObject(unwrappedData.definition)) {\n coreType = createSchema(unwrappedData.definition);\n }\n\n if (!unwrappedData.defaultValue) {\n return coreType;\n }\n return {\n type: coreType,\n default: unwrappedData.defaultValue,\n };\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): { definition: SupportedType; optional: boolean; defaultValue?: unknown } {\n let definition = data;\n const optional = false;\n let defaultValue = undefined;\n while (\"innerType\" in definition._def) {\n if (\"defaultValue\" in definition._def) {\n defaultValue = definition._def.defaultValue();\n }\n definition = definition._def.innerType;\n }\n return { definition, optional, defaultValue };\n}\n"],"mappings":"AACA,UAAYA,MAAc,WAkBnB,SAASC,EACZC,EACAC,EACAC,EAC+F,CAC/F,IAAMC,EAA4C,CAAC,EACnD,QAAWC,KAAOJ,EAAU,MAAO,CAC/B,IAAMK,EAAWL,EAAU,MAAMI,CAAG,EACpCD,EAAeC,CAAG,EAAIE,EAAaF,EAAKC,CAAQ,CACpD,CACA,IAAME,EAAS,IAAa,SAAOJ,CAAc,EACjD,MAAI,CAACD,GAAc,CAACD,EAAkBM,EAC/B,CAAE,OAAAA,EAAQ,MAAOL,EAAW,MAAiCD,EAAWM,CAAM,CAAE,CAC3F,CAQA,SAASC,EAAYC,EAAiE,CAClF,OAAOA,EAAW,KAAK,WAAa,WACxC,CASA,SAASH,EAAoCI,EAAcL,EAAuC,CAC9F,IAAMM,EAAgBC,EAAWP,CAAQ,EACrCQ,EACJ,OAAQF,EAAc,WAAW,KAAK,SAAU,CAC5C,IAAK,YACDE,EAAW,OACX,MACJ,IAAK,YACDA,EAAW,OACX,MACJ,IAAK,aACDA,EAAW,QACX,MACJ,IAAK,UACDA,EAAW,KACX,MACJ,IAAK,YACD,MACJ,IAAK,WACDA,EAAW,CAAC,EACZ,MACJ,QACI,MAAM,IAAI,MAAM,qBAAqBH,CAAI,EAAE,CACnD,CAKA,OAJIF,EAAYG,EAAc,UAAU,IACpCE,EAAWd,EAAaY,EAAc,UAAU,GAG/CA,EAAc,aAGZ,CACH,KAAME,EACN,QAASF,EAAc,YAC3B,EALWE,CAMf,CAQA,SAASD,EAAWE,EAA+F,CAC/G,IAAIL,EAAaK,EACXC,EAAW,GACbC,EACJ,KAAO,cAAeP,EAAW,MACzB,iBAAkBA,EAAW,OAC7BO,EAAeP,EAAW,KAAK,aAAa,GAEhDA,EAAaA,EAAW,KAAK,UAEjC,MAAO,CAAE,WAAAA,EAAY,SAAAM,EAAU,aAAAC,CAAa,CAChD","names":["Mongoose","createSchema","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","isZodObject","definition","type","unwrappedData","unwrapType","coreType","data","optional","defaultValue"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import * as Mongoose from \"mongoose\";\nimport type { SchemaDefinition } from \"mongoose\";\nimport type { z, ZodObject, ZodRawShape } from \"zod\";\n\nimport type { SupportedType } from \"./types\";\n\nexport function createSchema<T extends ZodRawShape>(\n zodObject: ZodObject<T>,\n modelName: string,\n connection: Mongoose.Connection,\n): { schema: Mongoose.Schema; model: Mongoose.Model<z.infer<typeof zodObject>> };\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 zodObject: ZodObject<T>,\n modelName?: string,\n connection?: Mongoose.Connection,\n): Mongoose.Schema | { schema: Mongoose.Schema; model: Mongoose.Model<z.infer<typeof zodObject>> } {\n const convertedShape: Partial<SchemaDefinition> = {};\n for (const key in zodObject.shape) {\n const zodField = zodObject.shape[key];\n convertedShape[key] = convertField(key, zodField);\n }\n const schema = new Mongoose.Schema(convertedShape);\n if (!connection || !modelName) return schema;\n return { schema, model: connection.model<z.infer<typeof zodObject>>(modelName, schema) };\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 return definition._def.typeName === \"ZodObject\";\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>]) {\n const unwrappedData = unwrapType(zodField);\n let coreType;\n switch (unwrappedData.definition._def.typeName) {\n case \"ZodString\":\n coreType = String;\n break;\n case \"ZodNumber\":\n coreType = Number;\n break;\n case \"ZodBoolean\":\n coreType = Boolean;\n break;\n case \"ZodDate\":\n coreType = Date;\n break;\n case \"ZodObject\":\n break;\n case \"ZodUnion\":\n coreType = {};\n break;\n default:\n throw new TypeError(`Unsupported type: ${type}`);\n }\n if (isZodObject(unwrappedData.definition)) {\n coreType = createSchema(unwrappedData.definition);\n }\n\n if (!unwrappedData.defaultValue) {\n return coreType;\n }\n return {\n type: coreType,\n default: unwrappedData.defaultValue,\n };\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): { definition: SupportedType; optional: boolean; defaultValue?: unknown } {\n let definition = data;\n const optional = false;\n let defaultValue = undefined;\n while (\"innerType\" in definition._def) {\n if (\"defaultValue\" in definition._def) {\n defaultValue = definition._def.defaultValue();\n }\n definition = definition._def.innerType;\n }\n return { definition, optional, defaultValue };\n}\n"],"mappings":"AAAA,UAAYA,MAAc,WAqBnB,SAASC,EACZC,EACAC,EACAC,EAC+F,CAC/F,IAAMC,EAA4C,CAAC,EACnD,QAAWC,KAAOJ,EAAU,MAAO,CAC/B,IAAMK,EAAWL,EAAU,MAAMI,CAAG,EACpCD,EAAeC,CAAG,EAAIE,EAAaF,EAAKC,CAAQ,CACpD,CACA,IAAME,EAAS,IAAa,SAAOJ,CAAc,EACjD,MAAI,CAACD,GAAc,CAACD,EAAkBM,EAC/B,CAAE,OAAAA,EAAQ,MAAOL,EAAW,MAAiCD,EAAWM,CAAM,CAAE,CAC3F,CAQA,SAASC,EAAYC,EAAiE,CAClF,OAAOA,EAAW,KAAK,WAAa,WACxC,CAWA,SAASH,EAAoCI,EAAcL,EAAuC,CAC9F,IAAMM,EAAgBC,EAAWP,CAAQ,EACrCQ,EACJ,OAAQF,EAAc,WAAW,KAAK,SAAU,CAC5C,IAAK,YACDE,EAAW,OACX,MACJ,IAAK,YACDA,EAAW,OACX,MACJ,IAAK,aACDA,EAAW,QACX,MACJ,IAAK,UACDA,EAAW,KACX,MACJ,IAAK,YACD,MACJ,IAAK,WACDA,EAAW,CAAC,EACZ,MACJ,QACI,MAAM,IAAI,UAAU,qBAAqBH,CAAI,EAAE,CACvD,CAKA,OAJIF,EAAYG,EAAc,UAAU,IACpCE,EAAWd,EAAaY,EAAc,UAAU,GAG/CA,EAAc,aAGZ,CACH,KAAME,EACN,QAASF,EAAc,YAC3B,EALWE,CAMf,CAQA,SAASD,EAAWE,EAA+F,CAC/G,IAAIL,EAAaK,EACXC,EAAW,GACbC,EACJ,KAAO,cAAeP,EAAW,MACzB,iBAAkBA,EAAW,OAC7BO,EAAeP,EAAW,KAAK,aAAa,GAEhDA,EAAaA,EAAW,KAAK,UAEjC,MAAO,CAAE,WAAAA,EAAY,SAAAM,EAAU,aAAAC,CAAa,CAChD","names":["Mongoose","createSchema","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","isZodObject","definition","type","unwrappedData","unwrapType","coreType","data","optional","defaultValue"]}
@@ -0,0 +1 @@
1
+ export { createSchema } from '../_tsup-dts-rollup';
@@ -0,0 +1 @@
1
+ export { createSchema } from '../_tsup-dts-rollup';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mirite/zod-to-mongoose",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Package for creating mongoose schemas out of Zod schemas for type safety, and removing duplicate effort.",
5
5
  "types": "./dist/_tsup-dts-rollup.d.ts",
6
6
  "type": "module",
@@ -33,29 +33,27 @@
33
33
  "license": "MIT",
34
34
  "scripts": {
35
35
  "build": "tsup",
36
- "test": "jest --coverage",
37
- "lint": "eslint . --fix --ignore-path .gitignore",
38
- "format": "prettier --write . --ignore-path .gitignore",
36
+ "test": "vitest --run --coverage",
37
+ "test:watch": "vitest --ui --coverage",
38
+ "lint": "eslint --fix",
39
+ "format": "prettier --write .",
39
40
  "release": "yarn build && npm publish"
40
41
  },
41
42
  "devDependencies": {
42
- "@types/eslint": "^8.56.10",
43
- "@types/jest": "^29.5.12",
44
- "@typescript-eslint/eslint-plugin": "^7.13.1",
45
- "@typescript-eslint/parser": "^7.13.1",
46
- "eslint": "^8.57.0",
47
- "eslint-plugin-jsdoc": "^48.2.12",
48
- "jest": "^29.7.0",
49
- "prettier": "^3.3.2",
43
+ "@microsoft/api-extractor": "^7.47.5",
44
+ "@mirite/eslint-config-mirite": "^0.0.4",
45
+ "@vitest/coverage-v8": "^2.0.5",
46
+ "@vitest/ui": "^2.0.5",
47
+ "eslint": "^9.9.0",
48
+ "prettier": "^3.3.3",
50
49
  "prettier-plugin-jsdoc": "^1.3.0",
51
- "ts-jest": "^29.1.5",
52
- "tsup": "^8.1.0",
53
- "typescript": "^5.4.5"
50
+ "tsup": "^8.2.4",
51
+ "typescript": "^5.5.4",
52
+ "vitest": "^2.0.5"
54
53
  },
55
- "packageManager": "yarn@4.3.0",
54
+ "packageManager": "yarn@4.4.0",
56
55
  "dependencies": {
57
- "@microsoft/api-extractor": "^7.47.0",
58
- "mongoose": "^8.4.3",
56
+ "mongoose": "^8.5.2",
59
57
  "zod": "^3.23.8"
60
58
  }
61
59
  }
package/readme.md CHANGED
@@ -13,6 +13,7 @@ yarn add @mirite/zod-to-mongoose
13
13
  ### Basic
14
14
 
15
15
  Passing in a Zod schema will return a Mongoose schema.
16
+
16
17
  ```typescript
17
18
  import { createSchema } from "@mirite/zod-to-mongoose";
18
19
 
@@ -30,14 +31,15 @@ const result = await model.create({
30
31
  isHappy: true,
31
32
  birthday: new Date("1980-01-01"),
32
33
  });
33
- ````
34
+ ```
34
35
 
35
36
  ### Advanced
37
+
36
38
  If you pass a connection, the model will be created, registered with the connection, and returned for use.
39
+
37
40
  ```typescript
38
41
  import { createSchema } from "@mirite/zod-to-mongoose";
39
42
 
40
-
41
43
  const model = createSchema(
42
44
  z.object({
43
45
  name: z.string().optional().default("Bob"),
@@ -56,12 +58,14 @@ const result = await model.create({
56
58
  ```
57
59
 
58
60
  ### Caveats
61
+
59
62
  Only a subset of Zod types are supported. The following types are supported:
60
- - `z.string()`
61
- - `z.number()`
62
- - `z.boolean()`
63
- - `z.date()`
64
- - `z.object()` (Nested objects are supported)
65
- - `z.union()` (Type safety is not guaranteed)
63
+
64
+ - `z.string()`
65
+ - `z.number()`
66
+ - `z.boolean()`
67
+ - `z.date()`
68
+ - `z.object()` (Nested objects are supported)
69
+ - `z.union()` (Type safety is not guaranteed)
66
70
 
67
71
  Another major caveat is that no additional validation is performed on the Mongoose schema. It is recommended to use Zod for validation before saving to the database, as well as on retrieval if there are data integrity concerns.