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: correctly select properties of primitives #1279

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/express-validator.spec.ts
Copy link
Member Author

Choose a reason for hiding this comment

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

Do we have another place to store these tests?

Copy link
Member

Choose a reason for hiding this comment

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

The tested aspects are the field selection + exists functionality.
I don't feel like they are needed though, everything is covered in some way already.

Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,22 @@ describe('ExpressValidator', () => {
});
});
});

describe('High level tests', () => {
const { body } = new ExpressValidator();
it('should error when .exists() is used on a nested field of a primitive', async () => {
const req = { body: { foo: 'hello' } };
const result = await body('foo.nop').exists().run(req);
expect(result.isEmpty()).toEqual(false);
});
it('should not error when .not().exists() is used on a nested field of a primitive', async () => {
const req = { body: { foo: 'hello' } };
const result = await body('foo.nop').not().exists().run(req);
expect(result.isEmpty()).toEqual(true);
});
it('should not error when .exists() is used on a nested field of a primitive using wildcard', async () => {
const req = { body: { foo: 'hello' } };
const result = await body('foo.*.nop').exists().run(req);
expect(result.isEmpty()).toEqual(true);
});
});
35 changes: 26 additions & 9 deletions src/field-selection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ describe('selectFields()', () => {
const instances = selectFields(req, ['foo', 'baz'], ['cookies']);

expect(instances).toHaveLength(2);
expect(instances[0]).toMatchObject({
expect(instances[0]).toEqual({
location: 'cookies',
path: 'foo',
originalPath: 'foo',
value: 'bar',
});
expect(instances[1]).toMatchObject({
expect(instances[1]).toEqual({
location: 'cookies',
path: 'baz',
originalPath: 'baz',
value: 'qux',
});
});
Expand Down Expand Up @@ -113,14 +115,19 @@ describe('selectFields()', () => {
const req = {
query: { foo: ['bar', 'baz'] },
};
const instances = selectFields(req, ['foo[1]'], ['query']);
const instances = selectFields(req, ['foo[1]', 'foo[2]'], ['query']);
Copy link
Member

Choose a reason for hiding this comment

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

OOC, does this cover anything non-obvious, or is just for thoroughness?


expect(instances).toHaveLength(1);
expect(instances).toHaveLength(2);
expect(instances[0]).toMatchObject({
location: 'query',
path: 'foo[1]',
value: 'baz',
});
expect(instances[1]).toMatchObject({
location: 'query',
path: 'foo[2]',
value: undefined,
});
});

it('selects from headers using lowercase', () => {
Expand Down Expand Up @@ -154,7 +161,7 @@ describe('selectFields()', () => {
});

it('selects inexistent properties', () => {
const instances = selectFields({}, ['foo.bar.baz'], ['cookies']);
const instances = selectFields({ cookies: {} }, ['foo.bar.baz'], ['cookies']);

expect(instances).toHaveLength(1);
expect(instances[0]).toEqual({
Expand All @@ -165,14 +172,24 @@ describe('selectFields()', () => {
});
});

it('does not select properties of primitives', () => {
it('selects properties of primitives', () => {
const req = {
body: { foo: 1 },
};
const instances = selectFields(req, ['foo.toFixed'], ['body']);
expect(instances).toHaveLength(0);
});
const instances = selectFields(req, ['foo.toFixed', 'foo.nop'], ['body']);

expect(instances).toHaveLength(2);
expect(instances[0]).toMatchObject({
location: 'body',
path: 'foo.toFixed',
value: expect.any(Function),
});
expect(instances[1]).toMatchObject({
location: 'body',
path: 'foo.nop',
value: undefined,
});
});
it('deduplicates field instances', () => {
const req = {
body: {
Expand Down
18 changes: 12 additions & 6 deletions src/field-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ function expandPath(object: any, path: string | string[], currPath: readonly str
const rest = segments.slice(1);

if (object != null && !_.isObjectLike(object)) {
if (key === '**' && !rest.length) {
// globstar leaves are always selected
return [reconstructFieldPath(currPath)];
if (key === '**') {
if (!rest.length) {
// globstar leaves are always selected
return [reconstructFieldPath(currPath)];
}
return [];
}

// there still are paths to traverse, but value is a primitive, stop
return [];
if (key === '*') {
// wildcard position does not exist
return [];
}
// value is a primitive, there are still still paths to traverse that will be likely undefined, we return the entire path
Copy link
Member

Choose a reason for hiding this comment

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

Fun fact: you could theoretically go forever now (not for any good reason though), e.g. foo.toFixed.name.toString.length.toFixed... you just have to keep diving deeper into the prototype!

Suggested change
// value is a primitive, there are still still paths to traverse that will be likely undefined, we return the entire path
// value is a primitive, paths being traversed from here might be in their prototype, return the entire path

return [reconstructFieldPath([...currPath, ...segments])];
}

// Use a non-null value so that inexistent fields are still selected
Expand Down