@mirite/zod-to-mongoose 0.1.1 → 0.2.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.
- package/dist/_tsup-dts-rollup.d.cts +5 -4
- package/dist/_tsup-dts-rollup.d.ts +5 -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 +7 -7
- package/readme.md +1 -0
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import * as Mongoose from 'mongoose';
|
|
2
|
-
import
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import type { ZodArray } from 'zod';
|
|
3
4
|
import type { ZodBoolean } from 'zod';
|
|
4
5
|
import type { ZodDate } from 'zod';
|
|
5
6
|
import type { ZodDefault } from 'zod';
|
|
6
7
|
import type { ZodNumber } from 'zod';
|
|
7
|
-
import
|
|
8
|
-
import
|
|
8
|
+
import { ZodObject } from 'zod';
|
|
9
|
+
import { ZodRawShape } from 'zod';
|
|
9
10
|
import type { ZodString } from 'zod';
|
|
10
11
|
import type { ZodTypeAny } from 'zod';
|
|
11
12
|
import type { ZodUnion } from 'zod';
|
|
@@ -33,6 +34,6 @@ export declare type FieldDefinition = {
|
|
|
33
34
|
|
|
34
35
|
declare type SupportedPrimitive = ZodBoolean | ZodDate | ZodNumber | ZodString;
|
|
35
36
|
|
|
36
|
-
export declare type SupportedType = SupportedPrimitive | ZodDefault<ZodTypeAny> | ZodObject<ZodRawShape> | ZodUnion<readonly [ZodTypeAny, ...ZodTypeAny[]]>;
|
|
37
|
+
export declare type SupportedType = SupportedPrimitive | ZodArray<ZodTypeAny> | ZodDefault<ZodTypeAny> | ZodObject<ZodRawShape> | ZodUnion<readonly [ZodTypeAny, ...ZodTypeAny[]]>;
|
|
37
38
|
|
|
38
39
|
export { }
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import * as Mongoose from 'mongoose';
|
|
2
|
-
import
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import type { ZodArray } from 'zod';
|
|
3
4
|
import type { ZodBoolean } from 'zod';
|
|
4
5
|
import type { ZodDate } from 'zod';
|
|
5
6
|
import type { ZodDefault } from 'zod';
|
|
6
7
|
import type { ZodNumber } from 'zod';
|
|
7
|
-
import
|
|
8
|
-
import
|
|
8
|
+
import { ZodObject } from 'zod';
|
|
9
|
+
import { ZodRawShape } from 'zod';
|
|
9
10
|
import type { ZodString } from 'zod';
|
|
10
11
|
import type { ZodTypeAny } from 'zod';
|
|
11
12
|
import type { ZodUnion } from 'zod';
|
|
@@ -33,6 +34,6 @@ export declare type FieldDefinition = {
|
|
|
33
34
|
|
|
34
35
|
declare type SupportedPrimitive = ZodBoolean | ZodDate | ZodNumber | ZodString;
|
|
35
36
|
|
|
36
|
-
export declare type SupportedType = SupportedPrimitive | ZodDefault<ZodTypeAny> | ZodObject<ZodRawShape> | ZodUnion<readonly [ZodTypeAny, ...ZodTypeAny[]]>;
|
|
37
|
+
export declare type SupportedType = SupportedPrimitive | ZodArray<ZodTypeAny> | ZodDefault<ZodTypeAny> | ZodObject<ZodRawShape> | ZodUnion<readonly [ZodTypeAny, ...ZodTypeAny[]]>;
|
|
37
38
|
|
|
38
39
|
export { }
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var h=Object.create;var p=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var g=Object.getPrototypeOf,Z=Object.prototype.hasOwnProperty;var b=(e,o)=>{for(var n in o)p(e,n,{get:o[n],enumerable:!0})},y=(e,o,n,t)=>{if(o&&typeof o=="object"||typeof o=="function")for(let a of T(o))!Z.call(e,a)&&a!==n&&p(e,a,{get:()=>o[a],enumerable:!(t=S(o,a))||t.enumerable});return e};var M=(e,o,n)=>(n=e!=null?h(g(e)):{},y(o||!e||!e.__esModule?p(n,"default",{value:e,enumerable:!0}):n,e)),w=e=>y(p({},"__esModule",{value:!0}),e);var j={};b(j,{createSchema:()=>l});module.exports=w(j);var m=M(require("mongoose"),1),r=require("zod");function l(e,o,n){let t={};for(let d in e.shape){let i=e.shape[d];t[d]=s(d,i)}let a=new m.Schema(t);return!n||!o?a:{model:n.model(o,a),schema:a}}function s(e,o){let n=k(o),t;switch(n.definition._def.typeName){case r.ZodFirstPartyTypeKind.ZodArray:{let d=n.definition._def.type;if(u(d)){let i=d.shape,f={};for(let c in i)f[c]=s(c,i[c]);t=[f]}else t=[s(e,d)];break}case r.ZodFirstPartyTypeKind.ZodBoolean:t=Boolean;break;case r.ZodFirstPartyTypeKind.ZodDate:t=Date;break;case r.ZodFirstPartyTypeKind.ZodNumber:t=Number;break;case r.ZodFirstPartyTypeKind.ZodObject:break;case r.ZodFirstPartyTypeKind.ZodString:t=String;break;case r.ZodFirstPartyTypeKind.ZodUnion:t={};break;default:throw new TypeError(`Unsupported type: ${e}`)}if(u(n.definition)&&(t=l(n.definition)),t===void 0)throw new TypeError(`Could not determine Mongoose type for Zod type: ${e}`);return n.defaultValue?{default:n.defaultValue,type:t}:t}function u(e){return e._def.typeName===r.ZodFirstPartyTypeKind.ZodObject}function k(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{defaultValue:t,definition:o,optional:n}}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 type { SchemaDefinition } from \"mongoose\";\nimport
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { SchemaDefinition } from \"mongoose\";\nimport * as Mongoose from \"mongoose\";\nimport { ZodFirstPartyTypeKind, type z, type ZodArray, type ZodObject, type ZodRawShape } 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 =\n\t| Mongoose.Schema\n\t| MongooseSchemaType[]\n\t| typeof Boolean\n\t| typeof Date\n\t| typeof Number\n\t// This covers both a schema definition for a sub-document and the empty\n\t// object `{}` used for Mixed types.\n\t| typeof String\n\t// This covers an array of sub-documents or an array of primitives.\n\t// Mongoose expects an array with a single element defining the type.\n\t| { [key: string]: MongooseSchemaType }\n\t// This covers the object format for specifying a default value.\n\t| {\n\t\t\tdefault?: unknown;\n\t\t\ttype: MongooseSchemaType;\n\t };\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.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 = {};\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\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 === ZodFirstPartyTypeKind.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,GACA,IAAAI,EAA0B,yBAC1BC,EAA+F,eA0CxF,SAASH,EACfI,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,KAAK,wBAAsB,SAAU,CAEpC,IAAMG,EADYH,EAAc,WACF,KAAK,KACnC,GAAII,EAAYD,CAAW,EAAG,CAC7B,IAAME,EAAQF,EAAY,MACpBT,EAAwD,CAAC,EAC/D,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,KAAK,wBAAsB,WAC1BD,EAAW,QACX,MACD,KAAK,wBAAsB,QAC1BA,EAAW,KACX,MACD,KAAK,wBAAsB,UAC1BA,EAAW,OACX,MACD,KAAK,wBAAsB,UAC1B,MACD,KAAK,wBAAsB,UAC1BA,EAAW,OACX,MACD,KAAK,wBAAsB,SAC1BA,EAAW,CAAC,EACZ,MACD,QACC,MAAM,IAAI,UAAU,qBAAqBH,CAAI,EAAE,CACjD,CAIA,GAHIK,EAAYJ,EAAc,UAAU,IACvCE,EAAWf,EAAaa,EAAc,UAAU,GAE7CE,IAAa,OAChB,MAAM,IAAI,UAAU,mDAAmDH,CAAI,EAAE,EAE9E,OAAKC,EAAc,aAGZ,CACN,QAASA,EAAc,aACvB,KAAME,CACP,EALQA,CAMT,CAQA,SAASE,EAAYE,EAAiE,CACrF,OAAOA,EAAW,KAAK,WAAa,wBAAsB,SAC3D,CAQA,SAASL,EAAWM,EAA+F,CAClH,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","import_zod","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 y from"mongoose";import{ZodFirstPartyTypeKind as a}from"zod";function u(t,o,n){let e={};for(let r in t.shape){let i=t.shape[r];e[r]=c(r,i)}let d=new y.Schema(e);return!n||!o?d:{model:n.model(o,d),schema:d}}function c(t,o){let n=m(o),e;switch(n.definition._def.typeName){case a.ZodArray:{let r=n.definition._def.type;if(f(r)){let i=r.shape,s={};for(let p in i)s[p]=c(p,i[p]);e=[s]}else e=[c(t,r)];break}case a.ZodBoolean:e=Boolean;break;case a.ZodDate:e=Date;break;case a.ZodNumber:e=Number;break;case a.ZodObject:break;case a.ZodString:e=String;break;case a.ZodUnion:e={};break;default:throw new TypeError(`Unsupported type: ${t}`)}if(f(n.definition)&&(e=u(n.definition)),e===void 0)throw new TypeError(`Could not determine Mongoose type for Zod type: ${t}`);return n.defaultValue?{default:n.defaultValue,type:e}:e}function f(t){return t._def.typeName===a.ZodObject}function m(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{defaultValue:e,definition:o,optional:n}}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
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { SchemaDefinition } from \"mongoose\";\nimport * as Mongoose from \"mongoose\";\nimport { ZodFirstPartyTypeKind, type z, type ZodArray, type ZodObject, type ZodRawShape } 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 =\n\t| Mongoose.Schema\n\t| MongooseSchemaType[]\n\t| typeof Boolean\n\t| typeof Date\n\t| typeof Number\n\t// This covers both a schema definition for a sub-document and the empty\n\t// object `{}` used for Mixed types.\n\t| typeof String\n\t// This covers an array of sub-documents or an array of primitives.\n\t// Mongoose expects an array with a single element defining the type.\n\t| { [key: string]: MongooseSchemaType }\n\t// This covers the object format for specifying a default value.\n\t| {\n\t\t\tdefault?: unknown;\n\t\t\ttype: MongooseSchemaType;\n\t };\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.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 = {};\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\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 === ZodFirstPartyTypeKind.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":"AACA,UAAYA,MAAc,WAC1B,OAAS,yBAAAC,MAAsF,MA0CxF,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,EAAY,MACpBT,EAAwD,CAAC,EAC/D,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,KAAKd,EAAsB,WAC1Ba,EAAW,QACX,MACD,KAAKb,EAAsB,QAC1Ba,EAAW,KACX,MACD,KAAKb,EAAsB,UAC1Ba,EAAW,OACX,MACD,KAAKb,EAAsB,UAC1B,MACD,KAAKA,EAAsB,UAC1Ba,EAAW,OACX,MACD,KAAKb,EAAsB,SAC1Ba,EAAW,CAAC,EACZ,MACD,QACC,MAAM,IAAI,UAAU,qBAAqBH,CAAI,EAAE,CACjD,CAIA,GAHIK,EAAYJ,EAAc,UAAU,IACvCE,EAAWZ,EAAaU,EAAc,UAAU,GAE7CE,IAAa,OAChB,MAAM,IAAI,UAAU,mDAAmDH,CAAI,EAAE,EAE9E,OAAKC,EAAc,aAGZ,CACN,QAASA,EAAc,aACvB,KAAME,CACP,EALQA,CAMT,CAQA,SAASE,EAAYE,EAAiE,CACrF,OAAOA,EAAW,KAAK,WAAajB,EAAsB,SAC3D,CAQA,SAASY,EAAWM,EAA+F,CAClH,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","ZodFirstPartyTypeKind","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.
|
|
3
|
+
"version": "0.2.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,20 +40,20 @@
|
|
|
40
40
|
"test:watch": "vitest --ui --coverage"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"mongoose": "^8.
|
|
43
|
+
"mongoose": "^8.18.2",
|
|
44
44
|
"zod": "^3.25.76"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@microsoft/api-extractor": "^7.52.
|
|
48
|
-
"@mirite/eslint-config-mirite": "^1.1
|
|
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.
|
|
51
|
+
"eslint": "^9.36.0",
|
|
52
52
|
"prettier": "^3.6.2",
|
|
53
53
|
"tsup": "^8.5.0",
|
|
54
|
-
"typescript": "^5.
|
|
54
|
+
"typescript": "^5.9.2",
|
|
55
55
|
"vitest": "^3.2.4"
|
|
56
56
|
},
|
|
57
|
-
"packageManager": "yarn@4.
|
|
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.
|