mongo-type-safe 1.0.2 → 1.0.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/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/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.2",
3
+ "version": "1.0.4",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {