Skip to content

Commit

Permalink
fix: ignore keyNotInSchema for $type in $currentDate
Browse files Browse the repository at this point in the history
  • Loading branch information
aldeed committed Apr 8, 2021
1 parent 1da95aa commit 3d7b815
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions package/lib/SimpleSchema.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,36 @@ describe('SimpleSchema', function () {
expect(foo.rawDefinition).toEqual({ foo: String });
});

it('$currentDate Date validation', function () {
const schema = new SimpleSchema({
date: Date,
});
const context = schema.namedContext();

let testModifer = {
$currentDate: {
date: true,
},
};
expect(context.validate(testModifer, { modifier: true })).toBe(true);

testModifer = {
$currentDate: {
date: { $type: 'date' },
},
};
context.validate(testModifer, { modifier: true });
expect(context.validate(testModifer, { modifier: true })).toBe(true);

// timestamp fails because it would save a MongoDB.Timestamp value into a Date field
testModifer = {
$currentDate: {
date: { $type: 'timestamp' },
},
};
expect(context.validate(testModifer, { modifier: true })).toBe(false);
});

describe('SimpleSchema.Any', function () {
const schema = new SimpleSchema({
testAny: SimpleSchema.Any,
Expand Down
2 changes: 1 addition & 1 deletion package/lib/doValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function doValidation({
// Get the schema for this key, marking invalid if there isn't one.
if (!def) {
// We don't need KEY_NOT_IN_SCHEMA error for $unset and we also don't need to continue
if (op === '$unset') return;
if (op === '$unset' || (op === '$currentDate' && affectedKey.endsWith('.$type'))) return;

validationErrors.push({
name: affectedKey,
Expand Down

0 comments on commit 3d7b815

Please sign in to comment.