@bedrockio/model 0.13.2 → 0.13.3

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,8 @@
1
+ ## 0.13.3
2
+
3
+ - Bumped yada version.
4
+ - Fix for empty string not unsetting nested enum string fields.
5
+
1
6
  ## 0.13.2
2
7
 
3
8
  - Bumped yada version.
@@ -30,7 +30,7 @@ function unsetReferenceFields(fields, schema = {}) {
30
30
  fields[key] = undefined;
31
31
  } else if (value instanceof _mongoose.default.Document) {
32
32
  fields[key] = value;
33
- } else if (value && typeof value === 'object') {
33
+ } else if (hasEnumerableFields(value)) {
34
34
  unsetReferenceFields(value, (0, _utils.getField)(schema, key));
35
35
  }
36
36
  }
@@ -42,11 +42,25 @@ function unsetReferenceFields(fields, schema = {}) {
42
42
  function flattenObject(obj, root = {}, rootPath = []) {
43
43
  for (let [key, val] of Object.entries(obj)) {
44
44
  const path = [...rootPath, key];
45
- if ((0, _lodash.isPlainObject)(val)) {
45
+ if (hasEnumerableFields(val)) {
46
46
  flattenObject(val, root, path);
47
47
  } else {
48
48
  root[path.join('.')] = val;
49
49
  }
50
50
  }
51
51
  return root;
52
+ }
53
+
54
+ // Only plain objects and arrays with fields should
55
+ // be enumerated to allow setting of their inner fields.
56
+ // Note that mongoose documents etc should NOT be enumerated
57
+ // over as their value should be set directly for the field.
58
+ // Additionally if an array is empty it should not have its
59
+ // fields enumerated but instead directly set the empty array
60
+ // for the field.
61
+ function hasEnumerableFields(arg) {
62
+ if ((0, _lodash.isPlainObject)(arg) || Array.isArray(arg)) {
63
+ return Object.keys(arg).length > 0;
64
+ }
65
+ return false;
52
66
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.13.2",
3
+ "version": "0.13.3",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -30,7 +30,7 @@
30
30
  "lodash": "^4.17.21"
31
31
  },
32
32
  "peerDependencies": {
33
- "@bedrockio/yada": "^1.6.0",
33
+ "@bedrockio/yada": "^1.6.1",
34
34
  "mongoose": "^8.13.1"
35
35
  },
36
36
  "devDependencies": {
@@ -39,7 +39,7 @@
39
39
  "@babel/preset-env": "^7.26.0",
40
40
  "@bedrockio/eslint-plugin": "^1.1.7",
41
41
  "@bedrockio/prettier-config": "^1.0.2",
42
- "@bedrockio/yada": "^1.6.0",
42
+ "@bedrockio/yada": "^1.6.1",
43
43
  "@shelf/jest-mongodb": "^5.1.0",
44
44
  "eslint": "^9.19.0",
45
45
  "jest": "^29.7.0",
package/src/assign.js CHANGED
@@ -25,7 +25,7 @@ function unsetReferenceFields(fields, schema = {}) {
25
25
  fields[key] = undefined;
26
26
  } else if (value instanceof mongoose.Document) {
27
27
  fields[key] = value;
28
- } else if (value && typeof value === 'object') {
28
+ } else if (hasEnumerableFields(value)) {
29
29
  unsetReferenceFields(value, getField(schema, key));
30
30
  }
31
31
  }
@@ -37,7 +37,7 @@ function unsetReferenceFields(fields, schema = {}) {
37
37
  function flattenObject(obj, root = {}, rootPath = []) {
38
38
  for (let [key, val] of Object.entries(obj)) {
39
39
  const path = [...rootPath, key];
40
- if (isPlainObject(val)) {
40
+ if (hasEnumerableFields(val)) {
41
41
  flattenObject(val, root, path);
42
42
  } else {
43
43
  root[path.join('.')] = val;
@@ -45,3 +45,17 @@ function flattenObject(obj, root = {}, rootPath = []) {
45
45
  }
46
46
  return root;
47
47
  }
48
+
49
+ // Only plain objects and arrays with fields should
50
+ // be enumerated to allow setting of their inner fields.
51
+ // Note that mongoose documents etc should NOT be enumerated
52
+ // over as their value should be set directly for the field.
53
+ // Additionally if an array is empty it should not have its
54
+ // fields enumerated but instead directly set the empty array
55
+ // for the field.
56
+ function hasEnumerableFields(arg) {
57
+ if (isPlainObject(arg) || Array.isArray(arg)) {
58
+ return Object.keys(arg).length > 0;
59
+ }
60
+ return false;
61
+ }