wgrisa / express-yup-middleware Goto Github PK
View Code? Open in Web Editor NEWexpress-yup-middleware is an express middleware that uses Yup schemas to validate a request and return a response with errors.
License: MIT License
express-yup-middleware is an express middleware that uses Yup schemas to validate a request and return a response with errors.
License: MIT License
I have found this dependencies in your project, most of them are unnecessary:
"dependencies": {
"@types/express": "^4.17.3",
"@types/yup": "^0.26.33",
"express": "^4.17.1",
"http-status-codes": "^1.4.0",
"ts-node": "^8.8.1",
"typescript": "^3.8.3",
"yup": "^0.28.3"
},
typescript
+ @types/*
+ ts-node
- should be located in "devDepencdencies"
express
+ yup
- are required only for "typescript types". Maybe this dependencies should be located in "peerDependecies"? or "devDependencies" also?
Hi,
Thank you for writing this great module! I have a suggestion about how to improve it.
I think it would be useful to have access to the validated and typed object after validation is performed in the request handler. Right now it seems this module will perform validation but the validated objects are lost and not presented to the request handler.
By not passing the typed objects, it forces that my code works with the untyped req.body
, or I can call yup.validate<DataType>(req.body)
again to get a validated result that is the same type as the schema.
Rusty
First, your library is very useful so thanks for putting it out there. I was thinking of writing one myself, but since I found yours I'm planning to use it.
One thing I found out while evaluating the library, and not sure if it's a bug or missing feature.
Essentially the validation seems to only be returning the first error. It looks like the response is of type Array so I'm thinking that the plan was to return all the errors?
For example with the following Yup schema:
export const SignUpSchema = Yup.object().shape({
email: Yup.string()
.email('Invalid email address')
.required('An email address is required'),
firstName: Yup.string().required(
'In order for us to get acquainted we need to know your first name'
),
lastName: Yup.string().required(
'In order for us to get acquainted we need to know your last name'
),
});
When I perform a request with an empty body, the response is:
{
"errors": {
"body": [
{
"message": "In order for us to get acquainted we need to know your last name",
"propertyPath": "lastName"
}
]
}
}
And I believe the desired outcome should be:
{
"errors": {
"body": [
{
"message": "An email address is required",
"propertyPath": "email"
},
{
"message": "In order for us to get acquainted we need to know your last name",
"propertyPath": "firstName"
},
{
"message": "In order for us to get acquainted we need to know your last name",
"propertyPath": "lastName"
},
]
}
}
I'm not able to pass the {abortEarly: false} property, to list all errors at once
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.