Comments (3)
Hello @ArkinSolomon you can execute this kind of logic by running chains in an imperative way, see docs for reference. Please ping me for further help!
from express-validator.
Thanks for your fast response, however this does not answer the question. I basically want to just pass a string in, not pull it from a body or anything. There is no request object as this is simply validating on the client side before submission.
from express-validator.
I have been quite absent from the project in the last year and a half, but at a quick glance it seems nothing changed to me and you still need to provide a req object, you can create a mock one just to be passed to validate to express-validator.
// data you need to validate
const name = 'Federico'
const age = 21
// helper function
const validate = validations => {
return async (req, res, next) => {
for (let validation of validations) {
const result = await validation.run(req);
if (result.errors.length) break;
}
const errors = validationResult(req);
return errors.array()
};
};
const validators = [
body('name').isString().isLength({ min: 4, max: 20 }),
body('age').isInt(),
]
const validationChain = validate(validators)
const req = {
body: {
name,
age
}
}
const errors = await validationChain.run(req) // You can then reuse req.body to compose the request to the server
if errors.length === 0 {
// perform server request here
} else {
// handle errors in the UI
}
from express-validator.
Related Issues (20)
- Create a test plan for "page" and limit in params HOT 1
- Documentation Error in validation-chain.md
- Property 'errors' is private and only accessible within class 'Result<T>'. HOT 4
- Is checkExact() with wildcards supported? HOT 2
- Unexpected change in behaviour v6/v7 for .notEmpty() HOT 1
- Usage of exist, if in checkSchema
- Validation on nested fields not working. HOT 1
- validationResult function don't give any Error HOT 3
- Empty array as default value not working. HOT 1
- isDate() returns false on "2021-02-12" for express-validator v7 HOT 5
- How to pass parameters to a custom validator using schemas HOT 2
- TS Error. Type 'IsURLOptions' does not include some properties
- const {query} = require("express-validator") not working but {check} is working HOT 1
- How to stop the validation flow to different checks HOT 1
- How can I prevent isStrongPassword() accepting whitespaces? HOT 1
- Validate json object HOT 6
- Able to use validation inside controller? HOT 5
- How to use if inside ParamSchema? HOT 1
- Check if a field is either a string or an Array of Strings HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from express-validator.