validflow 1.0.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/index.js +1 -0
- package/package.json +13 -0
- package/readme.md +0 -0
- package/src/myZod.js +89 -0
- package/test/zod.test.js +27 -0
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {z} from "./src/myZod.js";
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "validflow",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "myZod.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test/zod.test.js"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "Rohan Nandavdekar",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"description": "Minimal validation library in Javascript"
|
|
13
|
+
}
|
package/readme.md
ADDED
|
File without changes
|
package/src/myZod.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// myZod.js
|
|
2
|
+
|
|
3
|
+
class ZodString {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.minLength = null;
|
|
6
|
+
this.maxLength = null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
min(length) {
|
|
10
|
+
this.minLength = length;
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
max(length) {
|
|
15
|
+
this.maxLength = length;
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
parse(value) {
|
|
20
|
+
if (typeof value !== "string") {
|
|
21
|
+
throw new Error("Expected string");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (this.minLength !== null && value.length < this.minLength) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`String must contain at least ${this.minLength} characters`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (this.maxLength !== null && value.length > this.maxLength) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`String must contain at most ${this.maxLength} characters`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class ZodNumber {
|
|
41
|
+
constructor() {
|
|
42
|
+
this.minValue = null;
|
|
43
|
+
this.maxValue = null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
min(value) {
|
|
47
|
+
this.minValue = value;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
max(value) {
|
|
52
|
+
this.maxValue = value;
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
parse(value) {
|
|
57
|
+
if (typeof value !== "number") {
|
|
58
|
+
throw new Error("Expected number");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (this.minValue !== null && value < this.minValue) {
|
|
62
|
+
throw new Error(`Number must be >= ${this.minValue}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (this.maxValue !== null && value > this.maxValue) {
|
|
66
|
+
throw new Error(`Number must be <= ${this.maxValue}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
class ZodBoolean {
|
|
74
|
+
parse(value) {
|
|
75
|
+
if (typeof value !== "boolean") {
|
|
76
|
+
throw new Error("Expected boolean");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const z = {
|
|
84
|
+
string: () => new ZodString(),
|
|
85
|
+
number: () => new ZodNumber(),
|
|
86
|
+
boolean: () => new ZodBoolean(),
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
module.exports = z;
|
package/test/zod.test.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const z = require("../src/myZod");
|
|
2
|
+
|
|
3
|
+
try {
|
|
4
|
+
// String validation
|
|
5
|
+
const usernameSchema = z.string().min(3).max(10);
|
|
6
|
+
|
|
7
|
+
const username = usernameSchema.parse("Rohan");
|
|
8
|
+
|
|
9
|
+
console.log("Valid username:", username);
|
|
10
|
+
|
|
11
|
+
// Number validation
|
|
12
|
+
const ageSchema = z.number().min(18).max(60);
|
|
13
|
+
|
|
14
|
+
const age = ageSchema.parse(25);
|
|
15
|
+
|
|
16
|
+
console.log("Valid age:", age);
|
|
17
|
+
|
|
18
|
+
// Boolean validation
|
|
19
|
+
const adminSchema = z.boolean();
|
|
20
|
+
|
|
21
|
+
const isAdmin = adminSchema.parse(true);
|
|
22
|
+
|
|
23
|
+
console.log("Valid boolean:", isAdmin);
|
|
24
|
+
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.log("Validation Error:", error.message);
|
|
27
|
+
}
|