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

Bug: Async validation fails (path is required) on Schema.Types.UUID & 'required: true' fields #12991

Closed
2 tasks done
alexliss opened this issue Feb 2, 2023 · 0 comments · Fixed by #13018
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@alexliss
Copy link

alexliss commented Feb 2, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.9.0

Node.js version

14.17.5

MongoDB server version

6.0.3

Typescript version (if applicable)

4.3.5

Description

Using a UUID with 'required: true' throws a validation error 'Path is required'. It is worth noting that by removing the 'required: true' flag from the fields, you can successfully save the document to the database and the data will be saved as mongodb's UUID type. Maybe I'm doing something wrong, but there is no information in the documentation on how to work with UUID schema type.

Steps to Reproduce

import { model, Schema, Types } from 'mongoose';
import { v4 as uuidv4 } from 'uuid';

//////////////////////////////////////

const uuidSchema = new Schema({
  _id: { type: Schema.Types.UUID, required: true },
  name: { type: Schema.Types.String, required: true },
});

const uuidRefSchema = new Schema({
  _id: { type: Schema.Types.UUID, required: true },
  uuidRef: { type: Schema.Types.UUID, ref: 'UUIDModel', required: true },
  uuidNonRef: { type: Schema.Types.UUID, required: true },
  uuidRefNonRequired: { type: Schema.Types.UUID, ref: 'UUIDModel' },
  name: { type: Schema.Types.String, required: true },
});

const UUIDModel = model('UUIDModel', uuidSchema, 'uuids');

const UUIDRefModel = model('UUIDRefModel', uuidRefSchema, 'uuidRefs');

//////////////////////////////////////

const objectIDSchema = new Schema({
  _id: { type: Schema.Types.ObjectId, required: true },
  name: { type: Schema.Types.String, required: true },
});

const objectIDRefSchema = new Schema({
  _id: { type: Schema.Types.ObjectId, required: true },
  oidRef: { type: Schema.Types.ObjectId, ref: 'ObjectIDModel', required: true },
  oidNonRef: { type: Schema.Types.ObjectId, required: true },
  oidRefNonRequired: { type: Schema.Types.ObjectId, ref: 'ObjectIDModel' },
  name: { type: Schema.Types.String, required: true },
});

const ObjectIDModel = model('ObjectIDModel', objectIDSchema, 'objectIds');

const ObjectIDRefModel = model('ObjectIDRefModel', objectIDRefSchema, 'objectIds');

//////////////////////////////////////

async function test() {
  const uuid = new UUIDModel({ _id: uuidv4(), name: 'uuidName' });

  console.log(uuid.$isValid('_id')); // 'true'
  await uuid.validate().catch(err => console.log(err)); // 'path '_id' is required'

  const uuidRef = new UUIDRefModel({
    _id: uuidv4(),
    uuidRef: uuidv4(),
    uuidNonRef: uuidv4(),
    uuidRefNonRequired: uuidv4(),
    name: 'uuidRefName',
  });

  console.log(uuidRef.$isValid('_id')); // 'true'
  console.log(uuidRef.$isValid('uuidRef')); // 'true'
  console.log(uuidRef.$isValid('uuidNonRef')); // 'true'
  console.log(uuidRef.$isValid('uuidRefNonRequired')); // 'true'
  await uuidRef.validate().catch(err => console.log(err)); // 'path '_id' is required; 'path 'uuidRef' is required; 'path 'uuidNonRef' is required' (path 'uuidRefNonRequired' successfully passed validation)

  const objectId = new ObjectIDModel({ _id: new Types.ObjectId(), name: 'oidName' });

  objectId.$isValid('_id'); // 'true'
  await objectId.validate(); // 'success'

  const objectIdRef = new ObjectIDRefModel({
    _id: new Types.ObjectId(),
    oidRef: new Types.ObjectId(),
    oidNonRef: new Types.ObjectId(),
    oidRefNonRequired: new Types.ObjectId(),
    name: 'oidRefName',
  });

  console.log(objectIdRef.$isValid('_id')); // 'true'
  console.log(objectIdRef.$isValid('oidRef')); // 'true'
  console.log(objectIdRef.$isValid('oidNonRef')); // 'true'
  console.log(objectIdRef.$isValid('oidRefNonRequired')); // 'true'
  await objectIdRef.validate().catch(err => console.log(err)); // 'success'
}

test();

Expected Behavior

The same as when using ObjectID.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Feb 8, 2023
@vkarpov15 vkarpov15 added this to the 6.9.2 milestone Feb 10, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Feb 11, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Feb 16, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.9.2, 6.9.3, 6.9.4 Feb 16, 2023
vkarpov15 added a commit that referenced this issue Feb 28, 2023
fix(schematype): fixed validation for required UUID field
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants