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

Is there a way to escape all values in a nested object when the keys are unknown? #1137

Closed
mmacdo54 opened this issue Mar 4, 2022 · 2 comments · Fixed by #1216
Closed

Is there a way to escape all values in a nested object when the keys are unknown? #1137

mmacdo54 opened this issue Mar 4, 2022 · 2 comments · Fixed by #1216
Milestone

Comments

@mmacdo54
Copy link

mmacdo54 commented Mar 4, 2022

Say I have a req.body like so

{ user_info: { custom: { '63': 'aaaaaaaa' }, email: 'test@test.com', name: 'A Name', } }

I want run body().escape() on it all but when I do I get custom as '[object Object]'.

The thing to note here is I will not know what keys are inside custom and also I will not know what keys are inside user_info. The fields that come through are completely unknown. So I have tried [body('user_info.custom.*').escape(), body('user_info.*).escape()] but this still return custom as [object Object]. I really just want to escape every key that comes through. But is this possible when I dont know the key names and it is nested like this?

@nukuutos
Copy link

nukuutos commented Aug 8, 2022

There is a problem with unknown level of nested.

You can go through every key of user_info(one level of nested) like this

  body("user_info.*").customSanitizer((value) => {
    // if you really know that your values are objects{} and strings
    const isObject = typeof value === "object";

    if (isObject) return value;
    return myEscapeFunction(value);
  }),

But there's also a problem that you need to have your own escape function.

@gustavohenke
Copy link
Member

Hey,
This is now possible in v7.0.0 with globstar support.

  • To find all fields: **
  • To find all fields called name anywhere: **.name
  • To find all fields under some other field: foo.**
  • ...go crazy. You can even mix with regular wildcards. E.g. foo.*.bar.**.baz

Docs here

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

Successfully merging a pull request may close this issue.

3 participants