@mirite/zod-to-mongoose 0.1.1 → 0.2.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/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 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]=Z(i,f)}let a=new c.Schema(n);return!t||!o?a:{model:t.model(o,a),schema:a}}function Z(e,o){let t=g(o),n;switch(t.definition._def.typeName){case"ZodBoolean":n=Boolean;break;case"ZodDate":n=Date;break;case"ZodNumber":n=Number;break;case"ZodObject":break;case"ZodString":n=String;break;case"ZodUnion":n={};break;default:throw new TypeError(`Unsupported type: ${e}`)}return T(t.definition)&&(n=p(t.definition)),t.defaultValue?{default:t.defaultValue,type:n}:n}function T(e){return e._def.typeName==="ZodObject"}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{defaultValue:n,definition:o,optional:t}}0&&(module.exports={createSchema});
1
+ "use strict";var y=Object.create;var i=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var b=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var Z=(e,o)=>{for(var t in o)i(e,t,{get:o[t],enumerable:!0})},f=(e,o,t,n)=>{if(o&&typeof o=="object"||typeof o=="function")for(let a of S(o))!T.call(e,a)&&a!==t&&i(e,a,{get:()=>o[a],enumerable:!(n=h(o,a))||n.enumerable});return e};var g=(e,o,t)=>(t=e!=null?y(b(e)):{},f(o||!e||!e.__esModule?i(t,"default",{value:e,enumerable:!0}):t,e)),w=e=>f(i({},"__esModule",{value:!0}),e);var M={};Z(M,{createSchema:()=>m});module.exports=w(M);var l=g(require("mongoose"),1);function m(e,o,t){let n={};for(let r in e.shape){let d=e.shape[r];n[r]=p(r,d)}let a=new l.Schema(n);return!t||!o?a:{model:t.model(o,a),schema:a}}function p(e,o){let t=j(o),n;switch(t.definition._def.typeName){case"ZodArray":{let r=t.definition._def.type;if(u(r)){let d=r.shape,s={};for(let c in d)s[c]=p(c,d[c]);n=[s]}else n=[p(e,r)];break}case"ZodBoolean":n=Boolean;break;case"ZodDate":n=Date;break;case"ZodNumber":n=Number;break;case"ZodObject":break;case"ZodString":n=String;break;case"ZodUnion":n={};break;default:throw new TypeError(`Unsupported type: ${e}`)}return u(t.definition)&&(n=m(t.definition)),t.defaultValue?{default:t.defaultValue,type:n}:n}function u(e){return e._def.typeName==="ZodObject"}function j(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{defaultValue:n,definition:o,optional:t}}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 type { z, ZodObject, ZodRawShape } from \"zod\";\n\nimport * as Mongoose from \"mongoose\";\n\nimport type { SupportedType } from \"./types\";\n\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>]) {\n\tconst unwrappedData = unwrapType(zodField);\n\tlet coreType;\n\tswitch (unwrappedData.definition._def.typeName) {\n\t\tcase \"ZodBoolean\":\n\t\t\tcoreType = Boolean;\n\t\t\tbreak;\n\t\tcase \"ZodDate\":\n\t\t\tcoreType = Date;\n\t\t\tbreak;\n\t\tcase \"ZodNumber\":\n\t\t\tcoreType = Number;\n\t\t\tbreak;\n\t\tcase \"ZodObject\":\n\t\t\tbreak;\n\t\tcase \"ZodString\":\n\t\t\tcoreType = String;\n\t\t\tbreak;\n\t\tcase \"ZodUnion\":\n\t\t\tcoreType = {};\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\n\tif (!unwrappedData.defaultValue) {\n\t\treturn coreType;\n\t}\n\treturn {\n\t\tdefault: unwrappedData.defaultValue,\n\t\ttype: coreType,\n\t};\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 === \"ZodObject\";\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): { defaultValue?: unknown; definition: SupportedType; optional: boolean; } {\n\tlet definition = data;\n\tconst optional = false;\n\tlet defaultValue = undefined;\n\twhile (\"innerType\" in definition._def) {\n\t\tif (\"defaultValue\" in definition._def) {\n\t\t\tdefaultValue = definition._def.defaultValue();\n\t\t}\n\t\tdefinition = definition._def.innerType;\n\t}\n\treturn { defaultValue, definition, optional };\n}\n"],"mappings":"0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,IAAA,eAAAC,EAAAH,GAGA,IAAAI,EAA0B,yBAmBnB,SAASF,EACfG,EACAC,EACAC,EACmG,CACnG,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,EAAuC,CACjG,IAAMI,EAAgBC,EAAWL,CAAQ,EACrCM,EACJ,OAAQF,EAAc,WAAW,KAAK,SAAU,CAC/C,IAAK,aACJE,EAAW,QACX,MACD,IAAK,UACJA,EAAW,KACX,MACD,IAAK,YACJA,EAAW,OACX,MACD,IAAK,YACJ,MACD,IAAK,YACJA,EAAW,OACX,MACD,IAAK,WACJA,EAAW,CAAC,EACZ,MACD,QACC,MAAM,IAAI,UAAU,qBAAqBH,CAAI,EAAE,CACjD,CAKA,OAJII,EAAYH,EAAc,UAAU,IACvCE,EAAWd,EAAaY,EAAc,UAAU,GAG5CA,EAAc,aAGZ,CACN,QAASA,EAAc,aACvB,KAAME,CACP,EALQA,CAMT,CAQA,SAASC,EAAYC,EAAiE,CACrF,OAAOA,EAAW,KAAK,WAAa,WACrC,CAQA,SAASH,EAAWI,EAAgG,CACnH,IAAID,EAAaC,EACXC,EAAW,GACbC,EACJ,KAAO,cAAeH,EAAW,MAC5B,iBAAkBA,EAAW,OAChCG,EAAeH,EAAW,KAAK,aAAa,GAE7CA,EAAaA,EAAW,KAAK,UAE9B,MAAO,CAAE,aAAAG,EAAc,WAAAH,EAAY,SAAAE,CAAS,CAC7C","names":["index_exports","__export","createSchema","__toCommonJS","Mongoose","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","type","unwrappedData","unwrapType","coreType","isZodObject","definition","data","optional","defaultValue"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { SchemaDefinition } from \"mongoose\";\nimport type { z, ZodArray, ZodObject, ZodRawShape } from \"zod\";\n\nimport * as Mongoose from \"mongoose\";\n\nimport type { SupportedType } from \"./types\";\n\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>]) {\n\tconst unwrappedData = unwrapType(zodField);\n\tlet coreType;\n\tswitch (unwrappedData.definition._def.typeName) {\n\t\tcase \"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: SchemaDefinition = {};\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 \"ZodBoolean\":\n\t\t\tcoreType = Boolean;\n\t\t\tbreak;\n\t\tcase \"ZodDate\":\n\t\t\tcoreType = Date;\n\t\t\tbreak;\n\t\tcase \"ZodNumber\":\n\t\t\tcoreType = Number;\n\t\t\tbreak;\n\t\tcase \"ZodObject\":\n\t\t\tbreak;\n\t\tcase \"ZodString\":\n\t\t\tcoreType = String;\n\t\t\tbreak;\n\t\tcase \"ZodUnion\":\n\t\t\tcoreType = {};\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\n\tif (!unwrappedData.defaultValue) {\n\t\treturn coreType;\n\t}\n\treturn {\n\t\tdefault: unwrappedData.defaultValue,\n\t\ttype: coreType,\n\t};\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 === \"ZodObject\";\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): { defaultValue?: unknown; definition: SupportedType; optional: boolean; } {\n\tlet definition = data;\n\tconst optional = false;\n\tlet defaultValue = undefined;\n\twhile (\"innerType\" in definition._def) {\n\t\tif (\"defaultValue\" in definition._def) {\n\t\t\tdefaultValue = definition._def.defaultValue();\n\t\t}\n\t\tdefinition = definition._def.innerType;\n\t}\n\treturn { defaultValue, definition, optional };\n}\n"],"mappings":"0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,IAAA,eAAAC,EAAAH,GAGA,IAAAI,EAA0B,yBAmBnB,SAASF,EACfG,EACAC,EACAC,EACmG,CACnG,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,EAAuC,CACjG,IAAMI,EAAgBC,EAAWL,CAAQ,EACrCM,EACJ,OAAQF,EAAc,WAAW,KAAK,SAAU,CAC/C,IAAK,WAAY,CAEhB,IAAMG,EADYH,EAAc,WACF,KAAK,KACnC,GAAII,EAAYD,CAAW,EAAG,CAC7B,IAAME,EAAQF,EAAY,MACpBT,EAAmC,CAAC,EAC1C,QAAWC,KAAOU,EACjBX,EAAeC,CAAG,EAAIE,EAAaF,EAAKU,EAAMV,CAAG,CAAC,EAEnDO,EAAW,CAACR,CAAc,CAC3B,MACCQ,EAAW,CAACL,EAAaE,EAAMI,CAAW,CAAC,EAE5C,KACD,CACA,IAAK,aACJD,EAAW,QACX,MACD,IAAK,UACJA,EAAW,KACX,MACD,IAAK,YACJA,EAAW,OACX,MACD,IAAK,YACJ,MACD,IAAK,YACJA,EAAW,OACX,MACD,IAAK,WACJA,EAAW,CAAC,EACZ,MACD,QACC,MAAM,IAAI,UAAU,qBAAqBH,CAAI,EAAE,CACjD,CAKA,OAJIK,EAAYJ,EAAc,UAAU,IACvCE,EAAWd,EAAaY,EAAc,UAAU,GAG5CA,EAAc,aAGZ,CACN,QAASA,EAAc,aACvB,KAAME,CACP,EALQA,CAMT,CAQA,SAASE,EAAYE,EAAiE,CACrF,OAAOA,EAAW,KAAK,WAAa,WACrC,CAQA,SAASL,EAAWM,EAAgG,CACnH,IAAID,EAAaC,EACXC,EAAW,GACbC,EACJ,KAAO,cAAeH,EAAW,MAC5B,iBAAkBA,EAAW,OAChCG,EAAeH,EAAW,KAAK,aAAa,GAE7CA,EAAaA,EAAW,KAAK,UAE9B,MAAO,CAAE,aAAAG,EAAc,WAAAH,EAAY,SAAAE,CAAS,CAC7C","names":["index_exports","__export","createSchema","__toCommonJS","Mongoose","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","type","unwrappedData","unwrapType","coreType","elementType","isZodObject","shape","definition","data","optional","defaultValue"]}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
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]=p(d,r)}let a=new i.Schema(e);return!t||!o?a:{model:t.model(o,a),schema:a}}function p(n,o){let t=s(o),e;switch(t.definition._def.typeName){case"ZodBoolean":e=Boolean;break;case"ZodDate":e=Date;break;case"ZodNumber":e=Number;break;case"ZodObject":break;case"ZodString":e=String;break;case"ZodUnion":e={};break;default:throw new TypeError(`Unsupported type: ${n}`)}return f(t.definition)&&(e=c(t.definition)),t.defaultValue?{default:t.defaultValue,type:e}:e}function f(n){return n._def.typeName==="ZodObject"}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{defaultValue:e,definition:o,optional:t}}export{c as createSchema};
1
+ import*as f from"mongoose";function u(n,o,t){let e={};for(let a in n.shape){let d=n.shape[a];e[a]=c(a,d)}let r=new f.Schema(e);return!t||!o?r:{model:t.model(o,r),schema:r}}function c(n,o){let t=l(o),e;switch(t.definition._def.typeName){case"ZodArray":{let a=t.definition._def.type;if(s(a)){let d=a.shape,p={};for(let i in d)p[i]=c(i,d[i]);e=[p]}else e=[c(n,a)];break}case"ZodBoolean":e=Boolean;break;case"ZodDate":e=Date;break;case"ZodNumber":e=Number;break;case"ZodObject":break;case"ZodString":e=String;break;case"ZodUnion":e={};break;default:throw new TypeError(`Unsupported type: ${n}`)}return s(t.definition)&&(e=u(t.definition)),t.defaultValue?{default:t.defaultValue,type:e}:e}function s(n){return n._def.typeName==="ZodObject"}function l(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{defaultValue:e,definition:o,optional:t}}export{u 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 type { z, ZodObject, ZodRawShape } from \"zod\";\n\nimport * as Mongoose from \"mongoose\";\n\nimport type { SupportedType } from \"./types\";\n\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>]) {\n\tconst unwrappedData = unwrapType(zodField);\n\tlet coreType;\n\tswitch (unwrappedData.definition._def.typeName) {\n\t\tcase \"ZodBoolean\":\n\t\t\tcoreType = Boolean;\n\t\t\tbreak;\n\t\tcase \"ZodDate\":\n\t\t\tcoreType = Date;\n\t\t\tbreak;\n\t\tcase \"ZodNumber\":\n\t\t\tcoreType = Number;\n\t\t\tbreak;\n\t\tcase \"ZodObject\":\n\t\t\tbreak;\n\t\tcase \"ZodString\":\n\t\t\tcoreType = String;\n\t\t\tbreak;\n\t\tcase \"ZodUnion\":\n\t\t\tcoreType = {};\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\n\tif (!unwrappedData.defaultValue) {\n\t\treturn coreType;\n\t}\n\treturn {\n\t\tdefault: unwrappedData.defaultValue,\n\t\ttype: coreType,\n\t};\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 === \"ZodObject\";\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): { defaultValue?: unknown; definition: SupportedType; optional: boolean; } {\n\tlet definition = data;\n\tconst optional = false;\n\tlet defaultValue = undefined;\n\twhile (\"innerType\" in definition._def) {\n\t\tif (\"defaultValue\" in definition._def) {\n\t\t\tdefaultValue = definition._def.defaultValue();\n\t\t}\n\t\tdefinition = definition._def.innerType;\n\t}\n\treturn { defaultValue, definition, optional };\n}\n"],"mappings":"AAGA,UAAYA,MAAc,WAmBnB,SAASC,EACfC,EACAC,EACAC,EACmG,CACnG,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,EAAuC,CACjG,IAAMI,EAAgBC,EAAWL,CAAQ,EACrCM,EACJ,OAAQF,EAAc,WAAW,KAAK,SAAU,CAC/C,IAAK,aACJE,EAAW,QACX,MACD,IAAK,UACJA,EAAW,KACX,MACD,IAAK,YACJA,EAAW,OACX,MACD,IAAK,YACJ,MACD,IAAK,YACJA,EAAW,OACX,MACD,IAAK,WACJA,EAAW,CAAC,EACZ,MACD,QACC,MAAM,IAAI,UAAU,qBAAqBH,CAAI,EAAE,CACjD,CAKA,OAJII,EAAYH,EAAc,UAAU,IACvCE,EAAWZ,EAAaU,EAAc,UAAU,GAG5CA,EAAc,aAGZ,CACN,QAASA,EAAc,aACvB,KAAME,CACP,EALQA,CAMT,CAQA,SAASC,EAAYC,EAAiE,CACrF,OAAOA,EAAW,KAAK,WAAa,WACrC,CAQA,SAASH,EAAWI,EAAgG,CACnH,IAAID,EAAaC,EACXC,EAAW,GACbC,EACJ,KAAO,cAAeH,EAAW,MAC5B,iBAAkBA,EAAW,OAChCG,EAAeH,EAAW,KAAK,aAAa,GAE7CA,EAAaA,EAAW,KAAK,UAE9B,MAAO,CAAE,aAAAG,EAAc,WAAAH,EAAY,SAAAE,CAAS,CAC7C","names":["Mongoose","createSchema","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","type","unwrappedData","unwrapType","coreType","isZodObject","definition","data","optional","defaultValue"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { SchemaDefinition } from \"mongoose\";\nimport type { z, ZodArray, ZodObject, ZodRawShape } from \"zod\";\n\nimport * as Mongoose from \"mongoose\";\n\nimport type { SupportedType } from \"./types\";\n\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>]) {\n\tconst unwrappedData = unwrapType(zodField);\n\tlet coreType;\n\tswitch (unwrappedData.definition._def.typeName) {\n\t\tcase \"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: SchemaDefinition = {};\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 \"ZodBoolean\":\n\t\t\tcoreType = Boolean;\n\t\t\tbreak;\n\t\tcase \"ZodDate\":\n\t\t\tcoreType = Date;\n\t\t\tbreak;\n\t\tcase \"ZodNumber\":\n\t\t\tcoreType = Number;\n\t\t\tbreak;\n\t\tcase \"ZodObject\":\n\t\t\tbreak;\n\t\tcase \"ZodString\":\n\t\t\tcoreType = String;\n\t\t\tbreak;\n\t\tcase \"ZodUnion\":\n\t\t\tcoreType = {};\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\n\tif (!unwrappedData.defaultValue) {\n\t\treturn coreType;\n\t}\n\treturn {\n\t\tdefault: unwrappedData.defaultValue,\n\t\ttype: coreType,\n\t};\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 === \"ZodObject\";\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): { defaultValue?: unknown; definition: SupportedType; optional: boolean; } {\n\tlet definition = data;\n\tconst optional = false;\n\tlet defaultValue = undefined;\n\twhile (\"innerType\" in definition._def) {\n\t\tif (\"defaultValue\" in definition._def) {\n\t\t\tdefaultValue = definition._def.defaultValue();\n\t\t}\n\t\tdefinition = definition._def.innerType;\n\t}\n\treturn { defaultValue, definition, optional };\n}\n"],"mappings":"AAGA,UAAYA,MAAc,WAmBnB,SAASC,EACfC,EACAC,EACAC,EACmG,CACnG,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,EAAuC,CACjG,IAAMI,EAAgBC,EAAWL,CAAQ,EACrCM,EACJ,OAAQF,EAAc,WAAW,KAAK,SAAU,CAC/C,IAAK,WAAY,CAEhB,IAAMG,EADYH,EAAc,WACF,KAAK,KACnC,GAAII,EAAYD,CAAW,EAAG,CAC7B,IAAME,EAAQF,EAAY,MACpBT,EAAmC,CAAC,EAC1C,QAAWC,KAAOU,EACjBX,EAAeC,CAAG,EAAIE,EAAaF,EAAKU,EAAMV,CAAG,CAAC,EAEnDO,EAAW,CAACR,CAAc,CAC3B,MACCQ,EAAW,CAACL,EAAaE,EAAMI,CAAW,CAAC,EAE5C,KACD,CACA,IAAK,aACJD,EAAW,QACX,MACD,IAAK,UACJA,EAAW,KACX,MACD,IAAK,YACJA,EAAW,OACX,MACD,IAAK,YACJ,MACD,IAAK,YACJA,EAAW,OACX,MACD,IAAK,WACJA,EAAW,CAAC,EACZ,MACD,QACC,MAAM,IAAI,UAAU,qBAAqBH,CAAI,EAAE,CACjD,CAKA,OAJIK,EAAYJ,EAAc,UAAU,IACvCE,EAAWZ,EAAaU,EAAc,UAAU,GAG5CA,EAAc,aAGZ,CACN,QAASA,EAAc,aACvB,KAAME,CACP,EALQA,CAMT,CAQA,SAASE,EAAYE,EAAiE,CACrF,OAAOA,EAAW,KAAK,WAAa,WACrC,CAQA,SAASL,EAAWM,EAAgG,CACnH,IAAID,EAAaC,EACXC,EAAW,GACbC,EACJ,KAAO,cAAeH,EAAW,MAC5B,iBAAkBA,EAAW,OAChCG,EAAeH,EAAW,KAAK,aAAa,GAE7CA,EAAaA,EAAW,KAAK,UAE9B,MAAO,CAAE,aAAAG,EAAc,WAAAH,EAAY,SAAAE,CAAS,CAC7C","names":["Mongoose","createSchema","zodObject","modelName","connection","convertedShape","key","zodField","convertField","schema","type","unwrappedData","unwrapType","coreType","elementType","isZodObject","shape","definition","data","optional","defaultValue"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mirite/zod-to-mongoose",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
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,20 +40,20 @@
40
40
  "test:watch": "vitest --ui --coverage"
41
41
  },
