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

fix(schematype): fixed validation for required UUID field #13018

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/schema/uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ SchemaUUID.checkRequired = SchemaType.checkRequired;
*/

SchemaUUID.prototype.checkRequired = function checkRequired(value) {
return UUID_FORMAT.test(value);
if (Buffer.isBuffer(value)) {
value = binaryToString(value);
}
return value != null && UUID_FORMAT.test(value);
};

/**
Expand Down
63 changes: 63 additions & 0 deletions test/schema.validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const start = require('./common');
const Promise = require('bluebird');
const assert = require('assert');
const random = require('./util').random;
const { v4: uuidv4 } = require('uuid');

const mongoose = start.mongoose;
const Schema = mongoose.Schema;
Expand Down Expand Up @@ -1473,5 +1474,67 @@ describe('schema', function() {
assert.ifError(err);
});
});

it('should validate required UUID fields correctly (gh-12991)', function() {
const uuidSchema = new mongoose.Schema({
_id: { type: mongoose.Schema.Types.UUID, required: true },
name: { type: mongoose.Schema.Types.String, required: true }
});

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

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

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

const uuid = new UUIDModel({ _id: uuidv4(), name: 'uuidName' });
assert.ifError(uuid.validateSync());

const uuidRef = new UUIDRefModel({
_id: uuidv4(),
uuidRef: uuidv4(),
uuidNonRef: uuidv4(),
uuidRefNonRequired: uuidv4(),
name: 'uuidRefName'
});
assert.ifError(uuidRef.validateSync());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to also test that validation fails if each of the uuidRef, uuidNonRef, etc. properties aren't set


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

const err2 = uuidRef2.validateSync();
assert.ok(err2);
assert.ok(err2.errors['uuidRef']);

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

const err3 = uuidRef3.validateSync();
assert.ok(err3);
assert.ok(err3.errors['uuidNonRef']);

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

assert.ifError(uuidRef4.validateSync());
});
});
});