Skip to content

Commit fca3dcd

Browse files
committed
Added editorconfig & applied formatting
1 parent 393d1b5 commit fca3dcd

3 files changed

Lines changed: 526 additions & 483 deletions

File tree

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 4
4+
charset = utf-8
5+
end_of_line = lf
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true
8+
max_line_length = 100

lib/id-validator.js

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,137 @@
1-
var mongoose = require('mongoose');
2-
var traverse = require('traverse');
3-
var clone = require('clone');
4-
var Schema = mongoose.Schema;
1+
var mongoose = require('mongoose')
2+
var traverse = require('traverse')
3+
var clone = require('clone')
4+
var Schema = mongoose.Schema
55

6-
function IdValidator() {
7-
this.enabled = true;
6+
function IdValidator () {
7+
this.enabled = true
88
}
99

1010
IdValidator.prototype.enable = function () {
11-
this.enabled = true;
12-
};
11+
this.enabled = true
12+
}
1313

1414
IdValidator.prototype.disable = function () {
15-
this.enabled = false;
16-
};
15+
this.enabled = false
16+
}
1717

1818
IdValidator.prototype.validate = function (schema, options) {
19-
var self = this;
20-
options = options || {};
21-
var message = options.message || "{PATH} references a non existing ID";
22-
var connection = options.connection || mongoose;
23-
var allowDuplicates = options.allowDuplicates || false;
19+
var self = this
20+
options = options || {}
21+
var message = options.message || '{PATH} references a non existing ID'
22+
var connection = options.connection || mongoose
23+
var allowDuplicates = options.allowDuplicates || false
2424

25-
var caller = (self instanceof IdValidator) ? self : IdValidator.prototype;
25+
var caller = (self instanceof IdValidator) ? self : IdValidator.prototype
2626

27-
return caller.validateSchema(schema, message, connection, allowDuplicates);
28-
};
27+
return caller.validateSchema(schema, message, connection, allowDuplicates)
28+
}
2929

30-
IdValidator.prototype.validateSchema = function (schema, message, connection, allowDuplicates) {
31-
var self = this;
32-
var caller = (self instanceof IdValidator) ? self : IdValidator.prototype;
30+
IdValidator.prototype.validateSchema = function (
31+
schema, message, connection, allowDuplicates) {
32+
var self = this
33+
var caller = (self instanceof IdValidator) ? self : IdValidator.prototype
3334
schema.eachPath(function (path, schemaType) {
3435

3536
//Apply validation recursively to sub-schemas
3637
if (schemaType.schema) {
37-
return caller.validateSchema(schemaType.schema, message, connection);
38+
return caller.validateSchema(schemaType.schema, message,
39+
connection)
3840
}
3941

40-
var validateFunction = null;
41-
var refModelName = null;
42-
var conditions = {};
42+
var validateFunction = null
43+
var refModelName = null
44+
var conditions = {}
4345

4446
if (schemaType.options && schemaType.options.ref) {
45-
validateFunction = validateId;
46-
refModelName = schemaType.options.ref;
47+
validateFunction = validateId
48+
refModelName = schemaType.options.ref
4749
if (schemaType.options.refConditions) {
48-
conditions = schemaType.options.refConditions;
50+
conditions = schemaType.options.refConditions
4951
}
50-
} else if (schemaType.caster && schemaType.caster.instance && schemaType.caster.options && schemaType.caster.options.ref) {
51-
validateFunction = validateIdArray;
52-
refModelName = schemaType.caster.options.ref;
52+
} else if (schemaType.caster && schemaType.caster.instance &&
53+
schemaType.caster.options && schemaType.caster.options.ref) {
54+
validateFunction = validateIdArray
55+
refModelName = schemaType.caster.options.ref
5356
if (schemaType.caster.options.refConditions) {
54-
conditions = schemaType.caster.options.refConditions;
57+
conditions = schemaType.caster.options.refConditions
5558
}
5659
}
5760

5861
if (validateFunction) {
5962
schema.path(path).validate({
6063
validator: function (value, respond) {
61-
var conditionsCopy = conditions;
64+
var conditionsCopy = conditions
6265
//A query may not implement an isModified function.
6366
if (!!this.isModified && !this.isModified(path)) {
64-
return respond(true);
67+
return respond(true)
6568
}
6669
if (!(self instanceof IdValidator) || self.enabled) {
6770
if (Object.keys(conditionsCopy).length > 0) {
68-
var instance = this;
71+
var instance = this
6972

70-
conditionsCopy = clone(conditions);
73+
conditionsCopy = clone(conditions)
7174
traverse(conditionsCopy).forEach(function (value) {
7275
if (typeof value === 'function') {
73-
this.update(value.call(instance));
76+
this.update(value.call(instance))
7477
}
75-
});
78+
})
7679
}
7780

78-
return validateFunction(this, connection, refModelName, value, conditionsCopy, respond, allowDuplicates);
81+
return validateFunction(this, connection, refModelName,
82+
value, conditionsCopy, respond, allowDuplicates)
7983
}
80-
return respond(true);
84+
return respond(true)
8185
},
8286
isAsync: true,
8387
message: message
84-
});
88+
})
8589
}
86-
});
87-
};
90+
})
91+
}
8892

89-
function executeQuery(query, conditions, validateValue, respond) {
93+
function executeQuery (query, conditions, validateValue, respond) {
9094
for (var fieldName in conditions) {
91-
query.where(fieldName, conditions[fieldName]);
95+
query.where(fieldName, conditions[fieldName])
9296
}
9397
query.exec(function (err, count) {
9498
if (err) {
95-
return respond(err);
99+
return respond(err)
96100
}
97-
respond(count === validateValue);
98-
});
101+
respond(count === validateValue)
102+
})
99103
}
100104

101-
function validateId(doc, connection, refModelName, value, conditions, respond) {
105+
function validateId (
106+
doc, connection, refModelName, value, conditions, respond) {
102107
if (value == null) {
103-
return respond(true);
108+
return respond(true)
104109
}
105-
var refModel = connection.model(refModelName);
106-
var query = refModel.count({_id: value});
107-
executeQuery(query, conditions, 1, respond);
110+
var refModel = connection.model(refModelName)
111+
var query = refModel.count({_id: value})
112+
executeQuery(query, conditions, 1, respond)
108113
}
109114

110-
function validateIdArray(doc, connection, refModelName, values, conditions, respond, allowDuplicates) {
115+
function validateIdArray (
116+
doc, connection, refModelName, values, conditions, respond,
117+
allowDuplicates) {
111118
if (values == null || values.length == 0) {
112-
return respond(true);
119+
return respond(true)
113120
}
114121

115-
var checkValues = values;
122+
var checkValues = values
116123
if (allowDuplicates) {
117124
//Extract unique values only
118125
checkValues = values.filter(function (v, i) {
119126
return values.indexOf(v) === i
120-
});
127+
})
121128
}
122129

123-
var refModel = connection.model(refModelName);
124-
var query = refModel.count().where('_id')['in'](checkValues);
130+
var refModel = connection.model(refModelName)
131+
var query = refModel.count().where('_id')['in'](checkValues)
125132

126-
executeQuery(query, conditions, checkValues.length, respond);
133+
executeQuery(query, conditions, checkValues.length, respond)
127134
}
128135

129-
module.exports = IdValidator.prototype.validate;
130-
module.exports.getConstructor = IdValidator;
136+
module.exports = IdValidator.prototype.validate
137+
module.exports.getConstructor = IdValidator

0 commit comments

Comments
 (0)