Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

node.js - Custom Mongoose validator on Mixed/Object types

I have a Mongoose model that have a field (named clientConfig) type Mixed/Object, in order to allow the user to enter a key-value object (this is a design requirement that I cannot change). Naturally some validation is made to prevent the user to send anything:

  1. Not more than X properties
  2. Properties name cannot be longer than X chars
  3. Properties values need to be strings
  4. Properties values cannot be longer than X chars

All of that is done by:

const MySchema = new mongoose.Schema(
    {
       clientConfig: {
            type: Object,
            default: {},
            validate: [
                {
                    validator: (el) => Object.keys(el).length <= 10,
                    message: 'Too many keys'
                },
                {
                    validator: (el) => !Object.keys(el).find(key => key.length > 30),
                    message: 'At least one key is too long (30 chars max)'
                },
                {
                    validator: (el) => !Object.keys(el).find(key => typeof key != 'string'),
                    message: 'At least one key value is too long (30 chars max)'
                },
                {
                    validator: (el) => !Object.keys(el).find(key => el[key].length > 50),
                    message: 'At least one key value is too long (30 chars max)'
                }
            ]
        }
}

All of this works. The problem is that when there is an error, the path reported by Mongoose is top level clientConfig: "The error message". Thats the reason why the error messages I choose are so generic: "At least one of....", because I cannot make Mongoose show to the second level like this clientConfig.foo: "The error message".

I could personalized the message using a function in the message field as mentioned in the documentation (props), to have some like clientConfig: "The foo key name is too long!", the issue tho, is that the path is always at top level, and the path is used by my API client to know the name of the form field in the browser where to show the error message, and technically the field clientConfig does not exits, only clientConfig.foo exists.

Another solution could be making clientConfig an array of schemas, in that case the error message path will contain the index of the affected element, but an array will make everything too difficult to manage since those key will be also use for query/search and that involves using thinks like $elemMatch.

So I guess the final question will be if I can manipulate the Mongoose error in a way that I can change the path returned.

question from:https://stackoverflow.com/questions/65905937/custom-mongoose-validator-on-mixed-object-types

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...