validno 0.3.2 → 0.3.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.
|
@@ -5,23 +5,22 @@ function validateKeyValue(params) {
|
|
|
5
5
|
const missedCheck = [];
|
|
6
6
|
const typeChecked = [];
|
|
7
7
|
const rulesChecked = [];
|
|
8
|
-
if (hasMissing
|
|
8
|
+
if (hasMissing) {
|
|
9
9
|
return this.handleMissingKeyValidation({ results, key, nestedKey, data, reqs, missedCheck });
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
this.
|
|
11
|
+
const keyValue = data ? data[key] : undefined;
|
|
12
|
+
this.validateType({ results, key, value: keyValue, reqs, nestedKey, typeChecked });
|
|
13
|
+
this.validateRules({ results, nestedKey, value: keyValue, reqs, data, rulesChecked });
|
|
13
14
|
return this.finishValidation({ results, nestedKey, missedCheck, typeChecked, rulesChecked });
|
|
14
15
|
}
|
|
15
16
|
function validateKey(input) {
|
|
16
|
-
let { results, key, nestedKey, data, reqs } = input;
|
|
17
|
+
let { results, key, nestedKey = key, data, reqs } = input;
|
|
17
18
|
if (data === undefined) {
|
|
18
19
|
const noDataResult = new ValidnoResult();
|
|
19
20
|
noDataResult.setNoData(nestedKey);
|
|
20
21
|
}
|
|
21
22
|
if (!results)
|
|
22
23
|
results = new ValidnoResult();
|
|
23
|
-
if (!nestedKey)
|
|
24
|
-
nestedKey = key;
|
|
25
24
|
const hasMissing = _helpers.hasMissing(input);
|
|
26
25
|
if (_helpers.checkNestedIsMissing(reqs, data)) {
|
|
27
26
|
return this.handleMissingNestedKey(nestedKey, results);
|
|
@@ -2,6 +2,7 @@ import { ValidationDetails, ValidationIds } from "../../constants/details.js";
|
|
|
2
2
|
import _validations from "../../utils/validations.js";
|
|
3
3
|
import _errors from "../../utils/errors.js";
|
|
4
4
|
import _validateType from "../../utils/validateType.js";
|
|
5
|
+
import isObjectId from "../../utils/isObjectId.js";
|
|
5
6
|
const validateUnionType = (key, value, requirements, keyName = key) => {
|
|
6
7
|
const typeList = Array.isArray(requirements.type)
|
|
7
8
|
? requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el))
|
|
@@ -116,7 +117,8 @@ const handleTypeValidation = (key, value, requirements, keyName = key) => {
|
|
|
116
117
|
default: {
|
|
117
118
|
const isInstanceOf = typeof typeBySchema === 'function' && value instanceof typeBySchema;
|
|
118
119
|
const isConstructorSame = typeof typeBySchema === 'function' && ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === (typeBySchema === null || typeBySchema === void 0 ? void 0 : typeBySchema.name);
|
|
119
|
-
const
|
|
120
|
+
const isBothObjectId = isObjectId(value, typeBySchema);
|
|
121
|
+
const isOK = (isInstanceOf && isConstructorSame) || (isBothObjectId);
|
|
120
122
|
result.push(_validateType.getResult(keyName, isOK, getDetails(isOK)));
|
|
121
123
|
}
|
|
122
124
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
const isObjectId = (value, expectedType) => {
|
|
2
|
+
const isObjectId = typeof value === 'object' && (value === null || value === void 0 ? void 0 : value.constructor.name) === 'ObjectId';
|
|
3
|
+
const isTypeObjectId = typeof expectedType === 'function' && expectedType.name === 'ObjectId';
|
|
4
|
+
return (isObjectId && isTypeObjectId);
|
|
5
|
+
};
|
|
6
|
+
export default isObjectId;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "validno",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "A lightweight TypeScript validation library for runtime data validation.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"@types/jest": "^29.5.13",
|
|
23
23
|
"@types/node": "^18.14.2",
|
|
24
24
|
"eslint": "^8.14.0",
|
|
25
|
-
"jest": "^29.7.0"
|
|
25
|
+
"jest": "^29.7.0",
|
|
26
|
+
"mongodb": "^6.19.0"
|
|
26
27
|
},
|
|
27
28
|
"directories": {
|
|
28
29
|
"test": "tests"
|