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.

Validation not working for new entry in replaceOne route

See original GitHub issue

Hi, I was currently struggling with data validation in the PUT method since it Creates or Updates an entity I thought it would respect the corresponding CrudValidationGrup depending on the actual action it’s going to perform.

But after some digging, I realized that the “problem” was in this line and occurred me that is too hard to decide what validation group it’s going to use at the route creation.

Since the validation occurs before the endpoint actually getting hit and therefore before it can check entity existence, I’m not sure what would be the best approach to this.

From the top of my head what I can think of is:

  • Change the mentioned line to make PUT use the same validations as POST (probably the easiest way to change, but I’m not sure if it can bring any hidden bugs. I will open a CR for this).
  • Change replaceOne to be a route that does no validation and just checks user existence and redirects to the appropriate route with the appropriate validations (may increase the overhead for each call)
  • Create a new CrudValidationGroup (I don’t think that helps very much since it will probably be used at the same places as the CREATE)

For now I can overcome this by hiding the replaceOneBase route, creating one of my own and redirecting the call to replaceOneBase after validation. The problem is that I don’t know an easy way of adding the ValidationPipe with the correct configurations besides of creating a new controller and overriding createOneBase (what is terrible).

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cleivsoncommented, Jan 14, 2020

I agree that the custom DTO would come first in terms of prioritization as it adds more flexibility in terms of validation and data hiding.

in general, it should be in the same places as the CREATE, except maybe it also should cover some primary columns, or auto-generated ones, because PUT method means you need to send the whole resource but not the partial resource as you might send it on PATCH request.

I partially agree on that, I just don’t understand why should PUT validate more fields than create, but I accept that it could.

If we had to add a replace validation group, I think it should be complimentary to create since create defines the minimum requirements for registering an entity, no method should allow less than that.

1reaction
jonahsnidercommented, Jan 14, 2020

I have a similar issue with CRUD validation groups where it seemed the groups were being triggered on all write/create/update requests. It’s causing a major headache because the CREATE validation is being triggered on PATCH requests.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Mongoose doesn't validate on update? - Stack Overflow
As of Mongoose 4.0 you can run validators on update() and findOneAndUpdate() using the new flag runValidators: true . Mongoose 4.0 introduces an...
Read more >
db.collection.replaceOne() — MongoDB Manual
If upsert: true and no documents match the filter , db.collection.replaceOne() creates a new document based on the replacement document.
Read more >
How we validate input data using pydantic
We use the Python package pydantic for fast and easy validation of input data. Here's how.
Read more >
Mongoose v6.8.1: API docs
Used for declaring paths in your schema that should be MongoDB ObjectIds. Do not use this to create a new ObjectId instance, use...
Read more >
Schema Validation in MongoDB Atlas - Topcoder
Sometimes clients provide inputs which are not according to our ... the convenience of validating data while inserting a new entry into your ......
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