@bedrockio/model 0.4.1 → 0.4.2

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.
@@ -83,6 +83,7 @@ function applyValidation(schema, definition) {
83
83
  model: this,
84
84
  appendSchema,
85
85
  allowInclude,
86
+ allowEmpty: true,
86
87
  stripDeleted: true,
87
88
  stripTimestamps: true,
88
89
  allowDefaultTags: true,
@@ -286,6 +287,10 @@ function getSchemaForTypedef(typedef, options = {}) {
286
287
  // Unsetting only allowed for primitive types.
287
288
  if (allowUnset(typedef, options)) {
288
289
  schema = _yada.default.allow(null, '', schema);
290
+ } else if (allowEmpty(typedef, options)) {
291
+ schema = schema.options({
292
+ allowEmpty: true
293
+ });
289
294
  }
290
295
  }
291
296
  if (isRequired(typedef, options)) {
@@ -400,6 +405,9 @@ function isRequired(typedef, options) {
400
405
  function allowUnset(typedef, options) {
401
406
  return options.allowUnset && !typedef.required;
402
407
  }
408
+ function allowEmpty(typedef, options) {
409
+ return options.allowEmpty && typedef.type === 'String';
410
+ }
403
411
  function isExcludedField(field, options) {
404
412
  if ((0, _utils.isSchemaTypedef)(field)) {
405
413
  if (options.requireWriteAccess) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
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.40",
33
+ "@bedrockio/yada": "^1.1.2",
34
34
  "mongoose": "^7.6.13"
35
35
  },
36
36
  "devDependencies": {
@@ -38,7 +38,7 @@
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.40",
41
+ "@bedrockio/yada": "^1.1.2",
42
42
  "@shelf/jest-mongodb": "^4.3.2",
43
43
  "eslint": "^8.33.0",
44
44
  "eslint-plugin-bedrock": "^1.0.26",
package/src/validation.js CHANGED
@@ -95,6 +95,7 @@ export function applyValidation(schema, definition) {
95
95
  model: this,
96
96
  appendSchema,
97
97
  allowInclude,
98
+ allowEmpty: true,
98
99
  stripDeleted: true,
99
100
  stripTimestamps: true,
100
101
  allowDefaultTags: true,
@@ -301,6 +302,10 @@ function getSchemaForTypedef(typedef, options = {}) {
301
302
  // Unsetting only allowed for primitive types.
302
303
  if (allowUnset(typedef, options)) {
303
304
  schema = yd.allow(null, '', schema);
305
+ } else if (allowEmpty(typedef, options)) {
306
+ schema = schema.options({
307
+ allowEmpty: true,
308
+ });
304
309
  }
305
310
  }
306
311
 
@@ -459,6 +464,10 @@ function allowUnset(typedef, options) {
459
464
  return options.allowUnset && !typedef.required;
460
465
  }
461
466
 
467
+ function allowEmpty(typedef, options) {
468
+ return options.allowEmpty && typedef.type === 'String';
469
+ }
470
+
462
471
  function isExcludedField(field, options) {
463
472
  if (isSchemaTypedef(field)) {
464
473
  if (options.requireWriteAccess) {
@@ -14,7 +14,7 @@ export const INCLUDE_FIELD_SCHEMA: {
14
14
  meta: {};
15
15
  required(): any;
16
16
  default(arg: any): any;
17
- custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
17
+ custom(fn: Function): any;
18
18
  strip(strip: any): any;
19
19
  allow(...set: any[]): any;
20
20
  reject(...set: any[]): any;
package/types/load.d.ts CHANGED
@@ -4,7 +4,73 @@
4
4
  * @param {string} name
5
5
  * @returns mongoose.Model
6
6
  */
7
- export function loadModel(definition: object, name: string): any;
7
+ export function loadModel(definition: object, name: string): mongoose.Model<any, any, any, any, any, mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, any, any, any, {
8
+ [x: string]: any;
9
+ }, {
10
+ autoIndex?: boolean;
11
+ autoCreate?: boolean;
12
+ bufferCommands?: boolean;
13
+ bufferTimeoutMS?: number;
14
+ capped?: number | boolean | {
15
+ size?: number;
16
+ max?: number;
17
+ autoIndexId?: boolean;
18
+ };
19
+ collation?: mongoose.mongo.CollationOptions;
20
+ collectionOptions?: mongoose.mongo.CreateCollectionOptions;
21
+ timeseries?: mongoose.mongo.TimeSeriesCollectionOptions;
22
+ expireAfterSeconds?: number;
23
+ expires?: string | number;
24
+ collection?: string;
25
+ discriminatorKey?: string;
26
+ excludeIndexes?: boolean;
27
+ id?: boolean;
28
+ _id?: boolean;
29
+ minimize?: boolean;
30
+ optimisticConcurrency?: boolean;
31
+ pluginTags?: string[];
32
+ read?: string;
33
+ writeConcern?: mongoose.mongo.WriteConcern;
34
+ safe?: boolean | {
35
+ w?: string | number;
36
+ wtimeout?: number;
37
+ j?: boolean;
38
+ };
39
+ shardKey?: Record<string, unknown>;
40
+ strict?: boolean | "throw";
41
+ strictQuery?: boolean | "throw";
42
+ toJSON: {
43
+ getters: boolean;
44
+ versionKey: boolean;
45
+ transform: (doc: any, ret: any, options: any) => void;
46
+ } | mongoose.ToObjectOptions<any>;
47
+ toObject: {
48
+ getters: boolean;
49
+ versionKey: boolean;
50
+ transform: (doc: any, ret: any, options: any) => void;
51
+ } | mongoose.ToObjectOptions<any>;
52
+ typeKey?: string;
53
+ validateBeforeSave?: boolean;
54
+ validateModifiedOnly?: boolean;
55
+ versionKey?: string | boolean;
56
+ selectPopulatedPaths?: boolean;
57
+ skipVersioning?: {
58
+ [key: string]: boolean;
59
+ };
60
+ storeSubdocValidationError?: boolean;
61
+ timestamps: boolean | mongoose.SchemaTimestampsConfig;
62
+ suppressReservedKeysWarning?: boolean;
63
+ statics?: {
64
+ [x: string]: any;
65
+ };
66
+ methods?: any;
67
+ query?: any;
68
+ castNonArrays?: boolean;
69
+ virtuals?: mongoose.SchemaOptionsVirtualsPropertyType<any, any, any>;
70
+ overwriteModels?: boolean;
71
+ }, any, any>> & {
72
+ [x: string]: any;
73
+ };
8
74
  /**
9
75
  * Loads all model definitions in the given directory.
10
76
  * Returns the full loaded model set.
@@ -1 +1 @@
1
- {"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../src/load.js"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM,OAahB;AAED;;;;GAIG;AACH,sCAFW,MAAM,mBAkBhB"}
1
+ {"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../src/load.js"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAahB;AAED;;;;GAIG;AACH,sCAFW,MAAM,mBAkBhB"}
package/types/schema.d.ts CHANGED
@@ -6,7 +6,9 @@
6
6
  * @param {mongoose.SchemaOptions} options
7
7
  * @returns mongoose.Schema
8
8
  */
9
- export function createSchema(definition: object, options?: mongoose.SchemaOptions): mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, any, any, any, any, {
9
+ export function createSchema(definition: object, options?: mongoose.SchemaOptions): mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, any, any, any, {
10
+ [x: string]: any;
11
+ }, {
10
12
  autoIndex?: boolean;
11
13
  autoCreate?: boolean;
12
14
  bufferCommands?: boolean;
@@ -60,8 +62,10 @@ export function createSchema(definition: object, options?: mongoose.SchemaOption
60
62
  storeSubdocValidationError?: boolean;
61
63
  timestamps: boolean | mongoose.SchemaTimestampsConfig;
62
64
  suppressReservedKeysWarning?: boolean;
63
- statics?: mongoose.AddThisParameter<any, mongoose.Model<any, {}, {}, {}, any, any>>;
64
- methods?: mongoose.AddThisParameter<any, any> & mongoose.AnyObject;
65
+ statics?: {
66
+ [x: string]: any;
67
+ };
68
+ methods?: any;
65
69
  query?: any;
66
70
  castNonArrays?: boolean;
67
71
  virtuals?: mongoose.SchemaOptionsVirtualsPropertyType<any, any, any>;
@@ -1 +1 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAqBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAwChC;AAED,iEAsBC"}
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAqBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAwChC;AAED,iEAsBC"}
package/types/search.d.ts CHANGED
@@ -11,7 +11,7 @@ export function searchValidation(options?: {}): {
11
11
  meta: {};
12
12
  required(): any;
13
13
  default(arg: any): any;
14
- custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
14
+ custom(fn: Function): any;
15
15
  strip(strip: any): any;
16
16
  allow(...set: any[]): any;
17
17
  reject(...set: any[]): any;
@@ -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;
@@ -72,7 +73,7 @@ export const OBJECT_ID_SCHEMA: {
72
73
  assertions: any[];
73
74
  meta: {};
74
75
  default(arg: any): any;
75
- custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
76
+ custom(fn: Function): any;
76
77
  strip(strip: any): any;
77
78
  allow(...set: any[]): any;
78
79
  reject(...set: any[]): any;
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAkFA,kDAEC;AAED,oEA8FC;AAsBD,wEA2BC;AAmSD;;;EAEC;AAED;;;EAOC;AAjgBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAkFA,kDAEC;AAED,oEA+FC;AAsBD,wEA2BC;AA2SD;;;EAEC;AAED;;;EAOC;AA1gBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}