question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing Multiple Field Validation Hook (Schema Validation)

See original GitHub issue

Hi everybody. I have previous experience using other validation libraries and in all of them I have a hook to put my code where I want validate multiple fields. This hook is executed after successful validation of all individual fields. I know that is possible achieve the same using mongoose, but I end up executing the same field validation for each field dependency. E.g. field2 validation depends on field1 and field3 depends on field1. Did you see? field1 validation executed two times.

After search a lot, my only solution to avoid this was override schema.methods.validate, inside it call validate and then put my multiple field validation code. To improve this, I plan do a monkey patch on mongoose.Document.prototype.validate.

There is another solution for this? Please tell me.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
pedro-netcommented, Oct 3, 2016

I just found a way to workaround this issue: Create a method schema.validateTogether and put there multi-field validation code, then create another method schema.validateAll that calls schema.validate and schema.validateTogether. Then schema.pre('save') calls schema.validateTogether. If I want validate data, instead schema.validate I do schema.validateAll. Yeah, that’s it.

0reactions
vkarpov15commented, Oct 7, 2016

That’s a good workaround 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

react hook form - How to validate multiple fields together?
I have several fields that I need to validate together and return 1 error message. When I return an error value from resolver...
Read more >
Kubernetes Schema Validation | Datree.io
In this article, we will compare several schema validation solutions and explain why Kubeconform is ... Are any mandatory fields missing?
Read more >
Validation-and-Serialization - Fastify
Validating the input is very easy: just add the fields that you need inside the route schema, and you are done! The supported...
Read more >
Schema Validation - python-jsonschema - Read the Docs
The simplest way to validate an instance under a given schema is to use the validate function. jsonschema.validate(instance, schema, ...
Read more >
Form Validation Using Custom React Hooks - Upmostly
A login form with validation using a custom react hook. We'll be using the same project from part one. If you haven't yet...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found