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

.default() sanitizer not working when chained with .optional() #1057

Open
megargayu opened this issue Jun 30, 2021 · 4 comments
Open

.default() sanitizer not working when chained with .optional() #1057

megargayu opened this issue Jun 30, 2021 · 4 comments
Labels

Comments

@megargayu
Copy link

Describe the bug

I am trying to use the default() sanitizer to make my optional array field have a default value. However, when I chain the default() sanitizer with other checks, the default() sanitizer doesn't do anything. In order for it to work, I need to make another check which only has the default() sanitizer attached to it. Instead of body("...").isArray().optional().default([]), I have to split it up into body("...").isArray().optional() and body("...").default([]).

To Reproduce

  1. Create a new npm project with the following dependencies/dev dependencies:
@types/express@4.17.12
express-validator@6.12.0
express@4.17.1
typescript@4.3.4
  1. Create a new typescript file with the following code:
import express from "express";
import { body } from "express-validator";

const app = express();

app.use(express.json());

app.post(
  "/fails",
  body("children").isArray().optional().default([]),
  (req: express.Request) => {
    console.log(`/fails output: ${JSON.stringify(req.body, null, 4)}`);
  }
);

app.post(
  "/works",
  body("children").isArray().optional(),
  body("children").default([]),
  (req: express.Request) => {
    console.log(`/works output: ${JSON.stringify(req.body, null, 4)}`);
  }
);

app.listen(3000);
  1. Run the server and send a POST request with an empty body to both /fails and /works.

Expected behavior

When the server is run and an empty POST request is sent to both /fails and /works, the following should show up in the console:

/fails output: {
    "children": []
}
/works output: {
    "children": []
}

Current behavior

When the server is run and an empty POST request is sent to both /fails and /works, the following currently happens:

/fails output: {}
/works output: {
    "children": []
}

Express-validator version:

  • Version: 6.12.0
@megargayu
Copy link
Author

Looks like I had to remove optional(). When I removed optional(), /fails worked as well as /works (gives the correct output). I am going to close the issue with this, but could someone explain why this happens? Why does doing .isArray().optional().default([]) not work but .isArray().default([]) work? Is this a bug? Can it at least be documented?

@fedeci
Copy link
Member

fedeci commented Jun 30, 2021

Reopening this so that it helps me keep track of thing to do.

@fedeci
Copy link
Member

fedeci commented Aug 1, 2021

Note for whoever wants to try looking into this: probably when data is matched it does not take fields marked with .optional() into account.

@fedeci fedeci changed the title .default() sanitizer not working when chained .default() sanitizer not working when chained with .optional() Aug 1, 2021
@imjordanxd
Copy link

imjordanxd commented Sep 29, 2021

Also curious regarding the behaviour of this. In my scenario, children should be optional. But it should be an array if provided. My solution was to use default values when destructuring.

query("children").isArray().optional(),
(req, res) => {
	const { children = [] } = matchedData(req);
	....
}

lpizzinidev added a commit to lpizzinidev/express-validator that referenced this issue Oct 14, 2022
lpizzinidev added a commit to lpizzinidev/express-validator that referenced this issue Nov 9, 2022
gustavohenke pushed a commit to lpizzinidev/express-validator that referenced this issue Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants