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

Add doc as second params to validation message function #12651

Merged
merged 3 commits into from Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/error/validator.js
Expand Up @@ -12,15 +12,16 @@ class ValidatorError extends MongooseError {
* Schema validator error
*
* @param {Object} properties
* @param {Document} doc
* @api private
*/
constructor(properties) {
constructor(properties, doc) {
let msg = properties.message;
if (!msg) {
msg = MongooseError.messages.general.default;
}

const message = formatMessage(msg, properties);
const message = formatMessage(msg, properties, doc);
super(message);

properties = Object.assign({}, properties, { message: message });
Expand Down Expand Up @@ -75,9 +76,9 @@ ValidatorError.prototype.formatMessage = formatMessage;
* @api private
*/

function formatMessage(msg, properties) {
function formatMessage(msg, properties, doc) {
if (typeof msg === 'function') {
return msg(properties);
return msg(properties, doc);
}

const propertyNames = Object.keys(properties);
Expand Down
14 changes: 7 additions & 7 deletions lib/schematype.js
Expand Up @@ -1290,7 +1290,7 @@ SchemaType.prototype.doValidate = function(value, fn, scope, options) {
validatorProperties.value = value;

if (validator instanceof RegExp) {
validate(validator.test(value), validatorProperties);
validate(validator.test(value), validatorProperties, scope);
continue;
}

Expand All @@ -1299,7 +1299,7 @@ SchemaType.prototype.doValidate = function(value, fn, scope, options) {
}

if (value === undefined && validator !== this.requiredValidator) {
validate(true, validatorProperties);
validate(true, validatorProperties, scope);
continue;
}

Expand All @@ -1319,19 +1319,19 @@ SchemaType.prototype.doValidate = function(value, fn, scope, options) {

if (ok != null && typeof ok.then === 'function') {
ok.then(
function(ok) { validate(ok, validatorProperties); },
function(ok) { validate(ok, validatorProperties, scope); },
function(error) {
validatorProperties.reason = error;
validatorProperties.message = error.message;
ok = false;
validate(ok, validatorProperties);
validate(ok, validatorProperties, scope);
});
} else {
validate(ok, validatorProperties);
validate(ok, validatorProperties, scope);
}
}

function validate(ok, validatorProperties) {
function validate(ok, validatorProperties, scope) {
if (err) {
return;
}
Expand All @@ -1343,7 +1343,7 @@ SchemaType.prototype.doValidate = function(value, fn, scope, options) {
}
} else {
const ErrorConstructor = validatorProperties.ErrorConstructor || ValidatorError;
err = new ErrorConstructor(validatorProperties);
err = new ErrorConstructor(validatorProperties, scope);
err[validatorErrorSymbol] = true;
immediate(function() {
fn(err);
Expand Down
27 changes: 27 additions & 0 deletions test/schema.validation.test.js
Expand Up @@ -1330,6 +1330,33 @@ describe('schema', function() {
});
});

it('Allows for doc to be passed as another parameter gh-12564', function(done) {
let document = '';
const s = mongoose.Schema({
n: {
type: String,
validate: {
validator: function(v) {
return v != null;
},
message: function(properties, doc) {
document = doc.toString();
return 'fail ' + properties.path + ' on doc ' + doc;
}
}
},
field: String
});
const M = mongoose.model('gh-12564', s);
const m = new M({ n: null, field: 'Yo' });

m.validate(function(error) {
assert.equal(error.errors['n'].message.includes(document), true);
assert.equal('fail n on doc ' + document, error.errors['n'].message);
done();
});
});

it('evaluate message function for required field gh6523', function(done) {
const s = mongoose.Schema({
n: {
Expand Down