Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to hook cast error with custom error message. #3162

Closed
perzy opened this issue Jul 14, 2015 · 5 comments
Closed

I want to hook cast error with custom error message. #3162

perzy opened this issue Jul 14, 2015 · 5 comments
Labels
discussion If you have any thoughts or comments on this issue, please share them! new feature This change adds new functionality, like a new method or class
Milestone

Comments

@perzy
Copy link

perzy commented Jul 14, 2015

Why CastError is throw before validate. So I cannot custom error message with CastError.

like:

birthday:{
    type:Date
}
Schema.path('birthday').validate(function(date){
   return _.isDate(date);
},'invalid birthday');

And when I set birthday to a invalid date,then call save method.
But,'invalid birthday' hook is after mongoose build-in CastError , so I cannot get the error with message 'invalid birthday'.

Has any method to do it?Thank you!!

@vkarpov15
Copy link
Collaborator

Unfortunately not that I know of. Casting is deeply ingrained into mongoose and always executes before any validation, so I don't think there's a way to set custom cast errors. But definitely something to consider for the future.

@vkarpov15 vkarpov15 added this to the 5.0 milestone Jul 14, 2015
@vkarpov15 vkarpov15 added new feature This change adds new functionality, like a new method or class discussion If you have any thoughts or comments on this issue, please share them! labels Jul 14, 2015
@keithics
Copy link

keithics commented Jan 6, 2016

+1

@markstos
Copy link
Contributor

markstos commented May 4, 2016

There is a way to set a custom value for a CastError, it's right there in the source code:

https://github.com/Automattic/mongoose/blob/master/lib/error/cast.js#L16

It's just this value appears to be ignored. It doesn't help that that the CastError method is undocumented ( #4120).

We are working on mongoose-geojson-schema. It adds schema and validation support for GeoJSON elements to Mongoose.

When the GeoJSON is not valid, we would like to tell people why. Right now the error is always "failure to cast", which is frustrating because there are several ways GeoJSON validation can fail and the current diagnostic doesn't provide a clue as to /why/ the validation is failing.

@astik
Copy link

astik commented Jan 22, 2018

For those coming for finding a way customizing cast error message key. Here is an ugly, still working, way to do :

const castWithMessageKey = function(schema, options = {}) {
	schema.post('validate', function(error, doc, next) {
		Object.values(error.errors)
			.filter(fieldError => fieldError.name === 'CastError')
			.forEach(fieldError => {
				fieldError.message = options.messageKey || 'error.common.cast';
			});
		next(error);
	});
};

export default castWithMessageKey;

Then you can use it as a plugin :

import castWithMessageKey from './PATH/TO/castWithMessageKey.js'
...
mySchema.plugin(castWithMessageKey)
// or with custom message override :
mySchema.plugin(castWithMessageKey, {
    messageKey : 'foobar'
})

@vkarpov15
Copy link
Collaborator

With #13608, you'll be able to do the following:

new Schema({
  birthday: {
    type: Date,
    cast: 'invalid birthday'
  }
});

or:

Schema.path('birthday').castFunction('invalid birthday');

vkarpov15 added a commit that referenced this issue Jul 14, 2023
feat: support generating custom cast error message with a function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion If you have any thoughts or comments on this issue, please share them! new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

5 participants