Skip to content

Commit d5f6274

Browse files
committed
chore(formatting): add Prettier and run a pass
1 parent caa71ba commit d5f6274

19 files changed

+7817
-2292
lines changed

lib/compatible-validator.js

Lines changed: 64 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -21,104 +21,96 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
* THE SOFTWARE.
2323
*/
24-
var validator = require('validator');
24+
var validator = require('validator')
2525

2626
// Because of changes in the new node-validator library we need to add a few functions
2727
// for backwards compatibility
2828
//
29-
var compatibleValidator = Object.create( validator );
30-
module.exports = compatibleValidator;
29+
var compatibleValidator = Object.create(validator)
30+
module.exports = compatibleValidator
3131

3232
// Add the min/max validators
3333
//
34-
compatibleValidator.min = function(str, val)
35-
{
36-
var number = parseFloat(str);
37-
return isNaN(number) || number >= val;
38-
};
34+
compatibleValidator.min = function (str, val) {
35+
var number = parseFloat(str)
36+
return isNaN(number) || number >= val
37+
}
3938

40-
compatibleValidator.max = function(str, val)
41-
{
42-
var number = parseFloat(str);
43-
return isNaN(number) || number <= val;
44-
};
39+
compatibleValidator.max = function (str, val) {
40+
var number = parseFloat(str)
41+
return isNaN(number) || number <= val
42+
}
4543

4644
// Old isDecimal implementation doesn't exist anymore
4745
//
48-
compatibleValidator.isDecimal = function(str)
49-
{
50-
return validator.isFloat(str);
51-
};
46+
compatibleValidator.isDecimal = function (str) {
47+
return validator.isFloat(str)
48+
}
5249

5350
// notIn is just inverted isIn
5451
//
55-
compatibleValidator.notIn = function(str, options)
56-
{
57-
return !validator.isIn(str, options);
58-
};
52+
compatibleValidator.notIn = function (str, options) {
53+
return !validator.isIn(str, options)
54+
}
5955

6056
// Regular expression method now all use matches function
6157
//
62-
compatibleValidator.is = compatibleValidator.regex = function(str, pattern, modifiers)
63-
{
64-
return validator.matches(str, pattern, modifiers);
65-
};
66-
compatibleValidator.not = compatibleValidator.notRegex = function(str, pattern, modifiers)
67-
{
68-
return !validator.matches(str, pattern, modifiers);
69-
};
58+
compatibleValidator.is = compatibleValidator.regex = function (
59+
str,
60+
pattern,
61+
modifiers
62+
) {
63+
return validator.matches(str, pattern, modifiers)
64+
}
65+
compatibleValidator.not = compatibleValidator.notRegex = function (
66+
str,
67+
pattern,
68+
modifiers
69+
) {
70+
return !validator.matches(str, pattern, modifiers)
71+
}
7072

71-
compatibleValidator.notContains = function(str, elem)
72-
{
73-
return !validator.contains(str, elem);
74-
};
73+
compatibleValidator.notContains = function (str, elem) {
74+
return !validator.contains(str, elem)
75+
}
7576

7677
// New isURL validator has options
7778
//
78-
compatibleValidator.isUrl = compatibleValidator.isURL = function(str)
79-
{
80-
return validator.isURL(str);
81-
};
79+
compatibleValidator.isUrl = compatibleValidator.isURL = function (str) {
80+
return validator.isURL(str)
81+
}
8282

8383
// IP checking validators now take a version parameter
8484
//
85-
compatibleValidator.isIP = function(str)
86-
{
87-
return validator.isIP(str);
88-
};
85+
compatibleValidator.isIP = function (str) {
86+
return validator.isIP(str)
87+
}
8988

90-
compatibleValidator.isIPv4 = function(str)
91-
{
92-
return validator.isIP(str, 4);
93-
};
89+
compatibleValidator.isIPv4 = function (str) {
90+
return validator.isIP(str, 4)
91+
}
9492

95-
compatibleValidator.isIPv6 = function(str)
96-
{
97-
return validator.isIP(str, 6);
98-
};
93+
compatibleValidator.isIPv6 = function (str) {
94+
return validator.isIP(str, 6)
95+
}
9996

