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

Added pre-save validation for unique indexes #1571

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2372,13 +2372,48 @@ Model.compile = function compile (name, schema, collectionName, connection, base
// apply named scopes
if (schema.namedScopes) schema.namedScopes.compile(model);

// apply validation for paths with a unique index
for(var path in schema.paths) {
applyUniqueIndexValidation(model, schema, path);
};

model.schema = model.prototype.schema;
model.options = model.prototype.options;
model.collection = model.prototype.collection;

return model;
};

/**
* Adds validation for the path if its SchemaType indicates that it has a unique index.
*
* The validator checks to see if the collection already contains a document with the path’s value and throws an error
* if it does
*
* @param {Model} model
* @param {Schema} schema
* @param {String} path
* @api private
*/
function applyUniqueIndexValidation (model, schema, path) {
var schemaType = schema.paths[path];
if (schemaType._index && schemaType._index.unique) {
schemaType.validate(__checkUnique, 'duplicate');
}

function __checkUnique (value, respond) {
var conditions = {};
conditions[path] = value;
model.findOne(conditions, function (err, document) {
if (!document) {
respond(true);
} else {
respond(false);
}
});
}
}

/*!
* Subclass this model with `conn`, `schema`, and `collection` settings.
*
Expand Down
21 changes: 21 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,27 @@ describe('Model', function(){
});
})
})

describe('unique fields', function () {
it ('are validated before save', function (done) {
var db = start();
var uniqueSchema = new Schema({ uniqueField: { type: String, unique: true }, normalField: {type: String }});
var UniqueObject = db.model('UniqueObject', uniqueSchema);

new UniqueObject({ uniqueField: 'foo', normalField: 'bar' }).save(function (err) {
assert.strictEqual(null, err);
new UniqueObject({ uniqueField: 'foo', normalField: 'bar' }).save(function (err) {
assert.strictEqual('duplicate', err.errors.uniqueField.type);
assert.strictEqual('uniqueField', err.errors.uniqueField.path);
assert.strictEqual('foo', err.errors.uniqueField.value);
assert.strictEqual('undefined', typeof err.errors.normalField);

db.close();
done();
});
});
});
});
});

describe('defaults application', function(){
Expand Down