mongo-type-safe 1.0.3 → 1.0.5
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/README.md +0 -1
- package/dist/util/helper.js +35 -1
- package/dist/util/types.js +17 -0
- package/dist/util/validate.js +19 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -171,7 +171,6 @@ See the source for full method signatures and JSDoc documentation.
|
|
|
171
171
|
|
|
172
172
|
- All filters, updates, and documents are checked at compile time against your Zod schema.
|
|
173
173
|
- Invalid field names or types will cause TypeScript errors before you run your code.
|
|
174
|
-
- No runtime validation is performed—this is pure TypeScript safety.
|
|
175
174
|
|
|
176
175
|
---
|
|
177
176
|
|
package/dist/util/helper.js
CHANGED
|
@@ -1,12 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.formatZodErrors = formatZodErrors;
|
|
4
7
|
exports.createFilterSchema = createFilterSchema;
|
|
8
|
+
exports.isAllowedOperator = isAllowedOperator;
|
|
9
|
+
const zod_1 = __importDefault(require("zod"));
|
|
10
|
+
const types_1 = require("./types");
|
|
5
11
|
function formatZodErrors(issues) {
|
|
6
12
|
return issues
|
|
7
13
|
.map(issue => `Error at field - ${issue.path.join(".")}: ${issue.message}`)
|
|
8
14
|
.join("\n");
|
|
9
15
|
}
|
|
16
|
+
const comparisonOperators = (type) => zod_1.default
|
|
17
|
+
.object({
|
|
18
|
+
$eq: type.optional(),
|
|
19
|
+
$ne: type.optional(),
|
|
20
|
+
$gt: type.optional(),
|
|
21
|
+
$gte: type.optional(),
|
|
22
|
+
$lt: type.optional(),
|
|
23
|
+
$lte: type.optional(),
|
|
24
|
+
$in: zod_1.default.array(type).optional(),
|
|
25
|
+
$nin: zod_1.default.array(type).optional(),
|
|
26
|
+
$regex: zod_1.default.instanceof(RegExp).optional()
|
|
27
|
+
})
|
|
28
|
+
.optional();
|
|
29
|
+
function wrapWithOperators(type) {
|
|
30
|
+
return zod_1.default.union([type, comparisonOperators(type)]);
|
|
31
|
+
}
|
|
10
32
|
function createFilterSchema(schema) {
|
|
11
|
-
|
|
33
|
+
const shape = schema.shape;
|
|
34
|
+
const newShape = {};
|
|
35
|
+
for (const key in shape) {
|
|
36
|
+
newShape[key] = wrapWithOperators(shape[key]);
|
|
37
|
+
}
|
|
38
|
+
// lazy mora biti unutar rekursivnih poziva
|
|
39
|
+
const filterSchema = zod_1.default.lazy(() => zod_1.default
|
|
40
|
+
.object(Object.assign(Object.assign({}, newShape), { $or: zod_1.default.array(zod_1.default.lazy(() => filterSchema)).optional(), $and: zod_1.default.array(zod_1.default.lazy(() => filterSchema)).optional(), $not: zod_1.default.lazy(() => filterSchema).optional() }))
|
|
41
|
+
.strict());
|
|
42
|
+
return filterSchema;
|
|
43
|
+
}
|
|
44
|
+
function isAllowedOperator(key) {
|
|
45
|
+
return types_1.allowedOperators.includes(key);
|
|
12
46
|
}
|
package/dist/util/types.js
CHANGED
|
@@ -1,2 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.allowedOperators = void 0;
|
|
4
|
+
exports.allowedOperators = [
|
|
5
|
+
"$gt",
|
|
6
|
+
"$gte",
|
|
7
|
+
"$lt",
|
|
8
|
+
"$lte",
|
|
9
|
+
"$in",
|
|
10
|
+
"$nin",
|
|
11
|
+
"$ne",
|
|
12
|
+
"$eq",
|
|
13
|
+
"$exists",
|
|
14
|
+
"$regex",
|
|
15
|
+
"$and",
|
|
16
|
+
"$or",
|
|
17
|
+
"$not",
|
|
18
|
+
"$nor"
|
|
19
|
+
];
|
package/dist/util/validate.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.validateOrThrow = validateOrThrow;
|
|
4
|
+
exports.validateFilterOperators = validateFilterOperators;
|
|
4
5
|
exports.validateFilter = validateFilter;
|
|
5
6
|
exports.validateUpdate = validateUpdate;
|
|
6
7
|
const zod_1 = require("zod");
|
|
@@ -17,7 +18,25 @@ function validateOrThrow(schema, data) {
|
|
|
17
18
|
throw err;
|
|
18
19
|
}
|
|
19
20
|
}
|
|
21
|
+
function validateFilterOperators(filter) {
|
|
22
|
+
if (!filter || typeof filter !== "object")
|
|
23
|
+
return;
|
|
24
|
+
for (const key in filter) {
|
|
25
|
+
if (key.startsWith("$")) {
|
|
26
|
+
if (!(0, helper_1.isAllowedOperator)(key)) {
|
|
27
|
+
throw new Error(`Invalid filter operator: ${key}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const value = filter[key];
|
|
31
|
+
if (typeof value === "object" && value !== null) {
|
|
32
|
+
return validateFilterOperators(value);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
20
36
|
function validateFilter(filter, schema) {
|
|
37
|
+
if (!filter || typeof filter !== "object")
|
|
38
|
+
return;
|
|
39
|
+
validateFilterOperators(filter);
|
|
21
40
|
const filterSchema = (0, helper_1.createFilterSchema)(schema);
|
|
22
41
|
const parsed = filterSchema.safeParse(filter);
|
|
23
42
|
if (!parsed.success) {
|