-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.ts
More file actions
70 lines (64 loc) · 2.43 KB
/
test.ts
File metadata and controls
70 lines (64 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { superform } from "./packages/core/src/index.js"
// Helper for consistent result output
async function runValidationTest(name: string, result: any) {
if (result instanceof Promise) result = await result
const isValid = result === null || (typeof result === "object" && result.success === true)
if (isValid) {
console.log(`✅ ${name} VALID`)
} else {
const error = typeof result === "object" ? result.errors : result
console.log(`❌ ${name} INVALID → ${JSON.stringify(error)}`)
console.log(`❌ ${name} INVALID → ${JSON.stringify(error)}`)
console.log(`❌ ${name} INVALID → ${JSON.stringify(error)}`)
console.log(`❌ ${name} INVALID → ${JSON.stringify(error)}`)
}
}
async function runComplexTest() {
console.log("===== COMPLEX NESTED FORM TEST =====")
// Complex schema: User Profile with deeply nested structure, arrays, enums, optionals, and async refinements
const profileSchema = superform.object({
personal: superform.object({
firstName: superform.string().min(2),
lastName: superform.string().min(2),
email: superform.string().email(),
birthYear: superform.number().min(1920).max(new Date().getFullYear())
}),
account: superform.object({
username: superform.string().min(5).async(async (v) => {
const reserved = ["root", "admin", "system"];
if (reserved.includes(v)) return "Reserved username";
return null;
}),
password: superform.string().min(8),
}),
preferences: superform.object({
languages: superform.array().min(1).max(3),
notifications: superform.object({
email: superform.boolean(),
sms: superform.boolean(),
push: superform.boolean()
})
}),
});
const result = await profileSchema.validate({
personal: {
firstName: "John",
lastName: "Doe",
email: "john.doe@example.com",
birthYear: 1990
},
account: {
username: "johndoe",
password: "password123"
},
preferences: {
languages: ["en", "fr"],
notifications: {
email: true,
sms: false
}
}
});
await runValidationTest("Complex Test", result);
}
runComplexTest();