-
-
Notifications
You must be signed in to change notification settings - Fork 626
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
feat(extend): allow adding of custom validators globally (#1077) #1079
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ysfaran, this is an interesting solution, but is too similar to the current customValidator
validator. For full extensibility we have to inject validators and sanitizers directly in the chain.
@fedeci could you please explain what you mean with "full extensibility"? What are the plans here? I agree though that it's quite similar to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now we do not inject the extended ValidatorsImpl
in the middlewares like check()
, body()
... so they would not be available in the chain
// e.g.
body('name').isFoo() // not available
Pluggability of validators is something that we need to think more accurately also considering typescript requirements and security (we can continue discuss about it in the linked issue).
For the moment I like the idea of allowing the user to set options in .custom()
and .customSanitizer()
, but I would prefer to keep the meta
parameter as the second one and move options
at the last position.
I updated the PR and extended
With the change I did they should be available actually. I created a fully working typescript example in this test project: https://github.com/ysfaran/express-validator-extend-test With In // extensions.ts
import { extend } from "express-validator";
type TokenValidatorOptions = {
length?: number;
};
extend.validators({
isToken(value: string, meta, { length = 42 }: TokenValidatorOptions = {}) {
if (typeof value !== "string") {
throw Error("value must be a string");
}
if (!value.startsWith("0x")) {
throw Error("value must start with '0x'");
}
if (value.length !== length) {
throw Error(`value must be ${length} characters long`);
}
return true;
},
});
// index.ts
import "./extensions";
import express from "express";
import { body, validationResult } from "express-validator";
const app = express();
app.use(express.json());
const port = 8080;
app.post("/", body("token").isToken({ length: 3 }), (req, res) => {
const errors = validationResult(req);
if (errors.isEmpty()) {
res.send("body has indeed a valid token!");
} else {
res.status(400).send(errors.array());
}
});
app.listen(port, () => {
console.log(`server started at http://localhost:${port}`);
}); As you can see I use the newly added I checked the functionality by executing following $ curl -H "Content-Type: application/json" -d "{\"token\":\"0x1\"}" http://localhost:8080
body has indeed a valid token! $ curl -H "Content-Type: application/json" -d "{\"token\":\"0x\"}" http://localhost:8080
[{"value":"0x","msg":"value must be 3 characters long","param":"token","location":"body"}] So
To make this actually work in typescript you have to tell the compiler that // express-validator.d.ts
import "express-validator";
declare module "express-validator" {
interface ValidationChain {
isToken: (options?: { length?: number }) => ValidationChain;
}
} This approach is similiar to expect.extend from Please let me know if I missed something here 😃 |
994caa9
to
93b3761
Compare
18dd20e
to
439220d
Compare
d2afdd4
to
ae84dc7
Compare
Worked on something myself that superseded this. |
Description
Everything is already described in #1077.
This PR is my suggested implementation for the validators part. I didn't implement it for sanitizers so far.
To-do list