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(checkExact): returns unknown nested fields under a wildcard #1281

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
52 changes: 51 additions & 1 deletion src/field-selection.spec.ts
Expand Up @@ -430,6 +430,56 @@ describe('selectUnknownFields()', () => {
});
});

it('selects unknown fields nested under a checked wildcard', () => {
const req = {
body: {
obj: {
nestedobj1: { foo: true, bar: 123, boom: { foo: true } },
nestedobj2: { foo: true, baz: true },
},
arr: [{ foo: 'a' }, { foo: 'a', bar: true }],
},
};
// arr and obj are checked
const instances = selectUnknownFields(req, ['obj', 'obj.*.foo', 'arr', 'arr.*.foo'], ['body']);
expect(instances).toHaveLength(4);
expect(instances[0]).toMatchObject({
path: 'obj.nestedobj1.bar',
value: 123,
location: 'body',
});
expect(instances[1]).toMatchObject({
path: 'obj.nestedobj1.boom',
value: { foo: true },
location: 'body',
});
expect(instances[2]).toMatchObject({
path: 'obj.nestedobj2.baz',
value: true,
location: 'body',
});
expect(instances[3]).toMatchObject({
path: 'arr[1].bar',
value: true,
location: 'body',
});
});

it('selects unknown fields when there are muliple wildcards', () => {
const req = {
body: {
obj: {
nestedobj1: { foo: true, bar: 123, boom: { foo: true } },
nestedobj2: { foo: true, baz: true },
},
arr: [{ foo: 'a' }, { foo: 'a', bar: true }],
},
};
// arr and obj are checked
const instances = selectUnknownFields(req, ['obj', 'obj.*', 'arr', 'arr.*.*'], ['body']);
expect(instances).toHaveLength(0);
});

it('does not select any fields at a globstar level', () => {
const req = { body: { foo: 1, bar: { baz: 2 } } };
const instances = selectUnknownFields(req, ['**'], ['body']);
Expand All @@ -454,7 +504,7 @@ describe('selectUnknownFields()', () => {

it('does not select any fields nested under a known field', () => {
const req = { body: { obj1: { foo: 1, bar: 2 } } };
const instances = selectUnknownFields(req, ['obj1', 'obj.foo'], ['body']);
const instances = selectUnknownFields(req, ['obj1'], ['body']);
expect(instances).toHaveLength(0);
});

Expand Down
2 changes: 1 addition & 1 deletion src/field-selection.ts
Expand Up @@ -135,7 +135,7 @@ function findUnknownFields(
unknownFields: UnknownFieldInstance[] = [],
): UnknownFieldInstance[] {
const globstarBranch = tree['**'];
if (tree[''] || globstarBranch?.['']) {
if (globstarBranch?.[''] || (tree[''] && Object.keys(tree).length === 1)) {
// The rest of the tree from here is covered by some validation chain
// For example, when the current treePath is `['foo', 'bar']` but `foo` is known
return unknownFields;
Expand Down