42
42
  "dependencies": {
43
- "mongoose": "^8.16.3",
43
+ "mongoose": "^8.18.2",
44
44
  "zod": "^3.25.76"
45
45
  },
46
46
  "devDependencies": {
47
- "@microsoft/api-extractor": "^7.52.8",
48
- "@mirite/eslint-config-mirite": "^1.1.4",
47
+ "@microsoft/api-extractor": "^7.52.13",
48
+ "@mirite/eslint-config-mirite": "^1.3.1",
49
49
  "@vitest/coverage-v8": "^3.2.4",
50
50
  "@vitest/ui": "^3.2.4",
51
- "eslint": "^9.30.1",
51
+ "eslint": "^9.36.0",
52
52
  "prettier": "^3.6.2",
53
53
  "tsup": "^8.5.0",
54
- "typescript": "^5.8.3",
54
+ "typescript": "^5.9.2",
55
55
  "vitest": "^3.2.4"
56
56
  },
57
- "packageManager": "yarn@4.9.2",
57
+ "packageManager": "yarn@4.10.3",
58
58
  "issues": "https://github.com/mirite/zod-to-mongoose/issues"
59
59
  }
package/readme.md CHANGED
@@ -66,6 +66,7 @@ Only a subset of Zod types are supported. The following types are supported:
66
66
  - `z.boolean()`
67
67
  - `z.date()`
68
68
  - `z.object()` (Nested objects are supported)
69
+ - `z.array()` (Arrays of primitives and objects are supported)
69
70
  - `z.union()` (Type safety is not guaranteed)
70
71
 
71
72
  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.