Git Product home page Git Product logo

Comments (2)

vkarpov15 avatar vkarpov15 commented on July 21, 2024

What you're describing sounds like the sanitizeFilter option we introduced in Mongoose 6, see docs and intro blog post.

    // The following will throw an error if `req.query.search` is `{ $regex: 'abc' }` or `{ $lt: 'test' }` or any other query filter
    const documents = await model
        .find({ name: req.query.search })
        .sort({ name: 'asc' })
        .options({ sanitizeFilter: true })
        .exec();

Does the sanitizeFilter option sound like it fixes your issue?

from mongoose.

yhojann-cl avatar yhojann-cl commented on July 21, 2024

But apparently it does not strictly sanitize according to the property type and it is still possible to send data types that do not correspond to the schema, for example:

console.log(mongoose.version); // 7.0.3
await mongoose.connect('mongodb://localhost:27017/mongoose_test');
const schema = new mongoose.Schema({ name: String });
const TestModel = mongoose.model('Test', schema);

const doc = new TestModel({ name: 'foobar' });
await doc.save();

const docs = await TestModel.find({ name: [ 'foobar' ] }).setOptions({ sanitizeFilter: true }).exec();
console.log(docs);

The results is:

[ { name: 'foobar',
    _id: new ObjectId("666d04f039e8d300b31d303f"),
    __v: 0 } ]

In the name property can send an array instead of a string value with disabled filters in options.

What I propose is an option for the input data to adhere to the schema and not just a simple elimination of filters that begin with the $ character. Or could it be a bug in that option?.

from mongoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.