@aeriajs/validation 0.0.173 → 0.0.175

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.
@@ -6,6 +6,7 @@ export type ValidateOptions = {
6
6
  checkObjectIds?: boolean;
7
7
  throwOnError?: boolean;
8
8
  coerce?: boolean;
9
+ coerceObjectIds?: boolean;
9
10
  parentProperty?: Property | Description;
10
11
  descriptions?: Record<string, Description>;
11
12
  objectIdConstructor?: new (arg: unknown) => ObjectId;
package/dist/validate.js CHANGED
@@ -14,6 +14,8 @@ const getPropertyType = (property) => {
14
14
  case 'date':
15
15
  case 'date-time':
16
16
  return 'datetime';
17
+ case 'objectid':
18
+ return 'object';
17
19
  }
18
20
  }
19
21
  return property.type;
@@ -101,6 +103,11 @@ const validateProperty = (what, property, options = {}) => {
101
103
  return types_1.Result.result(new Date(what));
102
104
  }
103
105
  }
106
+ if ((options.coerce || options.coerceObjectIds) && options.objectIdConstructor) {
107
+ if (('$ref' in property || 'format' in property && property.format === 'objectid') && typeof what === 'string') {
108
+ return types_1.Result.result(new options.objectIdConstructor(what));
109
+ }
110
+ }
104
111
  return types_1.Result.error(makePropertyError(types_1.PropertyValidationErrorCode.Unmatching, {
105
112
  expected: expectedType,
106
113
  got: actualType,
package/dist/validate.mjs CHANGED
@@ -12,6 +12,8 @@ const getPropertyType = (property) => {
12
12
  case "date":
13
13
  case "date-time":
14
14
  return "datetime";
15
+ case "objectid":
16
+ return "object";
15
17
  }
16
18
  }
17
19
  return property.type;
@@ -93,6 +95,11 @@ export const validateProperty = (what, property, options = {}) => {
93
95
  return Result.result(new Date(what));
94
96
  }
95
97
  }
98
+ if ((options.coerce || options.coerceObjectIds) && options.objectIdConstructor) {
99
+ if (("$ref" in property || "format" in property && property.format === "objectid") && typeof what === "string") {
100
+ return Result.result(new options.objectIdConstructor(what));
101
+ }
102
+ }
96
103
  return Result.error(makePropertyError(PropertyValidationErrorCode.Unmatching, {
97
104
  expected: expectedType,
98
105
  got: actualType
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/validation",
3
- "version": "0.0.173",
3
+ "version": "0.0.175",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -28,9 +28,9 @@
28
28
  "mongodb": "^6.17.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "@aeriajs/common": "^0.0.154",
32
- "@aeriajs/entrypoint": "^0.0.160",
33
- "@aeriajs/types": "^0.0.132",
31
+ "@aeriajs/common": "^0.0.155",
32
+ "@aeriajs/entrypoint": "^0.0.161",
33
+ "@aeriajs/types": "^0.0.133",
34
34
  "mongodb": "^6.17.0"
35
35
  },
36
36
  "scripts": {