Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Invalid schema configuration: Z is not a valid type at path - Zod Schema related #14491

Closed
1 task done
ReturnXDev opened this issue Apr 2, 2024 · 3 comments
Closed
1 task done
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. Stale

Comments

@ReturnXDev
Copy link

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.2.3

Node.js version

v20.11.1

MongoDB version

6.0.14

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

11

Issue

I use Sentry and I get this TypeError:

Invalid schema configuration: Z is not a valid type at path dob. See https://bit.ly/mongoose-schematypes for a list of valid schema types.

Dob = date of birth, a string comes from the front end, from a datepicker so I coerce using Zod.

Code:

dob: z.coerce
      .date()
      .min(daysAgo(365 * 105), "Your date of birth appears to be invalid.")
      .max(daysAgo(365 * 18), "You must be 18 years or older.")
      .nullable(),
export const daysAgo = (days: number) =>
  new Date(Date.now() - days * ONE_DAY_MS);

This ultimately forms part of a modifyProfileSchema

export const modifyProfileSchema = z
  .object({

etc

I then:
export type ModifyProfile = z.infer<typeof modifyProfileSchema>;
and
export type Profile = ModifyProfile & { user_id: string; };
Before finally:

const modelName = "Profiles";
export const Profiles = mongoose.model<Profile & Timestamps>(
  modelName,
  new mongoose.Schema(
    {
...
dob: {
        type: Date,
      },
...

So it is sort of a non-issue, the data saves to the database - when I run it on local and interact with the front-end, it saves, pulls from the database but I constantly get this error from Sentry.

From the 'Z' in the error message I assume something relating to Zod is being passed back but no errors get thrown when I monitor it and ultimately the process works.

Let me know if anyone has any tips or insights.

Thank you.

@ReturnXDev ReturnXDev added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Apr 2, 2024
@vkarpov15 vkarpov15 added this to the 8.3.1 milestone Apr 4, 2024
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Apr 4, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.3.1, 8.3.2 Apr 5, 2024
@vkarpov15
Copy link
Collaborator

I'm unable to repro, can you please modify the following script to demonstrate the issue you're seeing?

const z = require('zod');

const Profiles = mongoose.model(
  'Profiles',
  new mongoose.Schema(
    {
      dob: {
        type: Date,
      }
    }
  )
);

void async function main() {
  await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');

  const modifyProfileSchema = z
  .object({
    dob: z.coerce
      .date()
      //.min(daysAgo(365 * 105), "Your date of birth appears to be invalid.")
      //.max(daysAgo(365 * 18), "You must be 18 years or older.")
      .nullable()
  });
  
  const { dob } = modifyProfileSchema.parse({ dob: new Date() });
  const { _id } = await Profiles.create({ dob });
  console.log(await Profiles.findById(_id).orFail().then(doc => doc.toObject()));
}();

Also, could you please provide the full stack trace of the error? The stack trace may provide something helpful. This error is indicative of trying to set the dob path in your schema to something Zod-related. For example, the following code would trigger the issue you're seeing: new mongoose.Schema({ dob: z.coerce.date() })

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Apr 10, 2024
@vkarpov15 vkarpov15 removed this from the 8.3.2 milestone Apr 10, 2024
Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Apr 25, 2024
Copy link

github-actions bot commented May 1, 2024

This issue was closed because it has been inactive for 19 days and has been marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. Stale
Projects
None yet
Development

No branches or pull requests

2 participants