Custom properties on schema property configuration object
See original GitHub issueIs there a way to add custom properties to schema property’s configuration object?
I would like to have the ability to denote my schema properties as “cloud only” denoting properties that I don’t want the client to be able to change (but I still want to be able to update them). I was hoping to be able to use Document.schema to pull up all the properties that are cloud only during validation to ensure they aren’t changed by the client. Right now I am having to hardcode a list of cloud only properties in an array, but this poses a problem in the event I forget to update that array and inadvertently allow the property to get updated by the client. Of course testing should resolve this but this feature would make for readable/maintainble code.
I tried to add cloudOnly: true
just to try it but run into TypeError: Undefined type
undefinedat
tokens.bru.cloudOnly``. This was expected. Do you have any solutions for this?
Here’s an example schema:
const userSchema = new mongoose.Schema({
userId: {
type: mongoose.ObjectId,
required: true,
cloudOnly: true,
}
});
Issue Analytics
- State:
- Created 5 years ago
- Comments:7
@govindrai I can’t repro your issue, the below script works fine for me. @mhombach 's explanation is unfortunately incorrect.
In general, there’s no reason why you can’t add custom properties on your schema types. There are plenty of plugins that rely on custom schematype options, like mongoose-autopopulate. Try using
User.schema.eachPath()
to iterate through every path in the user model’s schema. Here’s an exampleGood idea, we will improve this error message