@bedrockio/model 0.18.2 → 0.18.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,7 @@
1
+ ## 0.18.3
2
+
3
+ - Fixed issues with `getUpdateValidation` not allowing null for nested objects.
4
+
1
5
  ## 0.18.2
2
6
 
3
7
  - Further fix for `reload` not working with delete hooks.
@@ -183,7 +183,7 @@ function getObjectSchema(arg, options) {
183
183
  if ((0, _utils.isSchemaTypedef)(arg)) {
184
184
  return getSchemaForTypedef(arg, options);
185
185
  } else if ((0, _utils.isMongooseSchema)(arg)) {
186
- return getObjectSchema(arg.obj, options);
186
+ return getNestedSchema(arg.obj, options);
187
187
  } else if (Array.isArray(arg)) {
188
188
  return getArraySchema(arg, options);
189
189
  } else if (typeof arg === 'object') {
@@ -195,7 +195,7 @@ function getObjectSchema(arg, options) {
195
195
  const map = {};
196
196
  for (let [key, field] of Object.entries(arg)) {
197
197
  if (!isExcludedField(field, options)) {
198
- map[key] = getObjectSchema(field, options);
198
+ map[key] = getNestedSchema(field, options);
199
199
  }
200
200
  }
201
201
  let schema = _yada.default.object(map);
@@ -214,11 +214,21 @@ function getObjectSchema(arg, options) {
214
214
  allowFlatKeys: true
215
215
  });
216
216
  }
217
+ if (isNullAllowed(options)) {
218
+ schema = schema.nullable();
219
+ }
217
220
  return schema;
218
221
  } else {
219
222
  return getSchemaForType(arg, options);
220
223
  }
221
224
  }
225
+ function getNestedSchema(arg, options) {
226
+ return getObjectSchema(arg, {
227
+ ...options,
228
+ isNested: true,
229
+ isArrayElement: false
230
+ });
231
+ }
222
232
  function getArraySchema(arr, options) {
223
233
  let schema;
224
234
  if (arr.length === 0) {
@@ -228,7 +238,8 @@ function getArraySchema(arr, options) {
228
238
  // validations as they are a new context.
229
239
  schema = getObjectSchema(arr[0], {
230
240
  ...options,
231
- skipRequired: false
241
+ skipRequired: false,
242
+ isArrayElement: true
232
243
  });
233
244
  if (!options.unwindArrayFields) {
234
245
  schema = _yada.default.array(schema);
@@ -255,7 +266,7 @@ function getSchemaForTypedef(typedef, options = {}) {
255
266
  // Null may be allowed to unset non-required fields
256
267
  // in an update operation or to search for non-existent
257
268
  // fields in a search operation.
258
- if (allowNull(typedef, options)) {
269
+ if (isNullAllowedForTypedef(typedef, options)) {
259
270
  schema = schema.nullable();
260
271
  }
261
272
 
@@ -371,8 +382,16 @@ function getSearchSchema(schema, type) {
371
382
  function isRequired(typedef, options) {
372
383
  return typedef.required && !typedef.default && !options.skipRequired;
373
384
  }
374
- function allowNull(typedef, options) {
375
- if (!options.allowNull) {
385
+ function isNullAllowed(options) {
386
+ const {
387
+ allowNull,
388
+ isNested,
389
+ isArrayElement
390
+ } = options;
391
+ return allowNull && isNested && !isArrayElement;
392
+ }
393
+ function isNullAllowedForTypedef(typedef, options) {
394
+ if (!isNullAllowed(options)) {
376
395
  return false;
377
396
  }
378
397
  const {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.18.2",
3
+ "version": "0.18.3",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/validation.js CHANGED
@@ -200,15 +200,17 @@ function getObjectSchema(arg, options) {
200
200
  if (isSchemaTypedef(arg)) {
201
201
  return getSchemaForTypedef(arg, options);
202
202
  } else if (isMongooseSchema(arg)) {
203
- return getObjectSchema(arg.obj, options);
203
+ return getNestedSchema(arg.obj, options);
204
204
  } else if (Array.isArray(arg)) {
205
205
  return getArraySchema(arg, options);
206
206
  } else if (typeof arg === 'object') {
207
207
  const { stripUnknown, stripEmpty, allowFlatKeys } = options;
208
+
208
209
  const map = {};
210
+
209
211
  for (let [key, field] of Object.entries(arg)) {
210
212
  if (!isExcludedField(field, options)) {
211
- map[key] = getObjectSchema(field, options);
213
+ map[key] = getNestedSchema(field, options);
212
214
  }
213
215
  }
214
216
 
@@ -232,12 +234,24 @@ function getObjectSchema(arg, options) {
232
234
  });
233
235
  }
234
236
 
237
+ if (isNullAllowed(options)) {
238
+ schema = schema.nullable();
239
+ }
240
+
235
241
  return schema;
236
242
  } else {
237
243
  return getSchemaForType(arg, options);
238
244
  }
239
245
  }
240
246
 
247
+ function getNestedSchema(arg, options) {
248
+ return getObjectSchema(arg, {
249
+ ...options,
250
+ isNested: true,
251
+ isArrayElement: false,
252
+ });
253
+ }
254
+
241
255
  function getArraySchema(arr, options) {
242
256
  let schema;
243
257
  if (arr.length === 0) {
@@ -248,6 +262,7 @@ function getArraySchema(arr, options) {
248
262
  schema = getObjectSchema(arr[0], {
249
263
  ...options,
250
264
  skipRequired: false,
265
+ isArrayElement: true,
251
266
  });
252
267
  if (!options.unwindArrayFields) {
253
268
  schema = yd.array(schema);
@@ -275,7 +290,7 @@ function getSchemaForTypedef(typedef, options = {}) {
275
290
  // Null may be allowed to unset non-required fields
276
291
  // in an update operation or to search for non-existent
277
292
  // fields in a search operation.
278
- if (allowNull(typedef, options)) {
293
+ if (isNullAllowedForTypedef(typedef, options)) {
279
294
  schema = schema.nullable();
280
295
  }
281
296
 
@@ -407,8 +422,13 @@ function isRequired(typedef, options) {
407
422
  return typedef.required && !typedef.default && !options.skipRequired;
408
423
  }
409
424
 
410
- function allowNull(typedef, options) {
411
- if (!options.allowNull) {
425
+ function isNullAllowed(options) {
426
+ const { allowNull, isNested, isArrayElement } = options;
427
+ return allowNull && isNested && !isArrayElement;
428
+ }
429
+
430
+ function isNullAllowedForTypedef(typedef, options) {
431
+ if (!isNullAllowed(options)) {
412
432
  return false;
413
433
  }
414
434
  const { required, type } = typedef;
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AA6DA,kDAEC;AAED,oEA4FC;AAsBD,wEAiBC;AA8SD;;;EAEC;AAED;;;EAOC"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AA6DA,kDAEC;AAED,oEA4FC;AAsBD,wEAiBC;AAkUD;;;EAEC;AAED;;;EAOC"}