validation-box
Version:
The only validation library - with flexible regex - you need.
85 lines (84 loc) • 2.31 kB
JavaScript
import {
validator,
vboxSchema
} from "../chunk-UNIGDQBQ.mjs";
import "../chunk-TAG6UVFN.mjs";
// src/manual/schema.ts
var userSchema = new vboxSchema(
{
username: validator.username({
required: true,
min: 5,
messages: {
required: "Username is required",
min: "Username must be at least 5 characters"
}
}),
email: validator.email({
required: true,
allowedDomains: ["gmail.com"],
messages: {
required: "Email is required",
domain: "Only Gmail addresses are allowed"
}
})
}
// {
// validateAll: true,
// showErrors: true
// }
);
var testData = [
{
input: {
username: "admin",
email: "user@gmail.com",
password: "Secure@12345678901234"
}
// ❌ Username "admin" is banned
},
{
input: {
username: "test_123",
email: "example@outlook.com",
password: "Strong!P@ss4567890"
}
// ✅ All valid
},
{
input: {
username: "valid_user",
email: "test@hotmail.com",
password: "Valid@123"
}
// ❌ Invalid email domain
},
{
input: {
username: "ab",
email: "test@gmail.com",
password: "short"
}
// ❌ Username too short, weak password
}
];
console.log("\n\u{1F4CC} Running Schema Tests...\n");
var passedTests = 0;
var totalTests = testData.length;
testData.forEach(({ input }, index) => {
const result = userSchema.validate(input);
console.log(`\u{1F539} Test ${index + 1}:`);
console.log("\u{1F538} Input:", JSON.stringify(input, null, 2));
console.log("\u{1F538} Result:", JSON.stringify(result, null, 2));
const expectedSuccess = Object.values(result.errors || {}).length === 0;
const expected = expectedSuccess ? { success: true, data: input } : { success: false, errors: result.errors };
console.log("\u{1F538} Expected:", JSON.stringify(expected, null, 2));
const testPassed = JSON.stringify(result) === JSON.stringify(expected);
if (testPassed) passedTests++;
console.log(testPassed ? "\u2705 Test Passed!\n" : "\u274C Test Failed!\n");
});
console.log("\u{1F4CA} Test Summary");
console.log(`\u2705 Passed: ${passedTests}/${totalTests}`);
console.log(`\u274C Failed: ${totalTests - passedTests}/${totalTests}
`);
//# sourceMappingURL=schema.mjs.map