Это видео недоступно.
Сожалеем об этом.
Ajv JSON schema validation
HTML-код
- Опубликовано: 14 авг 2024
- 🔥More exclusive content: productioncode...
Twitter: / _jgoebel
Website: jangoebel.com
Blog: productioncode...
Code: github.com/pro...
Ajv.js: ajv.js.org
JSON schema: json-schema.org/
00:00 introduction to Ajv JSON schema validation
00:50 Ajv example project walkthrough
02:20 install ajv and ajv-formats
03:35 creating and configuring Ajv instance
05:18 creating a custom Ajv JSON schema
08:11 creating an Ajv schema validation middleware
09:56 Ajv errors: preserving the reference
12:13 making use of Ajv validator in express
12:50 testing Ajv validation with Postman
14:11 conclusion
In this video we cover how to perform JSON validation with Ajv. We use Ajv to validate incoming request payloads and to return a bad request error in case we are missing certain fields or in case certain fields are not formatted correctly.
We talk about how to set up Ajv with express and we create a custom Ajv schema which we can plug into our express middleware. The Node.js Ajv validator is probably one of the most promising request body validation approaches out there because it makes use of the standardized cross-plattform JSON schema RFC: json-schema.org/
What do you think about this video?
Was the explanation clear?
amazing explanations. very nice and clean way to handle input validation
Glad it was helpful!
Can I send the sample json data with the request instead using postman?
Great video, thanks a lot for clearly explaining why the error reference needs to be copied.
thx Saketh 👍
Thanks for the video!
If you are getting a error that says "Cannot set headers after they are sent to the client", you can add an 'else' in front of the 'next()' in validate-dto.js.
this error means that you have basically instructed the server to send a response and shortly after it you are trying to modify the response by adding an additional header
ajv-sanitizer is also a nice library to use with ajv
Awesome!! The video helped me a lot. Thanks!
Glad it helped!
Thank you soo much, Ajv concept cleared
thx Mohammed 👍
Thanke vier mach
you're most welcome
Is it also possible to use AJV for FRONTEND data validation?
Nice video, thanks.
Glad you liked it!
in the validateDto function, you might want to change "res.status(400).json(errors);" to "return res.status(400).json(errors);" to prevent next being called and immediately return the response to the client
I get a consistent-return eslint error after implementing your change. Would instead putting the "next();" under an else statement be ideal?
@@TakingItCasual can you send your code snippet after the change?
А что лучше использовать по вашему опыту для валидаций тела запроса в nodeJS?
this is ultimately a matter of taste. I'd go with some library. Some people use Ajv, others use yup or express-validator. Any of them works. Ajv has the additional advantage that it is standardized
Good video
thx abh! 👍
whenever I hear AJV my mind thinks of JAV