10097
// The isUUID validator now supports a version parameter
10198
//
102-
compatibleValidator.isUUID = function(str)
103-
{
104-
return validator.isUUID(str);
105-
};
106-
compatibleValidator.isUUIDv3 = function(str)
107-
{
108-
return validator.isUUID(str, 3);
109-
};
110-
compatibleValidator.isUUIDv4 = function(str)
111-
{
112-
return validator.isUUID(str, 4);
113-
};
114-
compatibleValidator.isUUIDv5 = function(str)
115-
{
116-
return validator.isUUID(str, 5);
117-
};
118-
compatibleValidator.isNatural = function(str)
119-
{
120-
return /^[0-9]+$/.test(str);
121-
};
122-
compatibleValidator.isBoolean = function(val) {
123-
return val === 'true' || val === 'false' || val === true || val === false;
124-
};
99+
compatibleValidator.isUUID = function (str) {
100+
return validator.isUUID(str)
101+
}
102+
compatibleValidator.isUUIDv3 = function (str) {
103+
return validator.isUUID(str, 3)
104+
}
105+
compatibleValidator.isUUIDv4 = function (str) {
106+
return validator.isUUID(str, 4)
107+
}
108+
compatibleValidator.isUUIDv5 = function (str) {
109+
return validator.isUUID(str, 5)
110+
}
111+
compatibleValidator.isNatural = function (str) {
112+
return /^[0-9]+$/.test(str)
113+
}
114+
compatibleValidator.isBoolean = function (val) {
115+
return val === 'true' || val === 'false' || val === true || val === false
116+
}

lib/conditions.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* The MIT License
33
*
44
* Copyright 2014 Timo Behrmann, Guillaume Chauvet.
@@ -21,32 +21,37 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
* THE SOFTWARE.
2323
*/
24-
var _ = require('lodash');
25-
var utils = require('./utils');
24+
var _ = require('lodash')
25+
var utils = require('./utils')
2626

2727
module.exports.paramMatches = function (params) {
28-
return conditionalChecker(params, function (matches, value) {
29-
var result;
30-
if (_.isArray(matches)) {
31-
result = _.contains(matches, value);
32-
} else {
33-
result = _.isEqual(matches, value);
34-
}
35-
return result;
36-
});
37-
};
28+
return conditionalChecker(params, function (matches, value) {
29+
var result
30+
if (_.isArray(matches)) {
31+
result = _.contains(matches, value)
32+
} else {
33+
result = _.isEqual(matches, value)
34+
}
35+
return result
36+
})
37+
}
3838

3939
module.exports.exists = function (params) {
40-
return conditionalChecker(params, function (matches, value) {
41-
return !_.isUndefined(value);
42-
});
43-
};
40+
return conditionalChecker(params, function (matches, value) {
41+
return !_.isUndefined(value)
42+
})
43+
}
4444

45-
var conditionalChecker = module.exports.conditionalChecker = function (params, validator) {
46-
return function () {
47-
var scope = !params.scope ? this.scope : utils.getExternalScope(params.scope);
48-
var variable = params.variable;
49-
var matches = params.matches;
50-
return validator(matches, this.req[scope][variable]);
51-
};
52-
};
45+
var conditionalChecker = (module.exports.conditionalChecker = function (
46+
params,
47+
validator
48+
) {
49+
return function () {
50+
var scope = !params.scope
51+
? this.scope
52+
: utils.getExternalScope(params.scope)
53+
var variable = params.variable
54+
var matches = params.matches
55+
return validator(matches, this.req[scope][variable])
56+
}
57+
})

lib/error.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,44 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
var _ = require('lodash');
25+
var _ = require('lodash')
2626

