@bedrockio/model 0.2.8 → 0.2.10

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.
@@ -173,18 +173,19 @@ async function errorOnForeignReferences(doc, options) {
173
173
  }
174
174
  function referenceIsAllowed(model, options) {
175
175
  const {
176
- cleanForeign
176
+ cleanForeign = {}
177
177
  } = options;
178
178
  const {
179
179
  only,
180
180
  except
181
181
  } = options?.errorHook || {};
182
+ if (model.modelName in cleanForeign) {
183
+ return true;
184
+ }
182
185
  if (only) {
183
186
  return !only.includes(model.modelName);
184
187
  } else if (except) {
185
188
  return except.includes(model.modelName);
186
- } else if (cleanForeign) {
187
- return model.modelName in cleanForeign;
188
189
  } else {
189
190
  return false;
190
191
  }
@@ -110,6 +110,7 @@ function applyValidation(schema, definition) {
110
110
  stripTimestamps: true,
111
111
  allowExpandedRefs: true,
112
112
  requireWriteAccess: true,
113
+ allowNullForPrimitives: true,
113
114
  ...(hasUnique && {
114
115
  assertUniqueOptions: {
115
116
  schema,
@@ -267,6 +268,8 @@ function getSchemaForTypedef(typedef, options = {}) {
267
268
  }
268
269
  if (isRequired(typedef, options)) {
269
270
  schema = schema.required();
271
+ } else if (allowsNull(typedef, options)) {
272
+ schema = _yada.default.allow(null, schema);
270
273
  }
271
274
  if (typedef.default && options.allowDefaultTags) {
272
275
  // Tag the default value to allow OpenAPI description
@@ -374,6 +377,16 @@ function getSearchSchema(schema, type) {
374
377
  function isRequired(typedef, options) {
375
378
  return typedef.required && !typedef.default && !options.skipRequired;
376
379
  }
380
+ function allowsNull(typedef, options) {
381
+ if (!options.allowNullForPrimitives) {
382
+ return false;
383
+ }
384
+ return !typedef.required && isPrimitiveTypedef(typedef);
385
+ }
386
+ const PRIMITIVE_TYPES = ['String', 'Number', 'Boolean'];
387
+ function isPrimitiveTypedef(typedef) {
388
+ return PRIMITIVE_TYPES.includes(typedef.type);
389
+ }
377
390
  function isExcludedField(field, options) {
378
391
  if ((0, _utils.isSchemaTypedef)(field)) {
379
392
  if (options.requireWriteAccess) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -170,14 +170,15 @@ async function errorOnForeignReferences(doc, options) {
170
170
  }
171
171
 
172
172
  function referenceIsAllowed(model, options) {
173
- const { cleanForeign } = options;
173
+ const { cleanForeign = {} } = options;
174
174
  const { only, except } = options?.errorHook || {};
175
+ if (model.modelName in cleanForeign) {
176
+ return true;
177
+ }
175
178
  if (only) {
176
179
  return !only.includes(model.modelName);
177
180
  } else if (except) {
178
181
  return except.includes(model.modelName);
179
- } else if (cleanForeign) {
180
- return model.modelName in cleanForeign;
181
182
  } else {
182
183
  return false;
183
184
  }
package/src/validation.js CHANGED
@@ -123,6 +123,7 @@ export function applyValidation(schema, definition) {
123
123
  stripTimestamps: true,
124
124
  allowExpandedRefs: true,
125
125
  requireWriteAccess: true,
126
+ allowNullForPrimitives: true,
126
127
  ...(hasUnique && {
127
128
  assertUniqueOptions: {
128
129
  schema,
@@ -280,6 +281,8 @@ function getSchemaForTypedef(typedef, options = {}) {
280
281
 
281
282
  if (isRequired(typedef, options)) {
282
283
  schema = schema.required();
284
+ } else if (allowsNull(typedef, options)) {
285
+ schema = yd.allow(null, schema);
283
286
  }
284
287
 
285
288
  if (typedef.default && options.allowDefaultTags) {
@@ -429,6 +432,19 @@ function isRequired(typedef, options) {
429
432
  return typedef.required && !typedef.default && !options.skipRequired;
430
433
  }
431
434
 
435
+ function allowsNull(typedef, options) {
436
+ if (!options.allowNullForPrimitives) {
437
+ return false;
438
+ }
439
+ return !typedef.required && isPrimitiveTypedef(typedef);
440
+ }
441
+
442
+ const PRIMITIVE_TYPES = ['String', 'Number', 'Boolean'];
443
+
444
+ function isPrimitiveTypedef(typedef) {
445
+ return PRIMITIVE_TYPES.includes(typedef.type);
446
+ }
447
+
432
448
  function isExcludedField(field, options) {
433
449
  if (isSchemaTypedef(field)) {
434
450
  if (options.requireWriteAccess) {
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAiFA,kDAEC;AAED,oEAmFC;AAsBD,wEAkBC;AA0RD;;;EAEC;AAED;;;EAOC;AAneD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAiFA,kDAEC;AAED,oEAoFC;AAsBD,wEAkBC;AAySD;;;EAEC;AAED;;;EAOC;AAnfD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}