@mirite/zod-to-mongoose 0.1.0 → 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/_tsup-dts-rollup.d.cts +4 -4
- package/dist/_tsup-dts-rollup.d.ts +4 -4
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +57 -58
- package/readme.md +19 -18
|
@@ -13,8 +13,8 @@ import type { ZodUnion } from 'zod';
|
|
|
13
13
|
export declare type Check = "";
|
|
14
14
|
|
|
15
15
|
export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>, modelName: string, connection: Mongoose.Connection): {
|
|
16
|
-
schema: Mongoose.Schema;
|
|
17
16
|
model: Mongoose.Model<z.infer<typeof zodObject>>;
|
|
17
|
+
schema: Mongoose.Schema;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Mongoose.Schema;
|
|
@@ -26,12 +26,12 @@ export declare type Field = {
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
export declare type FieldDefinition = {
|
|
29
|
-
typeName: string;
|
|
30
|
-
innerType?: Field;
|
|
31
29
|
description: string | undefined;
|
|
30
|
+
innerType?: Field;
|
|
31
|
+
typeName: string;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
declare type SupportedPrimitive =
|
|
34
|
+
declare type SupportedPrimitive = ZodBoolean | ZodDate | ZodNumber | ZodString;
|
|
35
35
|
|
|
36
36
|
export declare type SupportedType = SupportedPrimitive | ZodDefault<ZodTypeAny> | ZodObject<ZodRawShape> | ZodUnion<readonly [ZodTypeAny, ...ZodTypeAny[]]>;
|
|
37
37
|
|
|
@@ -13,8 +13,8 @@ import type { ZodUnion } from 'zod';
|
|
|
13
13
|
export declare type Check = "";
|
|
14
14
|
|
|
15
15
|
export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>, modelName: string, connection: Mongoose.Connection): {
|
|
16
|
-
schema: Mongoose.Schema;
|
|
17
16
|
model: Mongoose.Model<z.infer<typeof zodObject>>;
|
|
17
|
+
schema: Mongoose.Schema;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
export declare function createSchema<T extends ZodRawShape>(zodObject: ZodObject<T>): Mongoose.Schema;
|
|
@@ -26,12 +26,12 @@ export declare type Field = {
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
export declare type FieldDefinition = {
|
|
29
|
-
typeName: string;
|
|
30
|
-
innerType?: Field;
|
|
31
29
|
description: string | undefined;
|
|
30
|
+
innerType?: Field;
|
|
31
|
+
typeName: string;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
declare type SupportedPrimitive =
|
|
34
|
+
declare type SupportedPrimitive = ZodBoolean | ZodDate | ZodNumber | ZodString;
|
|
35
35
|
|
|
36
36
|
export declare type SupportedType = SupportedPrimitive | ZodDefault<ZodTypeAny> | ZodObject<ZodRawShape> | ZodUnion<readonly [ZodTypeAny, ...ZodTypeAny[]]>;
|
|
37
37
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
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
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import
|
|
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
|
|
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
|
|
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,60 +1,59 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
"packageManager": "yarn@4.9.1"
|
|
2
|
+
"name": "@mirite/zod-to-mongoose",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Package for creating mongoose schemas out of Zod schemas for type safety, and removing duplicate effort.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"zod",
|
|
7
|
+
"mongoose",
|
|
8
|
+
"schema",
|
|
9
|
+
"type",
|
|
10
|
+
"type-safe"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/mirite/zod-to-mongoose.git"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Jesse Conner",
|
|
19
|
+
"email": "me@jesseconner.ca",
|
|
20
|
+
"url": "https://jesseconner.ca"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": "./dist/index.js",
|
|
26
|
+
"require": "./dist/index.cjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"types": "./dist/_tsup-dts-rollup.d.ts",
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"check": "tsc --noEmit",
|
|
36
|
+
"format": "prettier --write --cache .",
|
|
37
|
+
"lint": "eslint --fix --cache",
|
|
38
|
+
"release": "yarn build && npm publish",
|
|
39
|
+
"test": "vitest --run --coverage",
|
|
40
|
+
"test:watch": "vitest --ui --coverage"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"mongoose": "^8.18.2",
|
|
44
|
+
"zod": "^3.25.76"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@microsoft/api-extractor": "^7.52.13",
|
|
48
|
+
"@mirite/eslint-config-mirite": "^1.3.1",
|
|
49
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
50
|
+
"@vitest/ui": "^3.2.4",
|
|
51
|
+
"eslint": "^9.36.0",
|
|
52
|
+
"prettier": "^3.6.2",
|
|
53
|
+
"tsup": "^8.5.0",
|
|
54
|
+
"typescript": "^5.9.2",
|
|
55
|
+
"vitest": "^3.2.4"
|
|
56
|
+
},
|
|
57
|
+
"packageManager": "yarn@4.10.3",
|
|
58
|
+
"issues": "https://github.com/mirite/zod-to-mongoose/issues"
|
|
60
59
|
}
|
package/readme.md
CHANGED
|
@@ -18,18 +18,18 @@ Passing in a Zod schema will return a Mongoose schema.
|
|
|
18
18
|
import { createSchema } from "@mirite/zod-to-mongoose";
|
|
19
19
|
|
|
20
20
|
const schema = createSchema(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
z.object({
|
|
22
|
+
name: z.string().optional().default("Bob"),
|
|
23
|
+
age: z.number().default(3).optional(),
|
|
24
|
+
isHappy: z.boolean().optional(),
|
|
25
|
+
birthday: z.date().default(new Date()),
|
|
26
|
+
}),
|
|
27
27
|
);
|
|
28
28
|
|
|
29
29
|
const model = mongoose.model("modelName", schema);
|
|
30
30
|
const result = await model.create({
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
isHappy: true,
|
|
32
|
+
birthday: new Date("1980-01-01"),
|
|
33
33
|
});
|
|
34
34
|
```
|
|
35
35
|
|
|
@@ -41,19 +41,19 @@ If you pass a connection, the model will be created, registered with the connect
|
|
|
41
41
|
import { createSchema } from "@mirite/zod-to-mongoose";
|
|
42
42
|
|
|
43
43
|
const model = createSchema(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
44
|
+
z.object({
|
|
45
|
+
name: z.string().optional().default("Bob"),
|
|
46
|
+
age: z.number().default(3).optional(),
|
|
47
|
+
isHappy: z.boolean().optional(),
|
|
48
|
+
birthday: z.date().default(new Date()),
|
|
49
|
+
}),
|
|
50
|
+
"modelName", // The unique name to give the model
|
|
51
|
+
connection, // Mongoose connection
|
|
52
52
|
);
|
|
53
53
|
|
|
54
54
|
const result = await model.create({
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
isHappy: true,
|
|
56
|
+
birthday: new Date("1980-01-01"),
|
|
57
57
|
});
|
|
58
58
|
```
|
|
59
59
|
|
|
@@ -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.
|