Custom Casting
Mongoose 5.4.0 introduced several ways to configure SchemaTypes globally.
One of these new features is the SchemaType.cast()
function, which enables you to override Mongoose's built-in casting.
For example, by default Mongoose will throw an error if you attempt to cast a string that contains a Japanese numeral to a number.
const schema = new mongoose.Schema({
age: Number
});
const Model = mongoose.model('Test', schema);
const doc = new Model({ age: '二' });
const err = doc.validateSync();
// "Cast to Number failed for value "二" at path "age""
err.message;
You can overwrite the default casting function for numbers to allow converting the string that contains the Japanese numeral "2" to a number as shown below.
// Calling `cast()` on a class that inherits from `SchemaType` returns the
// current casting function.
const originalCast = mongoose.Number.cast();
// Calling `cast()` with a function sets the current function used to
// cast a given schema type, in this cast Numbers.
mongoose.Number.cast(v => {
if (v === '二') {
return 2;
}
return originalCast(v);
});
const schema = new mongoose.Schema({
age: Number
});
const Model = mongoose.model('Test', schema);
const doc = new Model({ age: '二' });
const err = doc.validateSync();
err; // null
doc.age; // 2