Skip to content
This repository was archived by the owner on Apr 19, 2022. It is now read-only.

Commit cae5884

Browse files
committed
fix(validator): convert message field to snake_case
all rules are referenced as snake_case wherease their corresponding messages are saved as camelCase Closes #76
1 parent 5ac5781 commit cae5884

File tree

6 files changed

+26
-37
lines changed

6 files changed

+26
-37
lines changed

src/Parser/index.js

-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
const snakeCaseRegex = /_(\w)/g
1312
const arrayExpressionRegex = /(\w[^\.\*]+)(\.\*\.?)(.+)?/
1413
const _ = require('lodash')
1514

@@ -61,19 +60,6 @@ Parser.parse = function (validations) {
6160
return _parseValidations(validationsArray)
6261
}
6362

64-
/**
65-
* convert a snake case string to camelcase.
66-
*
67-
* @param {String} string
68-
*
69-
* @return {String}
70-
*/
71-
Parser.toCamelCase = function (string) {
72-
return string.replace(snakeCaseRegex, function (m, $1) {
73-
return $1.toUpperCase()
74-
})
75-
}
76-
7763
/**
7864
* parses an array expression to a consumable object
7965
*
@@ -90,7 +76,6 @@ Parser.expressionCurryFor = function (field, whenMatched, otherwise) {
9076
return whenMatched(expression[1], expression[3])
9177
}
9278

93-
9479
/**
9580
* parses a rule and returns an object with
9681
* field name and parsed rule.

src/Sanitization/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const filters = require('./filters')
2727
* @private
2828
*/
2929
function _getSanitizationMethod (filter) {
30-
return _.get(filters, Parser.toCamelCase(filter), function () {
30+
return _.get(filters, _.camelCase(filter), function () {
3131
throw new Error(`${filter} is not defined as a filter`)
3232
})
3333
}

src/Validator/engine.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111

1212
const Validations = require('../Validations')
13-
const Parser = require('../Parser')
1413
const Messages = require('../Messages')
1514
const _ = require('lodash')
1615
const Q = require('q')
@@ -74,7 +73,7 @@ ValidationEngine.runValidationOnField = function (data, field, validation, messa
7473
* @throws {Error} If validation is not found
7574
*/
7675
ValidationEngine.getValidationMethod = function (validation) {
77-
return _.get(Validations, Parser.toCamelCase(validation), function () {
76+
return _.get(Validations, _.camelCase(validation), function () {
7877
throw new Error(`${validation} is not defined as a validation`)
7978
})
8079
}

src/Validator/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Validator.extend = function (name, method, message) {
122122
throw new Error('Invalid arguments, extend expects a method to execute')
123123
}
124124
Validations[name] = method
125-
Messages.set(name, message)
125+
Messages.set(_.snakeCase(name), message)
126126
}
127127

128128
Validator.is = require('../Raw')

test/parser.spec.js

-18
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,4 @@ describe('Parser', function() {
122122
const parsed = {password:Parser.parse(rules.password)}
123123
expect(parsed).deep.equal(parsedRules)
124124
})
125-
126-
//////////////////
127-
// test suite 5 //
128-
//////////////////
129-
it('should convert snake case values to camelcase', function () {
130-
const rule = 'alpha_numeric'
131-
const convertedRule = Parser.toCamelCase(rule)
132-
expect(convertedRule).to.equal('alphaNumeric')
133-
})
134-
135-
//////////////////
136-
// test suite 6 //
137-
//////////////////
138-
it('should convert multiple snake case values to camelcase', function () {
139-
const rule = 'before_offset_of'
140-
const convertedRule = Parser.toCamelCase(rule)
141-
expect(convertedRule).to.equal('beforeOffsetOf')
142-
})
143125
})

test/validator.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -746,4 +746,27 @@ describe('Validator', function() {
746746
const passed = yield Validator.validate(data, rules)
747747
expect(passed).deep.equal(data)
748748
})
749+
750+
it('should be able to add it\'s own validation messages to validation store', function * () {
751+
const phone = function (data, field, message, args, get) {
752+
return new Promise(function (resolve, reject) {
753+
reject(message)
754+
})
755+
}
756+
Validator.extend('isPhone', phone, 'Enter valid phone number')
757+
758+
const rules = {
759+
contact_no: 'is_phone'
760+
}
761+
const body = {}
762+
763+
try {
764+
const validated = yield Validator.validate(body, rules)
765+
expect(validated).not.to.exist()
766+
} catch (e) {
767+
expect(e).to.be.an('array')
768+
expect(e[0].validation).to.equal('is_phone')
769+
expect(e[0].message).to.equal('Enter valid phone number')
770+
}
771+
})
749772
})

0 commit comments

Comments
 (0)