Skip to content

Commit

Permalink
Linting, .editorconfig, prep for travis-ci, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
viveleroi committed Oct 10, 2015
1 parent ce157d8 commit c251a3d
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 51 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
114 changes: 114 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"env": {
"amd": true,
"browser": true,
"node": true,
"mocha": true
},
"rules": {
"accessor-pairs": 2,
"array-bracket-spacing": [2, "never"],
"block-scoped-var": 2,
"block-spacing": [2, "always"],
"brace-style": [2, "1tbs"],
"callback-return": 2,
"camelcase": 2,
"comma-dangle": [2, "never"],
"comma-spacing": [2, {
"before": false,
"after": true
}],
"comma-style": [2, "last"],
"curly": [ 2, "all" ],
"default-case": 2,
"dot-location": [2, "property"],
"dot-notation": 2,
"eol-last": 2,
"eqeqeq": 2,
"guard-for-in": 2,
"indent": 2,
"jsx-quotes": [ 1, "prefer-single" ],
"key-spacing": [2, {
"beforeColon": false,
"afterColon": true
}],
"lines-around-comment": [2, {
"beforeBlockComment": true,
"beforeLineComment": true,
"allowBlockStart": true
}],
"linebreak-style": [2, "unix"],
"new-cap": 2,
"new-parens": 2,
"no-alert": 2,
"no-array-constructor": 2,
"no-cond-assign": 2,
"no-dupe-args": 2,
"no-dupe-keys": 2,
"no-empty": 2,
"no-eval": 2,
"no-ex-assign": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-fallthrough": 2,
"no-floating-decimal": 2,
"no-func-assign": 2,
"no-implicit-coercion": 2,
"no-implied-eval": 2,
"no-inline-comments": 2,
"no-inner-declarations": 2,
"no-irregular-whitespace": 2,
"no-iterator": 2,
"no-lone-blocks": 2,
"no-lonely-if": 2,
"no-mixed-requires": 2,
"no-mixed-spaces-and-tabs": 2,
"no-multi-spaces": 2,
"no-multiple-empty-lines": 2,
"no-native-reassign": 2,
"no-negated-in-lhs": 2,
"no-nested-ternary": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-wrappers": 2,
"no-obj-calls": 2,
"no-path-concat": 2,
"no-proto": 2,
"no-redeclare": 2,
"no-script-url": 2,
"no-self-compare": 2,
"no-sequences": 2,
"no-sparse-arrays": 2,
"no-trailing-spaces": 2,
"no-unexpected-multiline": 2,
"no-unneeded-ternary": 2,
"no-unreachable": 2,
"no-unused-vars": 2,
"no-use-before-define": 2,
"no-useless-call": 2,
"no-void": 2,
"no-with": 2,
"object-curly-spacing": [2, "always"],
"operator-assignment": [2, "always"],
"operator-linebreak": [2, "after"],
"padded-blocks": [2, "never"],
"quote-props": [2, "as-needed"],
"quotes": [2, "single"],
"radix": 2,
"semi": [2, "always"],
"space-after-keywords": 2,
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, "never"],
"space-before-keywords": [2, "always"],
"space-in-parens": [2, "never"],
"space-infix-ops": 2,
"space-return-throw-case": 2,
"space-unary-ops": 2,
"spaced-comment": 2,
"use-isnan": 2,
"valid-jsdoc": 2,
"valid-typeof": 2,
"wrap-iife": 2,
"yoda": [ 2, "never" ]
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Desktop.ini
$RECYCLE.BIN/
node_modules/
.tmp
npm-debug.log
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.12
- 4.0.0
88 changes: 52 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,61 @@
module.exports = function (schema, options) {
var message = 'Error, expected `{PATH}` to be unique. Value: `{VALUE}`';
if (options && options.message) {
message = options.message;
}
schema.eachPath(function (path, schemaType) {
if (schemaTypeHasUniqueIndex(schemaType)) {
var validator = buildUniqueValidator(path);
message = buildMessage(schemaType.options.unique, message);
schemaType.validate(validator, message);
'use strict';

/**
* Builds query to find any duplicate records.
*
* @param {string} path Path name.
* @param {string} value Value to search for.
* @param {object} id ObjectID of self
* @return {object} MongoDB query object
*/
var buildQuery = function buildQuery(path, value, id) {
// Build a base query, ensure duplicates aren't ourself
var query = { $and: [{
_id: {
$ne: id
}
});
};
}] };

function buildMessage(uniqueOption, message){
return typeof uniqueOption === 'string' ? uniqueOption : message;
}
// Match target path
var target = {};
target[path] = value;
query.$and.push(target);

function schemaTypeHasUniqueIndex(schemaType) {
return schemaType._index && schemaType._index.unique;
}
return query;
};

function buildUniqueValidator(path) {
return function (value, respond) {
/**
* Mongoose validator to be executed per-path.
*
* @param {string} path Path name.
* @return {function} Mongoose validation function
*/
var buildValidator = function buildValidator(path) {
return function(value, respond) {
// Awaiting more official way to obtain reference to model.
// https://github.com/Automattic/mongoose/issues/3430
var model = this.model(this.constructor.modelName);
var query = buildQuery(path, value, this._id);
var callback = buildValidationCallback(respond);
model.findOne(query, callback);
model.findOne(query, function(err, document) {
respond(!document);
});
};
}
};

function buildQuery(field, value, id) {
var query = { $and: [] };
var target = {};
target[field] = value;
query.$and.push({ _id: { $ne: id } });
query.$and.push(target);
return query;
}
// Export the mongoose plugin
module.exports = function(schema, options) {
var message = 'Error, expected `{PATH}` to be unique. Value: `{VALUE}`';
if (options && options.message) {
message = options.message;
}

function buildValidationCallback(respond) {
return function (err, document) {
respond(!document);
};
}
schema.eachPath(function(path, schemaType) {
if (schemaType._index && schemaType._index.unique) {
if (typeof schemaType.options.unique === 'string') {
message = schemaType.options.unique;
}

schemaType.validate(buildValidator(path), message);
}
});
};
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema.",
"main": "index.js",
"scripts": {
"test": "mocha test"
"test": "mocha test && ./node_modules/eslint/bin/eslint.js index.js test",
"lint": "eslint index.js test"
},
"keywords": [
"mongoose",
Expand All @@ -16,12 +17,10 @@
"email": "[email protected]",
"url": "http://blakehaswell.com/"
},
"contributors": [
{
"name": "Mike Botsko",
"email": "[email protected]"
}
],
"contributors": [{
"name": "Mike Botsko",
"email": "[email protected]"
}],
"license": "MIT",
"repository": {
"type": "git",
Expand Down
12 changes: 7 additions & 5 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

var mongoose = require('mongoose');

// Helper methods/objects for tests
module.exports = {
createUserSchema: function() {
return mongoose.Schema({
return new mongoose.Schema({
username: {
type: String,
unique: true
Expand All @@ -20,7 +22,7 @@ module.exports = {
},

createCustomUserSchema: function() {
return mongoose.Schema({
return new mongoose.Schema({
username: {
type: String,
unique: 'Username is already used.'
Expand All @@ -37,9 +39,9 @@ module.exports = {
},

USERS: [{
username: 'JohnSmith',
email: '[email protected]',
password: 'j0hnNYb0i'
username: 'JohnSmith',
email: '[email protected]',
password: 'j0hnNYb0i'
}, {
username: 'Robert Miller',
email: '[email protected]',
Expand Down
9 changes: 6 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var helpers = require('./helpers');
var expect = require('chai').expect;
var mongoose = require('mongoose');
Expand Down Expand Up @@ -28,7 +30,7 @@ describe('Mongoose Unique Validator', function() {
});
});

describe('Default Configuration', function () {
describe('Default Configuration', function() {
var User = mongoose.model('User', helpers.createUserSchema().plugin(uniqueValidator));
models.push(User);

Expand All @@ -38,6 +40,7 @@ describe('Mongoose Unique Validator', function() {
promise.then(function() {
// Try saving a duplicate
new User(helpers.USERS[0]).save().catch(function(err) {
// console.log(err);
expect(err.errors.username.message).to.equal('Error, expected `username` to be unique. Value: `JohnSmith`');
expect(err.errors.username.properties.type).to.equal('user defined');
expect(err.errors.username.properties.path).to.equal('username');
Expand Down Expand Up @@ -65,7 +68,7 @@ describe('Mongoose Unique Validator', function() {
});
});

describe('Custom Configuration', function () {
describe('Custom Configuration', function() {
var User = mongoose.model('UserErrorMessage', helpers.createUserSchema().plugin(uniqueValidator, {
message: 'Path: {PATH}, value: {VALUE}, type: {TYPE}'
}));
Expand All @@ -87,7 +90,7 @@ describe('Mongoose Unique Validator', function() {
});
});

describe('Configuration via Schema', function () {
describe('Configuration via Schema', function() {
var User = mongoose.model('UserErrorCustomMessage', helpers.createCustomUserSchema().plugin(uniqueValidator));
models.push(User);

Expand Down

0 comments on commit c251a3d

Please sign in to comment.