ac-sanitizer 4.0.15 → 4.1.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 CHANGED
@@ -1,3 +1,32 @@
1
+
2
+ # [4.1.0](https://github.com/mmpro/ac-sanitizer/compare/v4.0.16..v4.1.0) (2025-01-25 10:01:18)
3
+
4
+
5
+ ### Feature
6
+
7
+ * **App:** Add option to ingoreCase for string/enum | MP | [92c41c2a5b3e6634e8458acadee47fad1d5e5dc3](https://github.com/mmpro/ac-sanitizer/commit/92c41c2a5b3e6634e8458acadee47fad1d5e5dc3)
8
+ With ignoreCase=true enum checks are case insensitive (e.g. abc in [ABC] is valid)
9
+ Related issues:
10
+ ### Chores
11
+
12
+ * **App:** Updated packages | MP | [763b5fce7a31e867424ffb9bddb4a1b721ac0d9b](https://github.com/mmpro/ac-sanitizer/commit/763b5fce7a31e867424ffb9bddb4a1b721ac0d9b)
13
+ Updated packages
14
+ Related issues:
15
+ <a name="4.0.16"></a>
16
+
17
+ ## [4.0.16](https://github.com/mmpro/ac-sanitizer/compare/v4.0.15..v4.0.16) (2024-09-20 12:35:35)
18
+
19
+
20
+ ### Bug Fix
21
+
22
+ * **App:** Fixed handling objects with property array as null. | MP | [4e6e8035a335cc57e48bcddc5faef81d9437f588](https://github.com/mmpro/ac-sanitizer/commit/4e6e8035a335cc57e48bcddc5faef81d9437f588)
23
+ Array within object sent as null was accepted even if nullAllowed was not set.
24
+ Related issues: [undefined/undefined#master](undefined/browse/master)
25
+ ### Chores
26
+
27
+ * **App:** Updated packages | MP | [7875c76c8cbb131ab8967b1687ddf59dfd6ed1be](https://github.com/mmpro/ac-sanitizer/commit/7875c76c8cbb131ab8967b1687ddf59dfd6ed1be)
28
+ Updated packages
29
+ Related issues: [undefined/undefined#master](undefined/browse/master)
1
30
  <a name="4.0.15"></a>
2
31
 
3
32
  ## [4.0.15](https://github.com/mmpro/ac-sanitizer/compare/v4.0.14..v4.0.15) (2024-07-06 19:12:15)
package/README.md CHANGED
@@ -110,7 +110,7 @@ number | | Should no be used - use integer, long, short, floag
110
110
  ratio | | x:y
111
111
  rgb | | Check for valid RGB value (r,g,b) or (r%,g%, b%)
112
112
  short | | 0 - 2^15
113
- string | minLength (int), maxLength (int)|
113
+ string | minLength (int), maxLength (int), ignoreCase | With ignoreCase=true enum checks are case insensitive (e.g. abc in ['ABC'] is valid)
114
114
  url| protocols, require_tld, require_protocol | Default values: protocols ['http', 'https'], required_tld true, require_protocol true
115
115
 
116
116
  # Examples
package/index.js CHANGED
@@ -180,12 +180,21 @@ const sanitizer = function() {
180
180
  if (error) {
181
181
  // do not process other conditions
182
182
  }
183
- else if (!fieldIsRequired && _.isNil(_.get(paramsToCheck, fieldName))) {
184
- // do nothing -> the value is optional and not present
185
- }
186
183
  else if (field.nullAllowed && _.isNull(_.get(paramsToCheck, fieldName))) {
187
184
  // do nothing null is allowed and sent!
188
185
  }
186
+ else if (!fieldIsRequired && _.isNil(_.get(paramsToCheck, fieldName))) {
187
+ // value is optional, but sent as nil/null without nullAllowed = true
188
+ if (_.get(field, 'strict')) {
189
+ error = { message: `${fieldName}_nullNotAllowed` }
190
+ }
191
+ else {
192
+ // remove property
193
+ fields = _.filter(fields, item => {
194
+ if (item.field !== fieldName) return item
195
+ })
196
+ }
197
+ }
189
198
  else if (_.get(field, 'adminLevel') && adminLevel < _.get(field, 'adminLevel')) {
190
199
  if (omitFields) {
191
200
  fields = _.filter(fields, item => {
@@ -488,6 +497,14 @@ const sanitizer = function() {
488
497
  }
489
498
 
490
499
  if (!error && allowedValues && value) {
500
+ let orgValue
501
+ if (field.ignoreCase && _.isString(value)) {
502
+ // convert value and allowesValues to lowercase
503
+ orgValue = _.clone(orgValue)
504
+ value = _.toLower(value)
505
+ allowedValues = _.map(allowedValues, _.toLower)
506
+ }
507
+
491
508
  if (_.isArray(value)) {
492
509
  if (_.size(value) && !_.size(_.intersectionWith(value, allowedValues, _.isEqual))) {
493
510
  error = { message: fieldName + '_notAnAllowedValue', additionalInfo: { value } }
@@ -499,6 +516,8 @@ const sanitizer = function() {
499
516
  else if (_.indexOf(allowedValues, value) < 0) {
500
517
  error = { message: fieldName + '_notAnAllowedValue', additionalInfo: { value } }
501
518
  }
519
+
520
+ if (orgValue) value = orgValue
502
521
  }
503
522
 
504
523
  if (error && field.customErrorMessage) _.set(error, 'message', field.customErrorMessage)
package/package.json CHANGED
@@ -4,26 +4,26 @@
4
4
  "author": "Mark Poepping (https://www.admiralcloud.com)",
5
5
  "license": "MIT",
6
6
  "repository": "admiralcloud/ac-sanitizer",
7
- "version": "4.0.15",
7
+ "version": "4.1.0",
8
8
  "homepage": "https://www.admiralcloud.com",
9
9
  "dependencies": {
10
10
  "ac-countrylist": "^1.0.12",
11
- "ac-file-extensions": "^2.0.9",
12
- "ac-ip": "^4.1.1",
13
- "chai": "^4.4.1",
14
- "date-and-time": "^3.3.0",
11
+ "ac-file-extensions": "^2.0.10",
12
+ "ac-ip": "^4.1.2",
13
+ "chai": "^4.5.0",
14
+ "date-and-time": "^3.6.0",
15
15
  "hashids": "^2.3.0",
16
16
  "lodash": "^4.17.21",
17
17
  "validator": "^13.12.0"
18
18
  },
19
19
  "devDependencies": {
20
- "ac-semantic-release": "^0.4.2",
21
- "c8": "^10.1.2",
22
- "eslint": "^9.6.0",
23
- "mocha": "^10.6.0"
20
+ "ac-semantic-release": "^0.4.5",
21
+ "c8": "^10.1.3",
22
+ "eslint": "^9.19.0",
23
+ "mocha": "^11.1.0"
24
24
  },
25
25
  "scripts": {
26
- "test": "mocha --reporter spec",
26
+ "test": "mocha --reporter spec --bail",
27
27
  "coverage": "./node_modules/c8/bin/c8.js yarn test"
28
28
  },
29
29
  "engines": {
@@ -107,6 +107,32 @@ module.exports = {
107
107
  },
108
108
  error: "object_nested_id_notAFiniteNumber",
109
109
  },
110
+ {
111
+ name: "Object with nested properties - array as null - strict mode - should fail",
112
+ type: "object",
113
+ properties: [
114
+ { field: "arr", type: "array", valueType: "integer", strict: true }
115
+ ],
116
+ value: {
117
+ arr: null
118
+ },
119
+ error: "object_arr_nullNotAllowed",
120
+ },
121
+ {
122
+ name: "Object with nested properties - array as null - lenient - should remove the property",
123
+ type: "object",
124
+ properties: [
125
+ { field: "arr", type: "array", valueType: "integer" },
126
+ { field: "boo", type: "boolean" }
127
+ ],
128
+ value: {
129
+ arr: null,
130
+ boo: true
131
+ },
132
+ expected: {
133
+ boo: true
134
+ }
135
+ },
110
136
  {
111
137
  name: "Object with nested properties - missing nested property",
112
138
  type: "object",
@@ -115,7 +141,7 @@ module.exports = {
115
141
  { field: "enum", type: "string", enum: ["blue", "green"] },
116
142
  { field: "nested", type: "object", properties: [
117
143
  { field: 'id', type: 'integer', required: true }
118
- ]}
144
+ ] }
119
145
  ],
120
146
  value: {
121
147
  boo: true,
@@ -16,6 +16,8 @@ module.exports = {
16
16
  { name: 'Valid from enum', type: 'string', value: 'play', enum: ['play', 'pause'], expected: 'play' },
17
17
  { name: 'Invalid from enum', type: 'string', value: 'stop', enum: ['play', 'pause'], error: 'string_notAnAllowedValue', additionalInfo: { value: 'stop' } },
18
18
  { name: 'Valid string from randomValue function', type: 'string', value: randomValue, expected: randomValue },
19
+ { name: 'Invalid - uppercase vs lowercase', type: 'string', value: 'ABC', enum: ['abc'], error: 'string_notAnAllowedValue' },
20
+ { name: 'Valid - uppercase vs lowercase', type: 'string', ignoreCase: true, value: 'ABC', enum: ['abc'], expected: 'ABC' },
19
21
  ]
20
22
 
21
23
 
@@ -26,7 +28,7 @@ module.exports = {
26
28
  string: _.get(test, 'value')
27
29
  },
28
30
  fields: [
29
- { field: 'string', type: _.get(test, 'type'), required: _.get(test, 'required'), enum: _.get(test, 'enum'), minLength: _.get(test, 'minLength'), maxLength: _.get(test, 'maxLength'), convert: _.get(test, 'convert') }
31
+ { field: 'string', type: _.get(test, 'type'), ignoreCase: _.get(test, 'ignoreCase'), required: _.get(test, 'required'), enum: _.get(test, 'enum'), minLength: _.get(test, 'minLength'), maxLength: _.get(test, 'maxLength'), convert: _.get(test, 'convert') }
30
32
  ]
31
33
  }
32
34