@bedrockio/model 0.2.9 → 0.2.11

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.
@@ -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,
@@ -145,6 +146,7 @@ function applyValidation(schema, definition) {
145
146
  });
146
147
  schema.static('getBaseSchema', function getBaseSchema() {
147
148
  return getSchemaFromMongoose(schema, {
149
+ model: this,
148
150
  stripDeleted: true,
149
151
  requireReadAccess: true
150
152
  });
@@ -267,6 +269,8 @@ function getSchemaForTypedef(typedef, options = {}) {
267
269
  }
268
270
  if (isRequired(typedef, options)) {
269
271
  schema = schema.required();
272
+ } else if (allowsNull(typedef, options)) {
273
+ schema = _yada.default.allow(null, schema);
270
274
  }
271
275
  if (typedef.default && options.allowDefaultTags) {
272
276
  // Tag the default value to allow OpenAPI description
@@ -374,6 +378,16 @@ function getSearchSchema(schema, type) {
374
378
  function isRequired(typedef, options) {
375
379
  return typedef.required && !typedef.default && !options.skipRequired;
376
380
  }
381
+ function allowsNull(typedef, options) {
382
+ if (!options.allowNullForPrimitives) {
383
+ return false;
384
+ }
385
+ return !typedef.required && isPrimitiveTypedef(typedef);
386
+ }
387
+ const PRIMITIVE_TYPES = ['String', 'Number', 'Boolean'];
388
+ function isPrimitiveTypedef(typedef) {
389
+ return PRIMITIVE_TYPES.includes(typedef.type);
390
+ }
377
391
  function isExcludedField(field, options) {
378
392
  if ((0, _utils.isSchemaTypedef)(field)) {
379
393
  if (options.requireWriteAccess) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
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,
@@ -162,6 +163,7 @@ export function applyValidation(schema, definition) {
162
163
 
163
164
  schema.static('getBaseSchema', function getBaseSchema() {
164
165
  return getSchemaFromMongoose(schema, {
166
+ model: this,
165
167
  stripDeleted: true,
166
168
  requireReadAccess: true,
167
169
  });
@@ -280,6 +282,8 @@ function getSchemaForTypedef(typedef, options = {}) {
280
282
 
281
283
  if (isRequired(typedef, options)) {
282
284
  schema = schema.required();
285
+ } else if (allowsNull(typedef, options)) {
286
+ schema = yd.allow(null, schema);
283
287
  }
284
288
 
285
289
  if (typedef.default && options.allowDefaultTags) {
@@ -429,6 +433,19 @@ function isRequired(typedef, options) {
429
433
  return typedef.required && !typedef.default && !options.skipRequired;
430
434
  }
431
435
 
436
+ function allowsNull(typedef, options) {
437
+ if (!options.allowNullForPrimitives) {
438
+ return false;
439
+ }
440
+ return !typedef.required && isPrimitiveTypedef(typedef);
441
+ }
442
+
443
+ const PRIMITIVE_TYPES = ['String', 'Number', 'Boolean'];
444
+
445
+ function isPrimitiveTypedef(typedef) {
446
+ return PRIMITIVE_TYPES.includes(typedef.type);
447
+ }
448
+
432
449
  function isExcludedField(field, options) {
433
450
  if (isSchemaTypedef(field)) {
434
451
  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,oEAqFC;AAsBD,wEAkBC;AAySD;;;EAEC;AAED;;;EAOC;AApfD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}