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

Conversation

fedeci
Copy link
Member

@fedeci fedeci commented Feb 16, 2024

Description

Fixes #1245
It is correct to assume that primitives will not have properties in a request.
Despite that express-validator should provide the correct result, so if a user wants to validate a non existent property we should treat that as undefined.
Eventually in the future we may also update FieldInstance to look something like this:

export interface FieldInstance {
  path: string;
  originalPath: string;
  location: Location;
  value: any;
  exists: boolean // added property
}

To-do list

  • I have added tests for what I changed.
  • This pull request is ready to merge.

@coveralls
Copy link

coveralls commented Feb 16, 2024

Coverage Status

coverage: 100.0%. remained the same
when pulling 8f500c3 on fedeci/fix-property-selection
into ffd7994 on master.

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.

@fedeci fedeci changed the title fix: correctly selects properties of primitives fix: correctly select properties of primitives Feb 16, 2024
// 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

@@ -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?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validation on nested fields not working.
3 participants