ac-sanitizer 3.9.16 → 3.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/README.md +1 -0
- package/index.js +84 -51
- package/package.json +1 -1
- package/test/suites/default.js +10 -0
- package/test/tests/any.js +48 -0
- package/test/tests/array.js +4 -2
- package/test/tests/bool.js +1 -0
- package/test/tests/country.js +2 -1
- package/test/tests/date.js +2 -1
- package/test/tests/error.js +46 -0
- package/test/tests/index.js +2 -0
- package/test/tests/iso639.js +6 -3
- package/test/tests/number.js +3 -1
- package/test/tests/string.js +3 -1
- package/test/tests/stringOrInteger.js +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
+
<a name="3.10.0"></a>
|
|
2
|
+
|
|
3
|
+
# [3.10.0](https://github.com/mmpro/ac-sanitizer/compare/v3.9.18..v3.10.0) (2022-04-24 18:45:04)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Feature
|
|
7
|
+
|
|
8
|
+
* **App:** Add type any | MP | [ff0858e978b7f7195f68ad5c9c346723107075fb](https://github.com/mmpro/ac-sanitizer/commit/ff0858e978b7f7195f68ad5c9c346723107075fb)
|
|
9
|
+
Type any will try to determine the actual type but gives you flexibility while sanitizing.
|
|
10
|
+
Related issues: [/issues#undefined](https://github.com//issues/undefined)
|
|
11
|
+
<a name="3.9.18"></a>
|
|
12
|
+
|
|
13
|
+
## [3.9.18](https://github.com/mmpro/ac-sanitizer/compare/v3.9.17..v3.9.18) (2022-04-24 18:09:53)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Bug Fix
|
|
17
|
+
|
|
18
|
+
* **App:** Minor fix for special fields | MP | [dc5a393127a74ebe948c8c5c953969688463422b](https://github.com/mmpro/ac-sanitizer/commit/dc5a393127a74ebe948c8c5c953969688463422b)
|
|
19
|
+
Only check if they are in payload.
|
|
20
|
+
Related issues: [/issues#undefined](https://github.com//issues/undefined)
|
|
21
|
+
<a name="3.9.17"></a>
|
|
22
|
+
|
|
23
|
+
## [3.9.17](https://github.com/mmpro/ac-sanitizer/compare/v3.9.16..v3.9.17) (2022-04-24 14:58:01)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Bug Fix
|
|
27
|
+
|
|
28
|
+
* **App:** Check required fields first | MP | [9dacfd1f2849f6c4b96a211b37f40ec0064ed456](https://github.com/mmpro/ac-sanitizer/commit/9dacfd1f2849f6c4b96a211b37f40ec0064ed456)
|
|
29
|
+
Check requirements before any other checks are made.
|
|
30
|
+
Related issues: [/issues#undefined](https://github.com//issues/undefined)
|
|
31
|
+
### Tests
|
|
32
|
+
|
|
33
|
+
* **App:** Increased test coverage | MP | [3598e6a6b278ce560125ec92d8157f014db6466c](https://github.com/mmpro/ac-sanitizer/commit/3598e6a6b278ce560125ec92d8157f014db6466c)
|
|
34
|
+
Coverage now > 98%.
|
|
35
|
+
Related issues: [/issues#undefined](https://github.com//issues/undefined)
|
|
1
36
|
<a name="3.9.16"></a>
|
|
2
37
|
|
|
3
38
|
## [3.9.16](https://github.com/mmpro/ac-sanitizer/compare/v3.9.15..v3.9.16) (2022-04-24 14:23:01)
|
package/README.md
CHANGED
|
@@ -69,6 +69,7 @@ countrylist | list of country names | e.g. Laos, Brazil, Norway...
|
|
|
69
69
|
|
|
70
70
|
Type | Options | Remarks
|
|
71
71
|
--- | --- | --- |
|
|
72
|
+
any | | Any can be string, integer, boolean, object or array - it will be automatically detected.
|
|
72
73
|
base64 | | Checks if a string is base64 encoded, optional with field option "convert" (to string)
|
|
73
74
|
boolean | |
|
|
74
75
|
cidr | | Check CIDR, see example
|
package/index.js
CHANGED
|
@@ -94,39 +94,6 @@ const sanitizer = function() {
|
|
|
94
94
|
// mark deprecated fields
|
|
95
95
|
if (_.get(field, 'deprecated') && value) deprecated.push(fieldName)
|
|
96
96
|
|
|
97
|
-
/// SPECIAL FIELDS
|
|
98
|
-
// special field - can be string or integer -> determine type and then use type settings
|
|
99
|
-
if (field.type === 'integer | string') {
|
|
100
|
-
if (!_.isString(value) && !_.isFinite(parseInt(value))) {
|
|
101
|
-
error = { message: fieldName + '_neitherStringNorInteger' }
|
|
102
|
-
return {
|
|
103
|
-
error
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
if (_.isString(value)) {
|
|
107
|
-
field.type = 'string'
|
|
108
|
-
}
|
|
109
|
-
else if (_.isFinite(parseInt(value))) {
|
|
110
|
-
field.type = 'integer'
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
// special field - can be boolean or integer -> determine type and then use type settings
|
|
114
|
-
if (field.type === 'integer | boolean' && value) {
|
|
115
|
-
if (!_.isBoolean(value) && !_.isFinite(parseInt(value))) {
|
|
116
|
-
error = { message: fieldName + '_neitherBooleanNorInteger' }
|
|
117
|
-
return {
|
|
118
|
-
error
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if (_.isBoolean(value)) {
|
|
122
|
-
field.type = 'boolean'
|
|
123
|
-
}
|
|
124
|
-
else if (_.isFinite(parseInt(value))) {
|
|
125
|
-
field.type = 'integer'
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
///// END SPECIAL FIELDS
|
|
129
|
-
|
|
130
97
|
// REQUIREMENTS
|
|
131
98
|
let fieldIsRequired = false
|
|
132
99
|
if (_.has(field, 'required')) {
|
|
@@ -147,6 +114,63 @@ const sanitizer = function() {
|
|
|
147
114
|
}
|
|
148
115
|
}
|
|
149
116
|
|
|
117
|
+
if (!error && _.has(paramsToCheck, fieldName)) {
|
|
118
|
+
/// SPECIAL FIELDS
|
|
119
|
+
// field type any, can be any field
|
|
120
|
+
if (field.type === 'any') {
|
|
121
|
+
if (_.isString(value)) {
|
|
122
|
+
field.type = 'string'
|
|
123
|
+
}
|
|
124
|
+
else if (_.isPlainObject(value)) {
|
|
125
|
+
field.type = 'object'
|
|
126
|
+
}
|
|
127
|
+
else if (_.isArray(value)) {
|
|
128
|
+
field.type = 'array'
|
|
129
|
+
}
|
|
130
|
+
else if (_.isBoolean(value)) {
|
|
131
|
+
field.type = 'boolean'
|
|
132
|
+
}
|
|
133
|
+
else if (_.isFinite(parseInt(value))) {
|
|
134
|
+
field.type = 'integer'
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
error = { message: fieldName + '_any_couldNotResolveType' }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// special field - can be string or integer -> determine type and then use type settings
|
|
142
|
+
if (field.type === 'integer | string') {
|
|
143
|
+
if (!_.isString(value) && !_.isFinite(parseInt(value))) {
|
|
144
|
+
error = { message: fieldName + '_neitherStringNorInteger' }
|
|
145
|
+
return {
|
|
146
|
+
error
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (_.isString(value)) {
|
|
150
|
+
field.type = 'string'
|
|
151
|
+
}
|
|
152
|
+
else if (_.isFinite(parseInt(value))) {
|
|
153
|
+
field.type = 'integer'
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// special field - can be boolean or integer -> determine type and then use type settings
|
|
157
|
+
if (field.type === 'integer | boolean' && value) {
|
|
158
|
+
if (!_.isBoolean(value) && !_.isFinite(parseInt(value))) {
|
|
159
|
+
error = { message: fieldName + '_neitherBooleanNorInteger' }
|
|
160
|
+
return {
|
|
161
|
+
error
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (_.isBoolean(value)) {
|
|
165
|
+
field.type = 'boolean'
|
|
166
|
+
}
|
|
167
|
+
else if (_.isFinite(parseInt(value))) {
|
|
168
|
+
field.type = 'integer'
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
///// END SPECIAL FIELDS
|
|
172
|
+
}
|
|
173
|
+
|
|
150
174
|
if (error) {
|
|
151
175
|
// do not process other conditions
|
|
152
176
|
}
|
|
@@ -168,27 +192,36 @@ const sanitizer = function() {
|
|
|
168
192
|
}
|
|
169
193
|
else if (_.indexOf(['number', 'integer', 'long', 'short', 'float'], field.type) > -1) {
|
|
170
194
|
if (typeof value != 'number' || isNaN(value)) error = { message: fieldName + '_' + getTypeMapping('integer', 'errorMessage') }
|
|
195
|
+
else {
|
|
196
|
+
// Number types - usually we allow only non-negative values (unsigned). If you want negative values, set type.subtype 'signed'
|
|
197
|
+
if (field.type === 'number') console.error('SANITIZER - number should not be used, be more precise')
|
|
198
|
+
if (field.type === 'number') field.type = 'integer'
|
|
171
199
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
200
|
+
if (field.type === 'float' && value !== parseFloat(value)) {
|
|
201
|
+
error = { message: fieldName + '_not_' + field.type, additionalInfo: { value } }
|
|
202
|
+
}
|
|
203
|
+
else if (value !== parseInt(value)) {
|
|
204
|
+
error = { message: fieldName + '_typeIncorrect', additionalInfo: { value, int: parseInt(value) } }
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
const subtype = _.get(field, 'subtype')
|
|
208
|
+
let ranges = {
|
|
209
|
+
integer: [(subtype === 'signed' ? -Math.pow(2, 31) : 0), Math.pow(2, 31)],
|
|
210
|
+
long: [(subtype === 'signed' ? -(Math.pow(2,53) - 1) : 0), Math.pow(2, 53)-1],
|
|
211
|
+
short: [(subtype === 'signed' ? -Math.pow(2, 15) : 0), Math.pow(2, 15)],
|
|
212
|
+
float: [(subtype === 'signed' ? -Math.pow(2, 31) : 0), Math.pow(2, 31)]
|
|
213
|
+
}
|
|
214
|
+
let range = _.get(field, 'range', _.get(ranges, field.type, ranges.integer))
|
|
184
215
|
|
|
185
|
-
|
|
186
|
-
|
|
216
|
+
if (field.type === 'float') value = parseFloat(value)
|
|
217
|
+
else value = parseInt(value)
|
|
187
218
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
219
|
+
let lowest = _.first(range)
|
|
220
|
+
let highest = _.size(range) === 2 && _.last(range)
|
|
221
|
+
if (value < lowest || (highest && value > highest)) {
|
|
222
|
+
error = { message: fieldName + '_outOfRange', additionalInfo: { range, value } }
|
|
223
|
+
}
|
|
224
|
+
}
|
|
192
225
|
}
|
|
193
226
|
}
|
|
194
227
|
else if (field.type === 'string') {
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"author": "Mark Poepping (https://www.admiralcloud.com)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "admiralcloud/ac-sanitizer",
|
|
7
|
-
"version": "3.
|
|
7
|
+
"version": "3.10.0",
|
|
8
8
|
"homepage": "https://www.admiralcloud.com",
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"ac-countrylist": "^1.0.6",
|
package/test/suites/default.js
CHANGED
|
@@ -7,6 +7,16 @@ const timeOut = 60000
|
|
|
7
7
|
module.exports = {
|
|
8
8
|
testsuite: function() {
|
|
9
9
|
|
|
10
|
+
describe('ERROR', function() {
|
|
11
|
+
this.timeout(timeOut)
|
|
12
|
+
tests.error.test()
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
describe('ANY', function() {
|
|
16
|
+
this.timeout(timeOut)
|
|
17
|
+
tests.any.test()
|
|
18
|
+
})
|
|
19
|
+
|
|
10
20
|
describe('STRING', function() {
|
|
11
21
|
this.timeout(timeOut)
|
|
12
22
|
tests.string.test()
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const _ = require('lodash')
|
|
2
|
+
const expect = require('expect')
|
|
3
|
+
const sanitizer = require('../../index')
|
|
4
|
+
|
|
5
|
+
module.exports = {
|
|
6
|
+
|
|
7
|
+
test: () => {
|
|
8
|
+
|
|
9
|
+
const baseTests = [
|
|
10
|
+
{ name: 'Any - with string', type: 'any', value: 'admiralcloud', expected: 'admiralcloud' },
|
|
11
|
+
{ name: 'Any - with integer', type: 'any', value: 123, expected: 123 },
|
|
12
|
+
{ name: 'Any - with boolean', type: 'any', value: true, expected: true },
|
|
13
|
+
{ name: 'Any - with boolean - false', type: 'any', value: false, expected: false },
|
|
14
|
+
{ name: 'Any - with object', type: 'any', value: { a: 123 }, expected: { a: 123 } },
|
|
15
|
+
{ name: 'Any - with array', type: 'any', value: [1,2,3], expected: [1,2,3] },
|
|
16
|
+
{ name: 'Any - with array of objects', type: 'any', value: [{ a: 123 }], expected: [{ a: 123 }] },
|
|
17
|
+
{ name: 'Any - with float - cannot auto-detect', type: 'any', value: 123.235, error: 'any_typeIncorrect', additionalInfo: { value: 123.235, int: 123 } },
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
_.forEach(baseTests, (test) => {
|
|
21
|
+
it(test.name, (done) => {
|
|
22
|
+
let fieldsToCheck = {
|
|
23
|
+
params: {
|
|
24
|
+
any: _.get(test, 'value')
|
|
25
|
+
},
|
|
26
|
+
fields: [
|
|
27
|
+
{ field: 'any', type: _.get(test, 'type') }
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
let r = sanitizer.checkAndSanitizeValues(fieldsToCheck)
|
|
31
|
+
if (_.get(test, 'error')) {
|
|
32
|
+
expect(_.get(r, 'error.message')).toEqual(test.error)
|
|
33
|
+
if (_.get(test, 'additionalInfo')) {
|
|
34
|
+
expect(_.get(r, 'error.additionalInfo')).toEqual(_.get(test, 'additionalInfo'))
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
expect(_.get(r, 'params.any')).toEqual(_.get(test, 'expected'))
|
|
39
|
+
}
|
|
40
|
+
return done()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
}
|
|
48
|
+
}
|
package/test/tests/array.js
CHANGED
|
@@ -5,10 +5,12 @@ const sanitizer = require('../../index')
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
|
|
7
7
|
test: () => {
|
|
8
|
+
const randomValueInt = sanitizer.randomValue({ type: 'array', valueType: 'integer' })
|
|
9
|
+
const randomValueString = sanitizer.randomValue({ type: 'array' })
|
|
8
10
|
|
|
9
11
|
const baseTests = [
|
|
10
|
-
{ name: 'Valid array of numbers', type: 'array', value:
|
|
11
|
-
{ name: 'Valid array of strings', type: 'array', value:
|
|
12
|
+
{ name: 'Valid array of numbers', type: 'array', value: randomValueInt, expected: randomValueInt},
|
|
13
|
+
{ name: 'Valid array of strings', type: 'array', value: randomValueString, expected: randomValueString },
|
|
12
14
|
{ name: 'Invalid array', type: 'array', value: 'a', error: 'array_notAnArray' },
|
|
13
15
|
{ name: 'Array with enum match', type: 'array', value: ['video'], enum: ['audio', 'video'], expected: ['video'] },
|
|
14
16
|
{ name: 'Array without enum match', type: 'array', value: ['cookie'], enum: ['audio', 'video'], error: 'array_notAnAllowedValue' },
|
package/test/tests/bool.js
CHANGED
|
@@ -9,6 +9,7 @@ module.exports = {
|
|
|
9
9
|
const baseTests = [
|
|
10
10
|
{ name: 'Valid bool', type: 'boolean', value: true, expected: true },
|
|
11
11
|
{ name: 'Valid bool', type: 'boolean', value: false, expected: false },
|
|
12
|
+
{ name: 'Valid bool from randomValue function', type: 'boolean', value: sanitizer.randomValue({ type: 'boolean' }), expected: true },
|
|
12
13
|
{ name: 'Valid bool as string', type: 'boolean', value: 'true', expected: true },
|
|
13
14
|
{ name: 'Valid bool as string', type: 'boolean', value: 'false', expected: false },
|
|
14
15
|
{ name: 'Invalid - value is integer', type: 'boolean', value: 123, error: 'bool_notBoolean' },
|
package/test/tests/country.js
CHANGED
|
@@ -5,9 +5,10 @@ const sanitizer = require('../../index')
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
|
|
7
7
|
test: () => {
|
|
8
|
+
const randomValue = sanitizer.randomValue({ type: 'countryCode' })
|
|
8
9
|
|
|
9
10
|
const baseTests = [
|
|
10
|
-
{ name: 'Valid countryCode', type: 'countryCode', value:
|
|
11
|
+
{ name: 'Valid countryCode', type: 'countryCode', value: randomValue, expected: randomValue },
|
|
11
12
|
{ name: 'Invalid countryCode', type: 'countryCode', value: 'germany', error: 'countryCode_notAValidCountryCode' },
|
|
12
13
|
]
|
|
13
14
|
|
package/test/tests/date.js
CHANGED
|
@@ -15,6 +15,7 @@ module.exports = {
|
|
|
15
15
|
{ name: 'Invalid date with .', type: 'date', value: '32.01.2020', error: 'date_notaDate' },
|
|
16
16
|
{ name: 'Invalid date', type: 'date', value: '2020-13-30', error: 'date_notaDate' },
|
|
17
17
|
{ name: 'Valid DateTime ISO8601', type: 'date', value: '2020-10-17T16:34:50+02:00', expected: '2020-10-17T16:34:50+02:00' },
|
|
18
|
+
{ name: 'Invalid date with custom date format', type: 'date', dateFormat: 'mmm-xyt', value: '2020-13-30', error: 'date_notaDate' },
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
]
|
|
@@ -25,7 +26,7 @@ module.exports = {
|
|
|
25
26
|
date: _.get(test, 'value')
|
|
26
27
|
},
|
|
27
28
|
fields: [
|
|
28
|
-
{ field: 'date', type: _.get(test, 'type'), required: _.get(test, 'required') }
|
|
29
|
+
{ field: 'date', type: _.get(test, 'type'), required: _.get(test, 'required'), dateFormat: _.get(test, 'dateFormat') }
|
|
29
30
|
]
|
|
30
31
|
}
|
|
31
32
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const _ = require('lodash')
|
|
2
|
+
const expect = require('expect')
|
|
3
|
+
const sanitizer = require('../../index')
|
|
4
|
+
|
|
5
|
+
module.exports = {
|
|
6
|
+
|
|
7
|
+
test: () => {
|
|
8
|
+
|
|
9
|
+
const baseTests = [
|
|
10
|
+
{ name: 'Non-existing type', type: 'xyt', value: 'abc', error: 'errorField_typeCheck_xyt_notDefined' },
|
|
11
|
+
{ name: 'AdminLevel not sufficient', type: 'string', value: 'abc', adminLevel: 10, error: 'fieldName_adminLevelNotSufficient' },
|
|
12
|
+
{ name: 'AdminLevel not sufficient, omit fields', omitFields: true, type: 'string', value: 'abc', adminLevel: 10, expected: undefined }
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
_.forEach(baseTests, (test) => {
|
|
17
|
+
it(test.name, (done) => {
|
|
18
|
+
let fieldsToCheck = {
|
|
19
|
+
adminLevel: 4,
|
|
20
|
+
params: {
|
|
21
|
+
errorField: _.get(test, 'value'),
|
|
22
|
+
},
|
|
23
|
+
fields: [
|
|
24
|
+
{ field: 'errorField', type: _.get(test, 'type'), required: _.get(test, 'required'), adminLevel: _.get(test, 'adminLevel'), omitFields: _.get(test, 'omitFields') }
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let r = sanitizer.checkAndSanitizeValues(fieldsToCheck)
|
|
29
|
+
if (_.get(test, 'error')) {
|
|
30
|
+
expect(_.get(r, 'error.message')).toEqual(test.error)
|
|
31
|
+
if (_.get(test, 'additionalInfo')) {
|
|
32
|
+
expect(_.get(r, 'error.additionalInfo')).toEqual(_.get(test, 'additionalInfo'))
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
expect(_.get(r, 'params.email')).toEqual(_.get(test, 'expected'))
|
|
37
|
+
}
|
|
38
|
+
return done()
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
}
|
|
46
|
+
}
|
package/test/tests/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
+
any: require('./any'),
|
|
2
3
|
array: require('./array'),
|
|
3
4
|
base64: require('./base64'),
|
|
4
5
|
bool: require('./bool'),
|
|
@@ -7,6 +8,7 @@ module.exports = {
|
|
|
7
8
|
country: require('./country'),
|
|
8
9
|
date: require('./date'),
|
|
9
10
|
email: require('./email'),
|
|
11
|
+
error: require('./error'),
|
|
10
12
|
fileExtension: require('./fileExtension'),
|
|
11
13
|
fqdn: require('./fqdn'),
|
|
12
14
|
gps: require('./gps'),
|
package/test/tests/iso639.js
CHANGED
|
@@ -5,14 +5,17 @@ const sanitizer = require('../../index')
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
|
|
7
7
|
test: () => {
|
|
8
|
+
const randomValue1 = sanitizer.randomValue({ type: 'iso-639-1' })
|
|
9
|
+
const randomValue2 = sanitizer.randomValue({ type: 'iso-639-2' })
|
|
8
10
|
|
|
9
11
|
const baseTests = [
|
|
10
|
-
{ name: 'Valid ISO-639-1', type: 'iso-639-1', value:
|
|
11
|
-
{ name: 'Valid ISO-639-2', type: 'iso-639-2', value:
|
|
12
|
+
{ name: 'Valid ISO-639-1', type: 'iso-639-1', value: randomValue1, expected: randomValue1 },
|
|
13
|
+
{ name: 'Valid ISO-639-2', type: 'iso-639-2', value: randomValue2, expected: randomValue2 },
|
|
12
14
|
{ name: 'Valid ISO-639-2 with convert', type: 'iso-639-2', value: 'lao', convert: 'nativeName', expected: 'ພາສາລາວ' },
|
|
13
15
|
{ name: 'Valid ISO-639-1 - check for both', type: 'iso-639', value: 'gn', convert: 'nativeName', expected: 'Avañeẽ' },
|
|
14
16
|
{ name: 'Valid ISO-639-2 - check for both', type: 'iso-639', value: 'grn', convert: 'nativeName', expected: 'Avañeẽ' },
|
|
15
17
|
{ name: 'Invalid ISO-639-1', type: 'iso-639-1', value: 'xyz', error: 'language_notAValidIso-639-1' },
|
|
18
|
+
{ name: 'Valid ISO-639-1 with enum', type: 'iso-639-1', enum: 'iso-639-1', value: randomValue1, expected: randomValue1 },
|
|
16
19
|
]
|
|
17
20
|
|
|
18
21
|
|
|
@@ -23,7 +26,7 @@ module.exports = {
|
|
|
23
26
|
'language': _.get(test, 'value')
|
|
24
27
|
},
|
|
25
28
|
fields: [
|
|
26
|
-
{ field: 'language', type: _.get(test, 'type'), convert: _.get(test, 'convert') }
|
|
29
|
+
{ field: 'language', type: _.get(test, 'type'), convert: _.get(test, 'convert'), enum: _.get(test, 'enum') }
|
|
27
30
|
]
|
|
28
31
|
}
|
|
29
32
|
|
package/test/tests/number.js
CHANGED
|
@@ -26,6 +26,8 @@ module.exports = {
|
|
|
26
26
|
|
|
27
27
|
const numberTests = []
|
|
28
28
|
_.forEach(ranges, (range, key) => {
|
|
29
|
+
const randomValue = sanitizer.randomValue({ type: key })
|
|
30
|
+
|
|
29
31
|
let minValue = _.first(range)
|
|
30
32
|
let maxValue = _.last(range)
|
|
31
33
|
let tests = []
|
|
@@ -41,7 +43,7 @@ module.exports = {
|
|
|
41
43
|
test.expected = test.value
|
|
42
44
|
}
|
|
43
45
|
if (test.value === 'random') {
|
|
44
|
-
test.value =
|
|
46
|
+
test.value = randomValue
|
|
45
47
|
test.expected = test.value
|
|
46
48
|
}
|
|
47
49
|
if (test.value === 'randomNeg') {
|
package/test/tests/string.js
CHANGED
|
@@ -5,6 +5,7 @@ const sanitizer = require('../../index')
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
|
|
7
7
|
test: () => {
|
|
8
|
+
const randomValue = sanitizer.randomValue({ type: 'string' })
|
|
8
9
|
|
|
9
10
|
const baseTests = [
|
|
10
11
|
{ name: 'Valid string', type: 'string', value: 'abc-123', expected: 'abc-123' },
|
|
@@ -14,7 +15,8 @@ module.exports = {
|
|
|
14
15
|
{ name: 'Invalid - minLength', type: 'string', value: 'ab', minLength: 3, error: 'string_stringTooShort_minLength3' },
|
|
15
16
|
{ name: 'Valid - minLength', type: 'string', value: 'abc', minLength: 3, expected: 'abc' },
|
|
16
17
|
{ name: 'Valid from enum', type: 'string', value: 'play', enum: ['play', 'pause'], expected: 'play' },
|
|
17
|
-
{ name: 'Invalid from enum', type: 'string', value: 'stop', enum: ['play', 'pause'], error: 'string_notAnAllowedValue', additionalInfo: { value: 'stop' } }
|
|
18
|
+
{ name: 'Invalid from enum', type: 'string', value: 'stop', enum: ['play', 'pause'], error: 'string_notAnAllowedValue', additionalInfo: { value: 'stop' } },
|
|
19
|
+
{ name: 'Valid string from randomValue function', type: 'string', value: randomValue, expected: randomValue },
|
|
18
20
|
]
|
|
19
21
|
|
|
20
22
|
|
|
@@ -7,23 +7,23 @@ module.exports = {
|
|
|
7
7
|
test: () => {
|
|
8
8
|
|
|
9
9
|
const baseTests = [
|
|
10
|
-
{ name: '
|
|
10
|
+
{ name: 'Missing but required value', type: 'integer | string', required: true, error: 'field_stringOrInteger_required' },
|
|
11
11
|
{ name: 'Valid empty string', type: 'integer | string', value: '', expected: '', minLength: 0 },
|
|
12
12
|
{ name: 'Valid integer', type: 'integer | string', value: 123, expected: 123 },
|
|
13
13
|
{ name: 'Invalid - neither string nor integer', type: 'integer | string', value: ['abc'], error: 'stringOrInteger_neitherStringNorInteger' },
|
|
14
|
+
{ name: 'Valid - no data - ignore', type: 'integer | string' },
|
|
14
15
|
]
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
_.forEach(baseTests, (test) => {
|
|
18
19
|
it(test.name, (done) => {
|
|
19
20
|
let fieldsToCheck = {
|
|
20
|
-
params: {
|
|
21
|
-
stringOrInteger: _.get(test, 'value')
|
|
22
|
-
},
|
|
21
|
+
params: {},
|
|
23
22
|
fields: [
|
|
24
23
|
{ field: 'stringOrInteger', type: _.get(test, 'type'), required: _.get(test, 'required') }
|
|
25
24
|
]
|
|
26
25
|
}
|
|
26
|
+
if (_.has(test, 'value')) fieldsToCheck.params.stringOrInteger = test.value
|
|
27
27
|
|
|
28
28
|
let r = sanitizer.checkAndSanitizeValues(fieldsToCheck)
|
|
29
29
|
if (_.get(test, 'error')) {
|