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

Add ExpressValidator class #1209

Merged
merged 8 commits into from
Feb 5, 2023
Merged

Conversation

gustavohenke
Copy link
Member

@gustavohenke gustavohenke commented Feb 4, 2023

Description

This adds the ExpressValidator class, which can hold extension validators, sanitisers and default result formatters.

It can be used like this:

const { check, body, checkSchema, validationResult } = new ExpressValidator(
	// Custom validators
	{ isAllowedDomain: value => { ... } },
	// Custom sanitizers
	{ removeDomain: value => { ... } },
	// Options. Currently only errorFormatter
	{ errorFormatter: error => `Error of type ${error.type}` },
);

app.post('/new-user', [
	check('email')
		.trim()
		.isEmail()
		.isAllowedDomain()
		.removeDomain()
], (req, res) => {
	console.log(validationResult(req).array());
	// => 'Error of type field'
});

To-do list

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

gustavohenke added a commit that referenced this pull request Feb 4, 2023
I couldn't find any commit on TypeScript's repo that made
this obvious (also there are hundreds in the diff!),
but apparently v4.2.4 causes issues _only on Linux_ when
building #1209:

https://github.com/express-validator/express-validator/actions/runs/4078910523/jobs/7029689138

I verified that my dev macOS doesn't get this error,
but I do if I use Docker.
Upgrading to TypeScript 4.3.x seems to solve the issue.
@coveralls
Copy link

Coverage Status

Coverage: ?%. Remained the same when pulling c250ff8 on v7-express-validator-instances into 92e0752 on v7-features.

@coveralls
Copy link

coveralls commented Feb 5, 2023

Coverage Status

Coverage: 100.0%. Remained the same when pulling 020cd06 on v7-express-validator-instances into 92e0752 on v7-features.

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

Successfully merging this pull request may close these issues.

Allow simple extension of custom validators/sanitizers for better readability and reusability
2 participants