Mongoose: Suggestion: Schema option flag to skip type casting

Created on 27 Nov 2016  路  2Comments  路  Source: Automattic/mongoose

Would suggest the addition of a castToType boolean model flag with a default True representing no change from current behavior and a False setting doing nothing more than skipping type casting but letting validation proceed. Thus a String typed property would accept only values passed in as a string and fail on numeric or other values regardless of whether they could be coerced into Strings. Option would apply to all primitives.

The two workarounds for my use case are use of the Mixed type and a custom validator and a setter method. Setting type to Mixed is a bad option because it misrepresents that the values will indeed be strings. Setters work but are cumbersome and rather more opaque than the suggested flag would be.

Have not seen any other issues on this specifically, but have reviewed older issues requesting pre-casting hooks and how difficult they would be to implement.

won't fix

All 2 comments

Not gonna happen unfortunately, casting is too fundamental to how mongoose works and running without casting is too niche a use case for us to support.

Has this issue gone anywhere? I cannot really agree that disabling a potentially hidden bug source in the codebases that use Mongoose also for validation purposes would be a "niche use case".

Was this page helpful?
0 / 5 - 0 ratings