Conversion Mapping
Conversion Mapping
@nullix/zod-mongoose maps supported Zod types and checks to Mongoose SchemaType options. Document validation checks stored output without rerunning input transforms; see Validate Documents.
For a stored date, use z.date() or z.coerce.date(). For an ISO string you want to keep as a string, use z.iso.datetime() or z.iso.date(). The older z.string().datetime() and z.string().date() forms also keep strings.
Automatic Validation Mapping
The following Zod checks are directly converted to Mongoose SchemaType options:
| Zod Check | Mongoose Option | Target Types |
|---|---|---|
.min(n) | minlength: n | z.string() |
.max(n) | maxlength: n | z.string() |
.length(n) | minlength: n, maxlength: n | z.string() |
.regex(re) | match: re | z.string() |
.trim() | trim: true | z.string() |
.toLowerCase() | lowercase: true | z.string() |
.toUpperCase() | uppercase: true | z.string() |
.min(n) / .positive() | min: n | z.number(), z.date() |
.max(n) / .negative() | max: n | z.number(), z.date() |
.uuid() | UUID format validation; normally String | z.string() |
.datetime() | type: String; format checked by Zod | z.string() |
.date() | type: String; format checked by Zod | z.string() |
.time() | type: String | z.string() |
Type Conversion Table
The following table shows how Zod types are mapped to Mongoose types by default.
| Zod Type | Mongoose Type | Notes |
|---|---|---|
z.string() | String | Required by default; '' is valid unless constrained. |
z.uuid() / z.string().uuid() | String by default | UUID format is still checked by Zod. A configured global Mongoose instance may select its native UUID type. |
z.iso.datetime() / z.iso.date() | String | Keeps the ISO text and passes default post-validation. |
z.string().datetime() / z.string().date() | String | Keeps the ISO text. |
z.number() | Number | |
z.boolean() | Boolean | |
z.date() | Date | |
z.bigint() | BigInt | Fallback to Number if BigInt is not supported. Use withMongoose with type: 'Long' for 64-bit integers. |
z.enum() | String, Number, or Mixed | Preserves string, numeric, or heterogeneous enum values. Nullable wrappers accept null. |
z.nativeEnum() | Same as z.enum() | Uses the enum values to select the storage type. |
z.string().trim() | String | Automatically sets trim: true. |
z.string().toLowerCase() | String | Automatically sets lowercase: true. |
z.string().min(5) | String | Automatically sets minlength: 5. |
z.array() | Array of the inner type | No implicit empty-list default. Use .default([]) to fill omission. Element options stay on the element. |
z.set() | [] | Mapped to a Mongoose array of the value type. |
z.tuple() | Array | Unconstrained homogeneous primitives retain native casting; other tuples use Mixed elements. Zod checks positions, length, and rest elements. |
z.record() | Object | Mapped to a Mongoose nested object (POJO) with dynamic keys. |
z.map() | Map | Mapped to a Mongoose Map with of type. |
z.object() | Subdocument | Unknown keys are stripped. Nested objects have their own _id by default. See Nested Object IDs. |
z.strictObject() | Subdocument with strict: 'throw' | Rejects unknown keys; Mongoose-generated IDs and version keys are excluded from Zod document validation unless declared. |
z.looseObject() | Subdocument with strict: false | Preserves unknown keys. |
.catchall(valueSchema) | Subdocument with strict: false | Preserves unknown keys and validates values in the document validation hook. Dynamic keys do not get individual Mongoose paths. |
.required() / exactOptional() fields | Underlying field type | Required fields use Mongoose required where compatible; exact optional fields reject explicit undefined on document assignment. |
z.intersection() | Subdocument | Merges the definitions of both branches into a single subschema. |
z.union() | Schema.Types.Union (primitives) or Nested Object (objects) | Mapped to Union for primitives, merged into an object for z.object() unions. Others fallback to Mixed. |
z.xor() | mongoose.Schema.Types.Mixed | Non-inclusive union. Maps to Mixed with a custom Zod-based validator to enforce mutual exclusivity. |
z.discriminatedUnion() | Mongoose Discriminator | Maps to native Mongoose discriminators. Common fields are automatically moved to the base schema. |
zObjectId() | mongoose.Schema.Types.ObjectId | Specialized helper for ObjectIds. By default, it is omitted from the generated Mongoose schema to let Mongoose handle its auto-generation. |
zBuffer() | mongoose.Schema.Types.Buffer | Specialized helper for Buffers. |
zPoint() | GeoJSON Point subdocument | Two numeric coordinates; no nested _id by default. See GeoJSON helpers. |
zPolygon() | GeoJSON Polygon subdocument | One or more closed rings; no nested _id by default. |
zRef() | mongoose.Schema.Types.ObjectId | Helper for fields that can be either an ObjectId (unpopulated) or a full object (populated). During Zod validation, it accepts both but always outputs an ObjectId. |
z.instanceof(Buffer) | mongoose.Schema.Types.Buffer | |
z.instanceof(ObjectId) | mongoose.Schema.Types.ObjectId | |
z.literal() | Native primitive type, or Mixed | Enforces declared values, including boolean literals. Defaults require explicit .default(). |
.transform() | Mixed | Preserves output without guessing a callback return type. Parse input before storage. |
.pipe(output) / z.codec(input, output, ...) | Output type | Document validation checks the output schema. |
z.any() / z.unknown() | mongoose.Schema.Types.Mixed | Fallback for unhandled types. |
Unhandled and Unsupported Zod Types
The following Zod types are currently not explicitly handled or are unsupported by nature and will fall back to Mongoose.Schema.Types.Mixed unless a custom type is provided via withMongoose.
| Zod Type | Current Status | Recommended Alternative |
|---|---|---|
z.readonly() | Base Type | Automatically unwrapped, applies readOnly: true metadata. |
z.promise() | Unsupported | Not applicable for database schemas |
z.function() | Unsupported | Not applicable for database schemas |
z.void() / z.never() | Unsupported |
Optional, nullable, branded, and readonly wrappers retain their underlying storage type. Preprocessors, pipes, and codecs use their declared output schema. See Validate Documents for the input/output boundary.
See Object Modes and Unknown Keys for nested objects, option overrides, and query-update caveats.