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

feat(document): validateUpdatedOnly option. only validates modified #7492

Merged
merged 1 commit into from Feb 14, 2019
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
41 changes: 36 additions & 5 deletions lib/document.js
Expand Up @@ -1731,7 +1731,7 @@ Document.prototype.validate = function(options, callback) {
options = null;
}

return utils.promiseOrCallback(callback, cb => this.$__validate(function(error) {
return utils.promiseOrCallback(callback, cb => this.$__validate(options, function(error) {
cb(error);
}), this.constructor.events);
};
Expand Down Expand Up @@ -1862,7 +1862,23 @@ function _getPathsToValidate(doc) {
* ignore
*/

Document.prototype.$__validate = function(callback) {
Document.prototype.$__validate = function(options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}

const hasValidateModifiedOnlyOption = options &&
(typeof options === 'object') &&
('validateModifiedOnly' in options);

let shouldValidateModifiedOnly;
if (hasValidateModifiedOnlyOption) {
shouldValidateModifiedOnly = !!options.validateModifiedOnly;
} else {
shouldValidateModifiedOnly = this.schema.options.validateModifiedOnly;
}

const _this = this;
const _complete = () => {
const err = this.$__.validationError;
Expand All @@ -1884,7 +1900,9 @@ Document.prototype.$__validate = function(callback) {

// only validate required fields when necessary
const pathDetails = _getPathsToValidate(this);
const paths = pathDetails[0];
const paths = shouldValidateModifiedOnly ?
pathDetails[0].filter((path) => this.isModified(path)) :
pathDetails[0];
const skipSchemaValidators = pathDetails[1];

if (paths.length === 0) {
Expand Down Expand Up @@ -1979,16 +1997,29 @@ Document.prototype.$__validate = function(callback) {
* @api public
*/

Document.prototype.validateSync = function(pathsToValidate) {
Document.prototype.validateSync = function(pathsToValidate, options) {
const _this = this;

const hasValidateModifiedOnlyOption = options &&
(typeof options === 'object') &&
('validateModifiedOnly' in options);

let shouldValidateModifiedOnly;
if (hasValidateModifiedOnlyOption) {
shouldValidateModifiedOnly = !!options.validateModifiedOnly;
} else {
shouldValidateModifiedOnly = this.schema.options.validateModifiedOnly;
}

if (typeof pathsToValidate === 'string') {
pathsToValidate = pathsToValidate.split(' ');
}

// only validate required fields when necessary
const pathDetails = _getPathsToValidate(this);
let paths = pathDetails[0];
let paths = shouldValidateModifiedOnly ?
pathDetails[0].filter((path) => this.isModified(path)) :
pathDetails[0];
const skipSchemaValidators = pathDetails[1];

if (pathsToValidate && pathsToValidate.length) {
Expand Down
8 changes: 7 additions & 1 deletion lib/plugins/validateBeforeSave.js
Expand Up @@ -26,7 +26,13 @@ module.exports = function(schema) {

// Validate
if (shouldValidate) {
this.validate(function(error) {
const hasValidateModifiedOnlyOption = options &&
(typeof options === 'object') &&
('validateModifiedOnly' in options);
const validateOptions = hasValidateModifiedOnlyOption ?
{validateModifiedOnly: options.validateModifiedOnly} :
null;
this.validate(validateOptions, function(error) {
return _this.schema.s.hooks.execPost('save:error', _this, [ _this], { error: error }, function(error) {
next(error);
});
Expand Down
51 changes: 51 additions & 0 deletions test/document.test.js
Expand Up @@ -2276,6 +2276,57 @@ describe('document', function() {
});
});

it('does not filter validation on unmodified paths when validateModifiedOnly not set (gh-7421)', function(done) {
const testSchema = new Schema({ title: { type: String, required: true }, other: String });

const Test = db.model('gh7421_1', testSchema);

Test.create([{}], {validateBeforeSave: false}, function(createError, docs) {
assert.equal(createError, null);
const doc = docs[0];
doc.other = 'something';
assert.ok(doc.validateSync().errors);
doc.save(function(error) {
assert.ok(error.errors);
done();
});
});
});

it('filters out validation on unmodified paths when validateModifiedOnly set (gh-7421)', function(done) {
const testSchema = new Schema({ title: { type: String, required: true }, other: String });

const Test = db.model('gh7421_2', testSchema);

Test.create([{}], {validateBeforeSave: false}, function(createError, docs) {
assert.equal(createError, null);
const doc = docs[0];
doc.other = 'something';
assert.equal(doc.validateSync(undefined, {validateModifiedOnly: true}), null);
doc.save({validateModifiedOnly: true}, function(error) {
assert.equal(error, null);
done();
});
});
});

it('does not filter validation on modified paths when validateModifiedOnly set (gh-7421)', function(done) {
const testSchema = new Schema({ title: { type: String, required: true }, other: String });

const Test = db.model('gh7421_3', testSchema);

Test.create([{title: 'title'}], {validateBeforeSave: false}, function(createError, docs) {
assert.equal(createError, null);
const doc = docs[0];
doc.title = '';
assert.ok(doc.validateSync(undefined, {validateModifiedOnly: true}).errors);
doc.save({validateModifiedOnly: true}, function(error) {
assert.ok(error.errors);
done();
});
});
});

it('handles non-errors', function(done) {
const schema = new Schema({
name: { type: String, required: true }
Expand Down