mongo-type-safe 1.0.1 → 1.0.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.
package/README.md CHANGED
@@ -8,7 +8,7 @@ A type-safe MongoDB wrapper for Node.js using Zod schemas and TypeScript. Get co
8
8
 
9
9
  - **Type-safe CRUD**: Compile-time type checking for all MongoDB operations
10
10
  - **Zod integration**: Use your Zod schemas as the single source of truth
11
- - **No runtime validation**: Pure TypeScript safety, no extra checks in production
11
+ - **With runtime validation**: Enforces schema correctness at both compile-time and runtime using Zod.
12
12
  - **Familiar API**: Mirrors the official MongoDB Node.js driver
13
13
 
14
14
  ---
package/dist/index.js CHANGED
@@ -23,6 +23,7 @@ function createSafeCollection(collection, schema) {
23
23
  */
24
24
  updateOne(filter, update, options) {
25
25
  (0, validate_1.validateFilter)(filter, schema);
26
+ (0, validate_1.validateUpdate)(update, schema);
26
27
  return collection.updateOne(filter, update, options);
27
28
  },
28
29
  /**
@@ -30,6 +31,7 @@ function createSafeCollection(collection, schema) {
30
31
  */
31
32
  findOneAndUpdate(filter, update, options) {
32
33
  (0, validate_1.validateFilter)(filter, schema);
34
+ (0, validate_1.validateUpdate)(update, schema);
33
35
  return options
34
36
  ? collection.findOneAndUpdate(filter, update, options)
35
37
  : collection.findOneAndUpdate(filter, update);
@@ -39,6 +41,7 @@ function createSafeCollection(collection, schema) {
39
41
  */
40
42
  updateMany(filter, update, options) {
41
43
  (0, validate_1.validateFilter)(filter, schema);
44
+ (0, validate_1.validateUpdate)(update, schema);
42
45
  return collection.updateMany(filter, update, options);
43
46
  },
44
47
  /**
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validateOrThrow = validateOrThrow;
4
4
  exports.validateFilter = validateFilter;
5
+ exports.validateUpdate = validateUpdate;
5
6
  const zod_1 = require("zod");
6
7
  const helper_1 = require("./helper");
7
8
  function validateOrThrow(schema, data) {
@@ -24,3 +25,47 @@ function validateFilter(filter, schema) {
24
25
  }
25
26
  return filter;
26
27
  }
28
+ function validateUpdate(update, schema) {
29
+ if (!update || typeof update !== "object")
30
+ return;
31
+ const shape = schema.shape;
32
+ if (update.$set) {
33
+ const result = schema.partial().safeParse(update.$set);
34
+ if (!result.success) {
35
+ throw new Error(`Invalid $set update: ${result.error.message}`);
36
+ }
37
+ }
38
+ if (update.$setOnInsert) {
39
+ const result = schema.partial().safeParse(update.$setOnInsert);
40
+ if (!result.success) {
41
+ throw new Error(`Invalid $setOnInsert update: ${result.error.message}`);
42
+ }
43
+ }
44
+ if (update.$unset) {
45
+ const keys = Object.keys(update.$unset);
46
+ const allowed = Object.keys(shape);
47
+ for (const key of keys) {
48
+ if (!allowed.includes(key)) {
49
+ throw new Error(`Invalid $unset key: ${key}`);
50
+ }
51
+ }
52
+ }
53
+ for (const op of ["$inc", "$min", "$max"]) {
54
+ if (update[op]) {
55
+ for (const key in update[op]) {
56
+ const field = shape[key];
57
+ if (!field) {
58
+ throw new Error(`Invalid $inc key: ${key}, field not found in schema`);
59
+ }
60
+ let base = field;
61
+ while (base._def.typeName === "ZodOptional" ||
62
+ base._def.typeName === "ZodNullable") {
63
+ base = base._def.innerType;
64
+ }
65
+ if (base._def.typeName !== "ZodNumber") {
66
+ throw new Error(`Invalid $inc key: ${key}, not a number field`);
67
+ }
68
+ }
69
+ }
70
+ }
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-type-safe",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {