-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Linting, .editorconfig, prep for travis-ci, code cleanup
- Loading branch information
Showing
8 changed files
with
203 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ Desktop.ini | |
$RECYCLE.BIN/ | ||
node_modules/ | ||
.tmp | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- 0.12 | ||
- 4.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -20,7 +22,7 @@ module.exports = { | |
}, | ||
|
||
createCustomUserSchema: function() { | ||
return mongoose.Schema({ | ||
return new mongoose.Schema({ | ||
username: { | ||
type: String, | ||
unique: 'Username is already used.' | ||
|
@@ -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]', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters