ac-sanitizer 3.9.15 → 3.9.18

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 CHANGED
@@ -1,3 +1,44 @@
1
+ <a name="3.9.18"></a>
2
+
3
+ ## [3.9.18](https://github.com/mmpro/ac-sanitizer/compare/v3.9.17..v3.9.18) (2022-04-24 18:09:53)
4
+
5
+
6
+ ### Bug Fix
7
+
8
+ * **App:** Minor fix for special fields | MP | [dc5a393127a74ebe948c8c5c953969688463422b](https://github.com/mmpro/ac-sanitizer/commit/dc5a393127a74ebe948c8c5c953969688463422b)
9
+ Only check if they are in payload.
10
+ Related issues: [/issues#undefined](https://github.com//issues/undefined)
11
+ <a name="3.9.17"></a>
12
+
13
+ ## [3.9.17](https://github.com/mmpro/ac-sanitizer/compare/v3.9.16..v3.9.17) (2022-04-24 14:58:01)
14
+
15
+
16
+ ### Bug Fix
17
+
18
+ * **App:** Check required fields first | MP | [9dacfd1f2849f6c4b96a211b37f40ec0064ed456](https://github.com/mmpro/ac-sanitizer/commit/9dacfd1f2849f6c4b96a211b37f40ec0064ed456)
19
+ Check requirements before any other checks are made.
20
+ Related issues: [/issues#undefined](https://github.com//issues/undefined)
21
+ ### Tests
22
+
23
+ * **App:** Increased test coverage | MP | [3598e6a6b278ce560125ec92d8157f014db6466c](https://github.com/mmpro/ac-sanitizer/commit/3598e6a6b278ce560125ec92d8157f014db6466c)
24
+ Coverage now > 98%.
25
+ Related issues: [/issues#undefined](https://github.com//issues/undefined)
26
+ <a name="3.9.16"></a>
27
+
28
+ ## [3.9.16](https://github.com/mmpro/ac-sanitizer/compare/v3.9.15..v3.9.16) (2022-04-24 14:23:01)
29
+
30
+
31
+ ### Bug Fix
32
+
33
+ * **App:** Allow empty string for type integer | string | MP | [c06c64611a83f324d7736314e299d5fb768ab3a7](https://github.com/mmpro/ac-sanitizer/commit/c06c64611a83f324d7736314e299d5fb768ab3a7)
34
+ Allow empty string for type integer
35
+ string
36
+ Related issues: [/issues#undefined](https://github.com//issues/undefined)
37
+ ### Chores
38
+
39
+ * **App:** Updated packages | MP | [f5cf0cabe083f991f455c47df596cbe27ba12054](https://github.com/mmpro/ac-sanitizer/commit/f5cf0cabe083f991f455c47df596cbe27ba12054)
40
+ Updated packages
41
+ Related issues: [undefined/undefined#master](undefined/browse/master)
1
42
  <a name="3.9.15"></a>
2
43
 
3
44
  ## [3.9.15](https://github.com/mmpro/ac-sanitizer/compare/v3.9.14..v3.9.15) (2022-04-22 16:53:45)
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' && value) {
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,41 @@ const sanitizer = function() {
147
114
  }
148
115
  }
149
116
 
117
+ if (!error && _.has(paramsToCheck, fieldName)) {
118
+ /// SPECIAL FIELDS
119
+ // special field - can be string or integer -> determine type and then use type settings
120
+ if (field.type === 'integer | string') {
121
+ if (!_.isString(value) && !_.isFinite(parseInt(value))) {
122
+ error = { message: fieldName + '_neitherStringNorInteger' }
123
+ return {
124
+ error
125
+ }
126
+ }
127
+ if (_.isString(value)) {
128
+ field.type = 'string'
129
+ }
130
+ else if (_.isFinite(parseInt(value))) {
131
+ field.type = 'integer'
132
+ }
133
+ }
134
+ // special field - can be boolean or integer -> determine type and then use type settings
135
+ if (field.type === 'integer | boolean' && value) {
136
+ if (!_.isBoolean(value) && !_.isFinite(parseInt(value))) {
137
+ error = { message: fieldName + '_neitherBooleanNorInteger' }
138
+ return {
139
+ error
140
+ }
141
+ }
142
+ if (_.isBoolean(value)) {
143
+ field.type = 'boolean'
144
+ }
145
+ else if (_.isFinite(parseInt(value))) {
146
+ field.type = 'integer'
147
+ }
148
+ }
149
+ ///// END SPECIAL FIELDS
150
+ }
151
+
150
152
  if (error) {
151
153
  // do not process other conditions
152
154
  }
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.9.15",
7
+ "version": "3.9.18",
8
8
  "homepage": "https://www.admiralcloud.com",
9
9
  "dependencies": {
10
10
  "ac-countrylist": "^1.0.6",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "devDependencies": {
19
19
  "ac-semantic-release": "^0.3.0",
20
- "eslint": "^8.13.0",
20
+ "eslint": "^8.14.0",
21
21
  "expect": "^27.5.1",
22
22
  "mocha": "^9.2.2",
23
23
  "nyc": "^15.1.0"
@@ -7,6 +7,11 @@ 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
+
10
15
  describe('STRING', function() {
11
16
  this.timeout(timeOut)
12
17
  tests.string.test()
@@ -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: [1,2,3], expected: [1,2,3] },
11
- { name: 'Valid array of strings', type: 'array', value: ['a', 'b', 'c'], expected: ['a', 'b', 'c'] },
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' },
@@ -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' },
@@ -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: 'de', expected: 'de' },
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
 
@@ -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
+ }
@@ -7,6 +7,7 @@ module.exports = {
7
7
  country: require('./country'),
8
8
  date: require('./date'),
9
9
  email: require('./email'),
10
+ error: require('./error'),
10
11
  fileExtension: require('./fileExtension'),
11
12
  fqdn: require('./fqdn'),
12
13
  gps: require('./gps'),
@@ -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: 'lo', expected: 'lo' },
11
- { name: 'Valid ISO-639-2', type: 'iso-639-2', value: 'lao', expected: 'lao' },
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
 
@@ -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 = parseInt(Math.random()*10000)
46
+ test.value = randomValue
45
47
  test.expected = test.value
46
48
  }
47
49
  if (test.value === 'randomNeg') {
@@ -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,22 +7,23 @@ module.exports = {
7
7
  test: () => {
8
8
 
9
9
  const baseTests = [
10
- { name: 'Valid string', type: 'integer | string', value: 'abc-123', expected: 'abc-123' },
10
+ { name: 'Missing but required value', type: 'integer | string', required: true, error: 'field_stringOrInteger_required' },
11
+ { name: 'Valid empty string', type: 'integer | string', value: '', expected: '', minLength: 0 },
11
12
  { name: 'Valid integer', type: 'integer | string', value: 123, expected: 123 },
12
13
  { name: 'Invalid - neither string nor integer', type: 'integer | string', value: ['abc'], error: 'stringOrInteger_neitherStringNorInteger' },
14
+ { name: 'Valid - no data - ignore', type: 'integer | string' },
13
15
  ]
14
16
 
15
17
 
16
18
  _.forEach(baseTests, (test) => {
17
19
  it(test.name, (done) => {
18
20
  let fieldsToCheck = {
19
- params: {
20
- stringOrInteger: _.get(test, 'value')
21
- },
21
+ params: {},
22
22
  fields: [
23
23
  { field: 'stringOrInteger', type: _.get(test, 'type'), required: _.get(test, 'required') }
24
24
  ]
25
25
  }
26
+ if (_.has(test, 'value')) fieldsToCheck.params.stringOrInteger = test.value
26
27
 
27
28
  let r = sanitizer.checkAndSanitizeValues(fieldsToCheck)
28
29
  if (_.get(test, 'error')) {