mongo-type-safe 1.0.4 → 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.
@@ -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
- return schema.partial();
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
  }
@@ -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
+ ];
@@ -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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-type-safe",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {