validno 0.3.3 → 0.3.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.
package/README.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
A lightweight and flexible TypeScript validation library for Node.js applications.
|
|
4
4
|
|
|
5
|
+
## Documentation
|
|
6
|
+
|
|
7
|
+
Check out docs at [https://validno.kodzero.pro](https://validno.kodzero.pro)
|
|
8
|
+
|
|
5
9
|
## Installation
|
|
6
10
|
|
|
7
11
|
```bash
|
|
@@ -514,4 +518,5 @@ const newUser = {
|
|
|
514
518
|
const result = userSchema.validate<User>(newUser, ['name', 'email'])
|
|
515
519
|
|
|
516
520
|
// ...or use automatic type inference based on the object
|
|
517
|
-
const result = userSchema.validate(newUser, ['name', 'email'])
|
|
521
|
+
const result = userSchema.validate(newUser, ['name', 'email'])
|
|
522
|
+
```
|
package/dist/dev.js
CHANGED
|
@@ -1,12 +1,57 @@
|
|
|
1
1
|
import Schema from './index.js';
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
const configSchema = new Schema({
|
|
3
|
+
api: {
|
|
4
|
+
port: {
|
|
5
|
+
type: Number,
|
|
6
|
+
required: true,
|
|
7
|
+
rules: { min: 1000, max: 9999 }
|
|
8
|
+
},
|
|
9
|
+
cors: {
|
|
10
|
+
enabled: { type: Boolean, required: true },
|
|
11
|
+
origins: {
|
|
12
|
+
type: Array,
|
|
13
|
+
eachType: String,
|
|
14
|
+
required: false
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
rateLimit: {
|
|
18
|
+
enabled: { type: Boolean, required: true },
|
|
19
|
+
maxRequests: {
|
|
20
|
+
type: Number,
|
|
21
|
+
required: false,
|
|
22
|
+
rules: { min: 1, max: 10000 }
|
|
23
|
+
},
|
|
24
|
+
windowMs: {
|
|
25
|
+
type: Number,
|
|
26
|
+
required: false,
|
|
27
|
+
rules: { min: 1000, max: 86400000 }
|
|
28
|
+
}
|
|
29
|
+
}
|
|
7
30
|
}
|
|
8
31
|
});
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
32
|
+
const exampleConfigFile = {
|
|
33
|
+
"app": {
|
|
34
|
+
"name": "MyApp",
|
|
35
|
+
"version": "1.0.0",
|
|
36
|
+
"environment": "development"
|
|
37
|
+
},
|
|
38
|
+
"database": {
|
|
39
|
+
"host": "localhost",
|
|
40
|
+
"port": 5432,
|
|
41
|
+
"name": "myappdb",
|
|
42
|
+
"ssl": false
|
|
43
|
+
},
|
|
44
|
+
"api": {
|
|
45
|
+
"port": 3000,
|
|
46
|
+
"cors": {
|
|
47
|
+
"enabled": true,
|
|
48
|
+
"origins": ["http://localhost:3000"]
|
|
49
|
+
},
|
|
50
|
+
"rateLimit": {
|
|
51
|
+
"enabled": true,
|
|
52
|
+
"maxRequests": 1000,
|
|
53
|
+
"windowMs": 60000
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
const res = configSchema.validate(exampleConfigFile);
|
|
@@ -19,12 +19,10 @@ class ValidnoResult {
|
|
|
19
19
|
fixParentByChilds(parentKey, childChecks = []) {
|
|
20
20
|
const isEveryOk = childChecks.every(check => check === true);
|
|
21
21
|
this.setKeyStatus(parentKey, isEveryOk);
|
|
22
|
-
if (isEveryOk)
|
|
22
|
+
if (isEveryOk)
|
|
23
23
|
this.setPassed(parentKey);
|
|
24
|
-
|
|
25
|
-
else {
|
|
24
|
+
else
|
|
26
25
|
this.setFailed(parentKey);
|
|
27
|
-
}
|
|
28
26
|
}
|
|
29
27
|
setMissing(key, errMsg) {
|
|
30
28
|
const error = errMsg || _errors.getMissingError(key);
|
|
@@ -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,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "validno",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"description": "A lightweight TypeScript validation library for runtime data validation.",
|
|
3
|
+
"version": "0.3.5",
|
|
4
|
+
"description": "A lightweight TypeScript validation library for runtime data validation. Used as part of Kodzero API framework.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"tsc": "tsc -b src/",
|
|
@@ -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"
|