2727
function parseErrorMessage(errors, template) {
28-
var compiled = _.template(template || '<%= field %> (<%= type %>): <%= reason %>' );
29-
var key;
30-
var message = [];
28+
var compiled = _.template(
29+
template || '<%= field %> (<%= type %>): <%= reason %>'
30+
)
31+
var key
32+
var message = []
3133

32-
for (key in errors) {
33-
if (errors.hasOwnProperty(key)) {
34-
message.push(compiled({
35-
field: errors[key].field,
36-
type: errors[key].type,
37-
reason: errors[key].reason
38-
}));
39-
}
34+
for (key in errors) {
35+
if (errors.hasOwnProperty(key)) {
36+
message.push(
37+
compiled({
38+
field: errors[key].field,
39+
type: errors[key].type,
40+
reason: errors[key].reason,
41+
})
42+
)
4043
}
44+
}
4145

42-
return message.join(', ');
46+
return message.join(', ')
4347
}
4448

4549
module.exports.handle = function (errors, req, res, options, next) {
46-
if (options.handleError) {
47-
return options.handleError(res, errors, next);
48-
} else if (options.errorHandler) {
49-
res.send(new options.errorHandler(parseErrorMessage(errors, options.template)));
50-
return next(false);
51-
} else {
52-
res.send (409, {
53-
code: 'InvalidArgument',
54-
message: 'Validation failed',
55-
errors: errors
56-
});
57-
return next(false);
58-
}
59-
};
50+
if (options.handleError) {
51+
return options.handleError(res, errors, next)
52+
} else if (options.errorHandler) {
53+
res.send(
54+
new options.errorHandler(parseErrorMessage(errors, options.template))
55+
)
56+
return next(false)
57+
} else {
58+
res.send(409, {
59+
code: 'InvalidArgument',
60+
message: 'Validation failed',
61+
errors: errors,
62+
})
63+
return next(false)
64+
}
65+
}

lib/index.js

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,49 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
* THE SOFTWARE.
2323
*/
24-
var _ = require('lodash');
24+
var _ = require('lodash')
2525
var self = {
2626
validation: require('./validation'),
2727
error: require('./error'),
2828
model: require('./model'),
29-
when: require('./conditions')
30-
};
29+
when: require('./conditions'),
30+
}
3131

32-
module.exports.validation = self.validation;
33-
module.exports.error = self.error;
34-
module.exports.when = self.when;
35-
module.exports.validatorModels = self.model.validatorModels;
32+
module.exports.validation = self.validation
33+
module.exports.error = self.error
34+
module.exports.when = self.when
35+
module.exports.validatorModels = self.model.validatorModels
3636

3737
var defaultOptions = {
38-
errorsAsArray: true,
39-
errorHandler: false,
40-
forbidUndefinedVariables: false,
41-
validatorModels: {}
42-
};
38+
errorsAsArray: true,
39+
errorHandler: false,
40+
forbidUndefinedVariables: false,
41+
validatorModels: {},
42+
}
4343

4444
module.exports.validationPlugin = function (options) {
45-
options = _.extend({}, defaultOptions, options);
46-
if (_.isArray(options.validatorModels)) {
47-
// Combine list of validatorModels
48-
var validatorModels = _.toArray(options.validatorModels);
49-
validatorModels.unshift({});
50-
options.validatorModels = _.extend.apply(null, validatorModels);
51-
}
52-
return function (req, res, next) {
53-
var validationModel = req.route ? req.route.validation : undefined;
45+
options = _.extend({}, defaultOptions, options)
46+
if (_.isArray(options.validatorModels)) {
47+
// Combine list of validatorModels
48+
var validatorModels = _.toArray(options.validatorModels)
49+
validatorModels.unshift({})
50+
options.validatorModels = _.extend.apply(null, validatorModels)
51+
}
52+
return function (req, res, next) {
53+
var validationModel = req.route ? req.route.validation : undefined
5454

55-
if (validationModel) {
56-
// validate
57-
var errors = self.validation.process(validationModel, req, options);
55+
if (validationModel) {
56+
// validate
57+
var errors = self.validation.process(validationModel, req, options)
5858

59-
if (errors && (options.errorsAsArray && errors.length > 0) || (!options.errorsAsArray && _.keys(errors).length > 0)) {
60-
return self.error.handle(errors, req, res, options, next);
61-
}
62-
}
59+
if (
60+
(errors && options.errorsAsArray && errors.length > 0) ||
61+
(!options.errorsAsArray && _.keys(errors).length > 0)
62+
) {
63+
return self.error.handle(errors, req, res, options, next)
64+
}
65+
}
6366

64-
next();
65-
};
66-
};
67+
next()
68+
}
69+
}

0 commit comments

Comments
 (0)