Skip to content

Commit

Permalink
fix(query): handle $round with just 1 arg re: code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Oct 4, 2023
1 parent 4ec6b40 commit fd22f9b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
7 changes: 2 additions & 5 deletions lib/helpers/query/cast$expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,10 @@ function _castExpression(val, schema, strictQuery) {
}
if (val.$round) {
const $round = val.$round;
if (!Array.isArray($round) || $round.length !== 2) {
if (!Array.isArray($round) || $round.length < 1 || $round.length > 2) {
throw new CastError('Array', $round, '$round');
}
val.$round = [
castNumberOperator($round[0], schema, strictQuery),
castNumberOperator($round[1], schema, strictQuery)
];
val.$round = $round.map(v => castNumberOperator(v, schema, strictQuery));
}

_omitUndefined(val);
Expand Down
5 changes: 4 additions & 1 deletion test/helpers/query.cast$expr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ describe('castexpr', function() {
it('casts $round (gh-13881)', function() {
const testSchema = new Schema({ value: Number });

const res = cast$expr({ $eq: [{ $round: ['$value', '00'] }, 2] }, testSchema);
let res = cast$expr({ $eq: [{ $round: ['$value', '00'] }, 2] }, testSchema);
assert.deepStrictEqual(res, { $eq: [{ $round: ['$value', 0] }, 2] });

res = cast$expr({ $eq: [{ $round: ['$value'] }, 2] }, testSchema);
assert.deepStrictEqual(res, { $eq: [{ $round: ['$value'] }, 2] });
});
});

0 comments on commit fd22f9b

Please sign in to comment.