Mongoose Boolean validation not useful if a String is provided Mongoose Boolean validation not useful if a String is provided mongoose mongoose

Mongoose Boolean validation not useful if a String is provided


Here is a solution that borders on hackery, but it should work:

const mongoose = require('mongoose');mongoose.Schema.Types.Boolean.convertToFalse = new Set([false]);mongoose.Schema.Types.Boolean.convertToTrue = new Set([true]);

Remember to set these immediately after the first require, and keep an eye on the cache.

Relevant documentation:https://mongoosejs.com/docs/schematypes.html#booleans

Turns our Mongoose casts five different things to bool, even with strict schemas. Really grinds my gears.


you can change type Boolean to String and validate like that

        testField: {            type : String,            required: true,            validate: {                validator: function (value) {                    return value === "true"                },                message: 'Field must be true'            }        }