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.

Skip validation for certain path(s) in Document.validate() method

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Unable to skip or prevalidate or bypass the validation for a path on mongoose

If the current behavior is a bug, please provide the steps to reproduce.

  1. Create a model
  2. Add required validator on a path
  3. Run the doc.$markValid(“path”)
  4. Try doc.validate(["path"]).catch(e => console.log(Object.keys(e.errors).length))
  5. The length would be == 1

Here I want to bypass the required validation for gwId field image

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "./dist",
    "strict": true,
    "noImplicitAny": true,
    "typeRoots": ["./ctypings", "./node_modules/@types"],
    "types": ["express", "express-session", "process", "express-request-id"],
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": ["node_modules", "./node_modules", "./node_modules/*", "./node_modules/@types/node/index.d.ts"],
  "include": ["src/**/*.ts", "ctypings/**/*.ts"]
}

What is the expected behaviour?

Well, $markValid should bypass all the validators for one or more paths (except unique)

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version.

Mongoose: 5.12.7

Node: v14.16.0

Mongodb: 3.6.6

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
vkarpov15commented, May 11, 2021

I’m currently working on a series of blog posts about that very subject 😃 First one is here: http://thecodebarbarian.com/mongoose-internals-schemas-options-models.html .

Re: sanitizing query filters, we’re working on that for Mongoose 6.0. Follow #3944 for updates.

1reaction
IslandRhythmscommented, May 10, 2021
const mongoose = require('mongoose');
const {Schema} = mongoose;

const testSchema = new Schema({
    name: {
        type: String, 
        required: true
    },
    nickname: {
        type: String
    }
});
    const Test = new mongoose.model('Test', testSchema);

    async function run() {
        await mongoose.connect('mongodb://localhost:27017/test', {
            useNewUrlParser: true,
            useUnifiedTopology: true
          });
        
        await mongoose.connection.dropDatabase();

        const entry = await Test.create({nickname: 'Test'});
        entry.$markValid('name');
        await entry.validate('name').catch(e=> console.log('Yo', Object.keys(e.errors).length));
    }
    run();

Read more comments on GitHub >

github_iconTop Results From Across the Web

jQuery Validation plugin: disable validation for specified ...
Hello @lepe, you know how to re-apply jquery validation after writing $("form").validate().cancelSubmit = true; . I tried $("form").validate().cancelSubmit = ...
Read more >
48817 – Skip validation query and use JDBC API for validation
I've added a Validator interface with the following single method: public boolean validate(Connection connection, int validateAction) I also ...
Read more >
Mongoose v6.8.2: Validation
Before running validators, Mongoose attempts to coerce values to the correct type. This process is called casting the document. If casting fails for...
Read more >
Active Record Validations - Ruby on Rails Guides
The following methods skip validations, and will save the object to the database regardless of its validity. They should be used with caution....
Read more >
Client-side form validation - Learn web development | MDN
One of the most significant features of modern form controls is the ability to validate most user data without relying on JavaScript. This...
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