@bedrockio/model 0.2.20 → 0.2.21

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.
@@ -181,6 +181,12 @@ function nodeToPopulates(node) {
181
181
  populate
182
182
  };
183
183
  }
184
+
185
+ // Null serves as a flag that the key terminates
186
+ // the branch and this is a leaf node. Using null
187
+ // here as it's simple and serializes to JSON for
188
+ // easy inspection.
189
+ const LEAF_NODE = null;
184
190
  function pathsToNode(paths, modelName) {
185
191
  const node = {};
186
192
  for (let str of paths) {
@@ -228,7 +234,7 @@ function setNodePath(node, options) {
228
234
  // -user.name - Implies population of "user" but exclude "user.name",
229
235
  // so continue traversing into object when part is "user".
230
236
  if (isExact && exclude) {
231
- node['-' + key] = null;
237
+ node['-' + key] = LEAF_NODE;
232
238
  } else if (field.ref) {
233
239
  node[key] ||= {};
234
240
  setNodePath(node[key], {
@@ -239,7 +245,7 @@ function setNodePath(node, options) {
239
245
  });
240
246
  halt = true;
241
247
  } else if ((0, _utils.isSchemaTypedef)(field)) {
242
- node[key] = null;
248
+ node[key] = LEAF_NODE;
243
249
  }
244
250
  } else if (type === 'virtual') {
245
251
  const virtual = schema.virtual(key);
@@ -105,13 +105,13 @@ function applyValidation(schema, definition) {
105
105
  model: this,
106
106
  appendSchema,
107
107
  allowInclude,
108
+ allowUnset: true,
108
109
  skipRequired: true,
109
110
  stripUnknown: true,
110
111
  stripDeleted: true,
111
112
  stripTimestamps: true,
112
113
  allowExpandedRefs: true,
113
114
  requireWriteAccess: true,
114
- allowNullForPrimitives: true,
115
115
  ...(hasUnique && {
116
116
  assertUniqueOptions: {
117
117
  schema,
@@ -270,8 +270,8 @@ function getSchemaForTypedef(typedef, options = {}) {
270
270
  }
271
271
  if (isRequired(typedef, options)) {
272
272
  schema = schema.required();
273
- } else if (allowsNull(typedef, options)) {
274
- schema = _yada.default.allow(null, schema);
273
+ } else if (allowUnset(typedef, options)) {
274
+ schema = _yada.default.allow(null, '', schema);
275
275
  }
276
276
  if (typedef.default && options.allowDefaultTags) {
277
277
  // Tag the default value to allow OpenAPI description
@@ -379,15 +379,8 @@ function getSearchSchema(schema, type) {
379
379
  function isRequired(typedef, options) {
380
380
  return typedef.required && !typedef.default && !options.skipRequired;
381
381
  }
382
- function allowsNull(typedef, options) {
383
- if (!options.allowNullForPrimitives) {
384
- return false;
385
- }
386
- return !typedef.required && isPrimitiveTypedef(typedef);
387
- }
388
- const PRIMITIVE_TYPES = ['String', 'Number', 'Boolean'];
389
- function isPrimitiveTypedef(typedef) {
390
- return PRIMITIVE_TYPES.includes(typedef.type);
382
+ function allowUnset(typedef, options) {
383
+ return options.allowUnset && !typedef.required;
391
384
  }
392
385
  function isExcludedField(field, options) {
393
386
  if ((0, _utils.isSchemaTypedef)(field)) {
@@ -0,0 +1,14 @@
1
+ // Note this file MUST be a CJS module as it will be required
2
+ // by @shelf/jest-mongodb. Note also that the binary version
3
+ // does not match the mongodb driver version.
4
+
5
+ module.exports = {
6
+ mongodbMemoryServerOptions: {
7
+ binary: {
8
+ version: '6.0.2',
9
+ skipMD5: true,
10
+ },
11
+ autoStart: false,
12
+ instance: {},
13
+ },
14
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.2.20",
3
+ "version": "0.2.21",
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.0.39",
33
+ "@bedrockio/yada": "^1.0.40",
34
34
  "mongoose": "^7.6.4"
35
35
  },
36
36
  "devDependencies": {
@@ -38,13 +38,13 @@
38
38
  "@babel/core": "^7.20.12",
39
39
  "@babel/preset-env": "^7.20.2",
40
40
  "@bedrockio/prettier-config": "^1.0.2",
41
- "@bedrockio/yada": "^1.0.39",
42
- "@shelf/jest-mongodb": "^4.1.7",
41
+ "@bedrockio/yada": "^1.0.40",
42
+ "@shelf/jest-mongodb": "^4.2.0",
43
43
  "eslint": "^8.33.0",
44
44
  "eslint-plugin-bedrock": "^1.0.26",
45
45
  "jest": "^29.4.1",
46
46
  "jest-environment-node": "^29.4.1",
47
- "mongodb": "^6.2.0",
47
+ "mongodb": "^6.5.0",
48
48
  "mongoose": "^7.6.4",
49
49
  "prettier-eslint": "^15.0.1",
50
50
  "typescript": "^4.9.5"
package/src/include.js CHANGED
@@ -170,6 +170,12 @@ function nodeToPopulates(node) {
170
170
  };
171
171
  }
172
172
 
173
+ // Null serves as a flag that the key terminates
174
+ // the branch and this is a leaf node. Using null
175
+ // here as it's simple and serializes to JSON for
176
+ // easy inspection.
177
+ const LEAF_NODE = null;
178
+
173
179
  function pathsToNode(paths, modelName) {
174
180
  const node = {};
175
181
  for (let str of paths) {
@@ -214,7 +220,7 @@ function setNodePath(node, options) {
214
220
  // -user.name - Implies population of "user" but exclude "user.name",
215
221
  // so continue traversing into object when part is "user".
216
222
  if (isExact && exclude) {
217
- node['-' + key] = null;
223
+ node['-' + key] = LEAF_NODE;
218
224
  } else if (field.ref) {
219
225
  node[key] ||= {};
220
226
  setNodePath(node[key], {
@@ -225,7 +231,7 @@ function setNodePath(node, options) {
225
231
  });
226
232
  halt = true;
227
233
  } else if (isSchemaTypedef(field)) {
228
- node[key] = null;
234
+ node[key] = LEAF_NODE;
229
235
  }
230
236
  } else if (type === 'virtual') {
231
237
  const virtual = schema.virtual(key);
package/src/validation.js CHANGED
@@ -118,13 +118,13 @@ export function applyValidation(schema, definition) {
118
118
  model: this,
119
119
  appendSchema,
120
120
  allowInclude,
121
+ allowUnset: true,
121
122
  skipRequired: true,
122
123
  stripUnknown: true,
123
124
  stripDeleted: true,
124
125
  stripTimestamps: true,
125
126
  allowExpandedRefs: true,
126
127
  requireWriteAccess: true,
127
- allowNullForPrimitives: true,
128
128
  ...(hasUnique && {
129
129
  assertUniqueOptions: {
130
130
  schema,
@@ -283,8 +283,8 @@ function getSchemaForTypedef(typedef, options = {}) {
283
283
 
284
284
  if (isRequired(typedef, options)) {
285
285
  schema = schema.required();
286
- } else if (allowsNull(typedef, options)) {
287
- schema = yd.allow(null, schema);
286
+ } else if (allowUnset(typedef, options)) {
287
+ schema = yd.allow(null, '', schema);
288
288
  }
289
289
 
290
290
  if (typedef.default && options.allowDefaultTags) {
@@ -434,17 +434,8 @@ function isRequired(typedef, options) {
434
434
  return typedef.required && !typedef.default && !options.skipRequired;
435
435
  }
436
436
 
437
- function allowsNull(typedef, options) {
438
- if (!options.allowNullForPrimitives) {
439
- return false;
440
- }
441
- return !typedef.required && isPrimitiveTypedef(typedef);
442
- }
443
-
444
- const PRIMITIVE_TYPES = ['String', 'Number', 'Boolean'];
445
-
446
- function isPrimitiveTypedef(typedef) {
447
- return PRIMITIVE_TYPES.includes(typedef.type);
437
+ function allowUnset(typedef, options) {
438
+ return options.allowUnset && !typedef.required;
448
439
  }
449
440
 
450
441
  function isExcludedField(field, options) {
@@ -11,6 +11,7 @@ export function getTupleValidator(types: any): {
11
11
  };
12
12
  export const OBJECT_ID_SCHEMA: {
13
13
  required(): any;
14
+ allowEmpty(): any;
14
15
  length(length: number): any;
15
16
  min(length: number): any;
16
17
  max(length: number): any;
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAkFA,kDAEC;AAED,oEAqFC;AAsBD,wEAkBC;AAySD;;;EAEC;AAED;;;EAOC;AArfD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAkFA,kDAEC;AAED,oEAqFC;AAsBD,wEAkBC;AAgSD;;;EAEC;AAED;;;EAOC;AA5eD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}
@@ -1,12 +0,0 @@
1
- const { devDependencies } = require('./package.json');
2
-
3
- module.exports = {
4
- mongodbMemoryServerOptions: {
5
- binary: {
6
- version: devDependencies.mongodb,
7
- skipMD5: true,
8
- },
9
- autoStart: false,
10
- instance: {},
11
- },
12
- };