Skip to content

Commit

Permalink
fix(schema): correctly handle trailing array filters when looking up …
Browse files Browse the repository at this point in the history
…schema paths

Fix #9977
  • Loading branch information
vkarpov15 committed Mar 4, 2021
1 parent 6c09419 commit d2d531b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/helpers/schema/cleanPositionalOperators.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

module.exports = function cleanPositionalOperators(path) {
return path.
replace(/\.\$(\[[^\]]*\])?\./g, '.0.').
replace(/\.(\[[^\]]*\])?\$$/g, '.0');
replace(/\.\$(\[[^\]]*\])?(?=\.)/g, '.0').
replace(/\.\$(\[[^\]]*\])?$/g, '.0');
};
22 changes: 22 additions & 0 deletions test/helpers/schema.cleanPositionalOperators.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const assert = require('assert');
const cleanPositionalOperators = require('../../lib/helpers/schema/cleanPositionalOperators');

describe('cleanPositionalOperators', function() {
it('replaces trailing array filter', function() {
assert.equal(cleanPositionalOperators('questions.$[q]'), 'questions.0');
});

it('replaces trailing $', function() {
assert.equal(cleanPositionalOperators('questions.$'), 'questions.0');
});

it('replaces interior array filters', function() {
assert.equal(cleanPositionalOperators('questions.$[q].$[r].test'), 'questions.0.0.test');
});

it('replaces interior elemMatch', function() {
assert.equal(cleanPositionalOperators('questions.$.$.test'), 'questions.0.0.test');
});
});

0 comments on commit d2d531b

Please sign in to comment.