@bedrockio/model 0.1.1 → 0.1.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.
@@ -233,7 +233,12 @@ function applyTupleExtension(typedef) {
233
233
  type
234
234
  } = typedef;
235
235
  if (Array.isArray(type) && type.length > 1) {
236
- typedef.type = ['Mixed'];
236
+ // Note that mongoose appears to have a bug where passing
237
+ // "Array" as a string will become a double nested array.
238
+ // Compare to Array (native function) which will not result
239
+ // in this nesting. Using an empty array instead to signal
240
+ // mixed types. https://mongoosejs.com/docs/schematypes.html#arrays
241
+ typedef.type = [];
237
242
  typedef.validate = (0, _validation.getTupleValidator)(type);
238
243
  }
239
244
  }
@@ -117,7 +117,7 @@ function applyValidation(schema, definition) {
117
117
 
118
118
  // Yada schemas
119
119
 
120
- function getSchemaFromMongoose(schema, options) {
120
+ function getSchemaFromMongoose(schema, options = {}) {
121
121
  let {
122
122
  obj
123
123
  } = schema;
@@ -175,15 +175,22 @@ function getObjectSchema(arg, options) {
175
175
  return getSchemaForType(arg);
176
176
  }
177
177
  }
178
- function getArraySchema(obj, options) {
179
- // Nested array fields may not skip required
180
- // validations as they are a new context.
181
- let schema = getObjectSchema(obj[0], {
182
- ...options,
183
- skipRequired: false
184
- });
185
- if (!options.unwindArrayFields) {
186
- schema = _yada.default.array(schema);
178
+ function getArraySchema(arr, options) {
179
+ let schema;
180
+ if (arr.length === 0) {
181
+ schema = _yada.default.array();
182
+ } else if (arr.length === 1) {
183
+ // Nested array fields may not skip required
184
+ // validations as they are a new context.
185
+ schema = getObjectSchema(arr[0], {
186
+ ...options,
187
+ skipRequired: false
188
+ });
189
+ if (!options.unwindArrayFields) {
190
+ schema = _yada.default.array(schema);
191
+ }
192
+ } else {
193
+ throw new Error('Array schema may not have more than 1 element.');
187
194
  }
188
195
  return schema;
189
196
  }
@@ -339,7 +346,7 @@ function getTupleValidator(types) {
339
346
  types = types.map(type => {
340
347
  return getSchemaForTypedef(type);
341
348
  });
342
- return wrapSchemaAsValidator(_yada.default.array(types).length(types.length));
349
+ return wrapSchemaAsValidator(_yada.default.tuple(types));
343
350
  }
344
351
 
345
352
  // Returns an async function that will error on failure.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@bedrockio/logger": "^1.0.5",
29
- "@bedrockio/yada": "^1.0.16",
29
+ "@bedrockio/yada": "^1.0.17",
30
30
  "lodash": "^4.17.21"
31
31
  },
32
32
  "peerDependencies": {
package/src/schema.js CHANGED
@@ -243,7 +243,12 @@ function applyScopeExtension(scope, definition) {
243
243
  function applyTupleExtension(typedef) {
244
244
  const { type } = typedef;
245
245
  if (Array.isArray(type) && type.length > 1) {
246
- typedef.type = ['Mixed'];
246
+ // Note that mongoose appears to have a bug where passing
247
+ // "Array" as a string will become a double nested array.
248
+ // Compare to Array (native function) which will not result
249
+ // in this nesting. Using an empty array instead to signal
250
+ // mixed types. https://mongoosejs.com/docs/schematypes.html#arrays
251
+ typedef.type = [];
247
252
  typedef.validate = getTupleValidator(type);
248
253
  }
249
254
  }
package/src/validation.js CHANGED
@@ -129,7 +129,7 @@ export function applyValidation(schema, definition) {
129
129
 
130
130
  // Yada schemas
131
131
 
132
- function getSchemaFromMongoose(schema, options) {
132
+ function getSchemaFromMongoose(schema, options = {}) {
133
133
  let { obj } = schema;
134
134
  if (options.stripReserved) {
135
135
  obj = omit(obj, RESERVED_FIELDS);
@@ -185,15 +185,22 @@ function getObjectSchema(arg, options) {
185
185
  }
186
186
  }
187
187
 
188
- function getArraySchema(obj, options) {
189
- // Nested array fields may not skip required
190
- // validations as they are a new context.
191
- let schema = getObjectSchema(obj[0], {
192
- ...options,
193
- skipRequired: false,
194
- });
195
- if (!options.unwindArrayFields) {
196
- schema = yd.array(schema);
188
+ function getArraySchema(arr, options) {
189
+ let schema;
190
+ if (arr.length === 0) {
191
+ schema = yd.array();
192
+ } else if (arr.length === 1) {
193
+ // Nested array fields may not skip required
194
+ // validations as they are a new context.
195
+ schema = getObjectSchema(arr[0], {
196
+ ...options,
197
+ skipRequired: false,
198
+ });
199
+ if (!options.unwindArrayFields) {
200
+ schema = yd.array(schema);
201
+ }
202
+ } else {
203
+ throw new Error('Array schema may not have more than 1 element.');
197
204
  }
198
205
  return schema;
199
206
  }
@@ -378,7 +385,7 @@ export function getTupleValidator(types) {
378
385
  types = types.map((type) => {
379
386
  return getSchemaForTypedef(type);
380
387
  });
381
- return wrapSchemaAsValidator(yd.array(types).length(types.length));
388
+ return wrapSchemaAsValidator(yd.tuple(types));
382
389
  }
383
390
 
384
391
  // Returns an async function that will error on failure.
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAiEA,kDAEC;AAED,oEA0DC;AAaD,wEAeC;AAyND;;;EAEC;AAED;;;EAKC;AAtWD,8EAUK"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAiEA,kDAEC;AAED,oEA0DC;AAaD,wEAeC;AAgOD;;;EAEC;AAED;;;EAKC;AA7WD,8EAUK"}