valireq 0.0.0-stage → 0.2.0
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/CHANGELOG.md +39 -0
- package/LICENSE +9 -0
- package/README.md +409 -2
- package/examples/basic.js +81 -0
- package/examples/custom-validation.js +136 -0
- package/examples/nested.js +125 -0
- package/package.json +32 -4
- package/src/index.js +618 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ValiReq — Nested Object and Array Example
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates:
|
|
5
|
+
* - Nested object schemas via `properties`
|
|
6
|
+
* - Array item validation via `items`
|
|
7
|
+
* - minItems / maxItems constraints
|
|
8
|
+
* - Precise error paths (address.pincode, hobbies[1])
|
|
9
|
+
* - Unknown-field stripping inside nested objects
|
|
10
|
+
*
|
|
11
|
+
* Run:
|
|
12
|
+
* node examples/nested.js
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const { validateData } = require('../src');
|
|
16
|
+
|
|
17
|
+
async function main() {
|
|
18
|
+
// ── Successful validation ──
|
|
19
|
+
const result = await validateData(
|
|
20
|
+
{
|
|
21
|
+
name: ' Prakhar ',
|
|
22
|
+
address: {
|
|
23
|
+
city: 'Delhi',
|
|
24
|
+
pincode: '110001',
|
|
25
|
+
extra: 'this will be stripped',
|
|
26
|
+
},
|
|
27
|
+
hobbies: ['coding', 'reading'],
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: { required: true, type: 'string', trim: true },
|
|
31
|
+
|
|
32
|
+
address: {
|
|
33
|
+
type: 'object',
|
|
34
|
+
properties: {
|
|
35
|
+
city: { required: true, type: 'string' },
|
|
36
|
+
pincode: { required: true, type: 'string', exactLength: 6 },
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
hobbies: {
|
|
41
|
+
type: 'array',
|
|
42
|
+
items: { type: 'string' },
|
|
43
|
+
minItems: 1,
|
|
44
|
+
maxItems: 10,
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
console.log('Validated output:');
|
|
50
|
+
console.log(JSON.stringify(result, null, 2));
|
|
51
|
+
// {
|
|
52
|
+
// "name": "Prakhar",
|
|
53
|
+
// "address": { "city": "Delhi", "pincode": "110001" },
|
|
54
|
+
// "hobbies": ["coding", "reading"]
|
|
55
|
+
// }
|
|
56
|
+
// Note: address.extra was stripped.
|
|
57
|
+
|
|
58
|
+
// ── Validation failure with nested error paths ──
|
|
59
|
+
console.log('\n--- Validation errors demo ---');
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
await validateData(
|
|
63
|
+
{
|
|
64
|
+
name: 'Ada',
|
|
65
|
+
address: { pincode: '12' },
|
|
66
|
+
hobbies: ['ok', 42],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: { required: true, type: 'string' },
|
|
70
|
+
address: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
city: { required: true, type: 'string' },
|
|
74
|
+
pincode: { type: 'string', exactLength: 6 },
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
hobbies: { type: 'array', items: { type: 'string' } },
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.log('Errors:');
|
|
82
|
+
for (const err of error.errors) {
|
|
83
|
+
console.log(` ${err.field}: [${err.rule}] ${err.message}`);
|
|
84
|
+
}
|
|
85
|
+
// address.city: [required] This field is required.
|
|
86
|
+
// address.pincode: [exactLength] Must be exactly 6 characters.
|
|
87
|
+
// hobbies[1]: [type] Expected type string.
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ── Deeply nested: array of objects ──
|
|
91
|
+
console.log('\n--- Deeply nested schema ---');
|
|
92
|
+
|
|
93
|
+
const deepResult = await validateData(
|
|
94
|
+
{
|
|
95
|
+
users: [
|
|
96
|
+
{ name: 'Ada', addresses: [{ city: 'London' }] },
|
|
97
|
+
{ name: 'Bob', addresses: [{ city: 'NYC' }, { city: 'LA' }] },
|
|
98
|
+
],
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
users: {
|
|
102
|
+
type: 'array',
|
|
103
|
+
items: {
|
|
104
|
+
type: 'object',
|
|
105
|
+
properties: {
|
|
106
|
+
name: { required: true, type: 'string' },
|
|
107
|
+
addresses: {
|
|
108
|
+
type: 'array',
|
|
109
|
+
items: {
|
|
110
|
+
type: 'object',
|
|
111
|
+
properties: {
|
|
112
|
+
city: { required: true, type: 'string' },
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
console.log(JSON.stringify(deepResult, null, 2));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
main().catch(console.error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valireq",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
}
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "A lightweight request validation, sanitization and transformation toolkit for Express.js.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"exports": { ".": "./src/index.js" },
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"engines": { "node": ">=18" },
|
|
9
|
+
"files": ["src", "README.md", "LICENSE", "CHANGELOG.md", "examples"],
|
|
10
|
+
"scripts": { "test": "node --test", "lint": "node --check src/index.js" },
|
|
11
|
+
"keywords": [
|
|
12
|
+
"express",
|
|
13
|
+
"validation",
|
|
14
|
+
"sanitization",
|
|
15
|
+
"middleware",
|
|
16
|
+
"schema",
|
|
17
|
+
"security",
|
|
18
|
+
"request",
|
|
19
|
+
"transform",
|
|
20
|
+
"coerce",
|
|
21
|
+
"cleanup"
|
|
22
|
+
],
|
|
23
|
+
"author": "Prakhar Jaiswal (https://github.com/Prakhar-Jaiswal17)",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/Prakhar-Jaiswal17/ValiReq.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/Prakhar-Jaiswal17/ValiReq/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/Prakhar-Jaiswal17/ValiReq#readme",
|
|
33
|
+
"publishConfig": { "access": "public" }
|
|
34
|
+
}
|