@bedrockio/model 0.1.2 → 0.1.4
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/dist/cjs/schema.js +6 -1
- package/dist/cjs/validation.js +19 -10
- package/package.json +3 -2
- package/src/schema.js +6 -1
- package/src/validation.js +19 -10
- package/types/schema.d.ts +3 -3
- package/types/validation.d.ts.map +1 -1
package/dist/cjs/schema.js
CHANGED
|
@@ -233,7 +233,12 @@ function applyTupleExtension(typedef) {
|
|
|
233
233
|
type
|
|
234
234
|
} = typedef;
|
|
235
235
|
if (Array.isArray(type) && type.length > 1) {
|
|
236
|
-
|
|
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
|
}
|
package/dist/cjs/validation.js
CHANGED
|
@@ -175,15 +175,22 @@ function getObjectSchema(arg, options) {
|
|
|
175
175
|
return getSchemaForType(arg);
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
|
-
function getArraySchema(
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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,9 @@ function getTupleValidator(types) {
|
|
|
339
346
|
types = types.map(type => {
|
|
340
347
|
return getSchemaForTypedef(type);
|
|
341
348
|
});
|
|
342
|
-
|
|
349
|
+
// Using "loose" here to allow empty arrays through,
|
|
350
|
+
// which mongoose defaults to for array fields.
|
|
351
|
+
return wrapSchemaAsValidator(_yada.default.tuple(types).loose());
|
|
343
352
|
}
|
|
344
353
|
|
|
345
354
|
// 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.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Bedrock utilities for model creation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@bedrockio/logger": "^1.0.5",
|
|
29
|
-
"@bedrockio/yada": "^1.0.16",
|
|
30
29
|
"lodash": "^4.17.21"
|
|
31
30
|
},
|
|
32
31
|
"peerDependencies": {
|
|
32
|
+
"@bedrockio/yada": "^1.0.18",
|
|
33
33
|
"mongoose": "^6.9.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"@babel/core": "^7.20.12",
|
|
38
38
|
"@babel/preset-env": "^7.20.2",
|
|
39
39
|
"@bedrockio/prettier-config": "^1.0.2",
|
|
40
|
+
"@bedrockio/yada": "^1.0.18",
|
|
40
41
|
"@shelf/jest-mongodb": "^4.1.6",
|
|
41
42
|
"babel-plugin-import-replacement": "^1.0.1",
|
|
42
43
|
"eslint": "^8.33.0",
|
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
|
-
|
|
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
|
@@ -185,15 +185,22 @@ function getObjectSchema(arg, options) {
|
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
function getArraySchema(
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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,9 @@ export function getTupleValidator(types) {
|
|
|
378
385
|
types = types.map((type) => {
|
|
379
386
|
return getSchemaForTypedef(type);
|
|
380
387
|
});
|
|
381
|
-
|
|
388
|
+
// Using "loose" here to allow empty arrays through,
|
|
389
|
+
// which mongoose defaults to for array fields.
|
|
390
|
+
return wrapSchemaAsValidator(yd.tuple(types).loose());
|
|
382
391
|
}
|
|
383
392
|
|
|
384
393
|
// Returns an async function that will error on failure.
|
package/types/schema.d.ts
CHANGED
|
@@ -16,8 +16,8 @@ export function createSchema(definition: object, options?: mongoose.SchemaOption
|
|
|
16
16
|
max?: number;
|
|
17
17
|
autoIndexId?: boolean;
|
|
18
18
|
};
|
|
19
|
-
collation?: import("mongodb").CollationOptions;
|
|
20
|
-
timeseries?: import("mongodb").TimeSeriesCollectionOptions;
|
|
19
|
+
collation?: import("mongoose/node_modules/mongodb").CollationOptions;
|
|
20
|
+
timeseries?: import("mongoose/node_modules/mongodb").TimeSeriesCollectionOptions;
|
|
21
21
|
expireAfterSeconds?: number;
|
|
22
22
|
expires?: string | number;
|
|
23
23
|
collection?: string;
|
|
@@ -29,7 +29,7 @@ export function createSchema(definition: object, options?: mongoose.SchemaOption
|
|
|
29
29
|
optimisticConcurrency?: boolean;
|
|
30
30
|
pluginTags?: string[];
|
|
31
31
|
read?: string;
|
|
32
|
-
writeConcern?: import("mongodb").WriteConcern;
|
|
32
|
+
writeConcern?: import("mongoose/node_modules/mongodb").WriteConcern;
|
|
33
33
|
safe?: boolean | {
|
|
34
34
|
w?: string | number;
|
|
35
35
|
wtimeout?: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAiEA,kDAEC;AAED,oEA0DC;AAaD,wEAeC;
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAiEA,kDAEC;AAED,oEA0DC;AAaD,wEAeC;AAgOD;;;EAEC;AAED;;;EAOC;AA/WD,8EAUK"}
|