fortify-schema
Version:
A modern TypeScript validation library designed around familiar interface syntax and powerful conditional validation. Experience schema validation that feels natural to TypeScript developers while unlocking advanced runtime validation capabilities.
73 lines (59 loc) โข 2.63 kB
text/typescript
import { Interface } from "../../schema/mode/interfaces/Interface";
// Test different regex complexities
const SimpleRegexSchema = Interface({
// Simple pattern (works)
simplePattern: "string(/^[a-z]+$/)",
// Medium complexity
mediumPattern: "string(/^[A-Za-z0-9]+$/)",
// With special chars
specialPattern: "string(/^[a-z@]+$/)",
// Strong password pattern (fails)
strongPassword:
"string(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$/)",
// IPv4 address
ipv4: "ip",
});
// Test the constraint parser directly
import { ConstraintParser } from "../../schema/mode/interfaces/validators/ConstraintParser";
console.log("๐ Debug: Testing constraint parsing");
// Test simple regex
const parsed1 = ConstraintParser.parseConstraints("string(/^[a-z]+$/)");
console.log("Simple regex:", JSON.stringify(parsed1, null, 2));
// Test complex regex
const complexRegex =
"string(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$/)";
const parsed2 = ConstraintParser.parseConstraints(complexRegex);
console.log("Complex regex:", JSON.stringify(parsed2, null, 2));
// Test if the issue is with nested parentheses
const testRegex = "string(/^(?=test).*$/)";
const parsed3 = ConstraintParser.parseConstraints(testRegex);
console.log("Nested parentheses test:", JSON.stringify(parsed3, null, 2));
const AdvancedPatternSchema = SimpleRegexSchema;
// Test 1: Valid patterns
console.log("\n๐งช Test 1: Valid patterns");
const result1 = AdvancedPatternSchema.safeParse({
simplePattern: "hello", // Should pass /^[a-z]+$/
mediumPattern: "Hello123", // Should pass /^[A-Za-z0-9]+$/
specialPattern: "hello@", // Should pass /^[a-z@]+$/
strongPassword: "MyP@ssw0rd123", // Should pass complex regex
ipv4: "192.168.1.1", // Valid IPv4
});
if (result1.success) {
console.log("โ
Expected success:", result1.data);
} else {
console.log("โ Unexpected errors:", result1.errors);
}
// Test 2: Invalid patterns
console.log("\n๐งช Test 2: Invalid patterns");
const result2 = AdvancedPatternSchema.safeParse({
simplePattern: "Hello123", // Should fail /^[a-z]+$/ (has uppercase and numbers)
mediumPattern: "hello@", // Should fail /^[A-Za-z0-9]+$/ (has special char)
specialPattern: "Hello", // Should fail /^[a-z@]+$/ (has uppercase)
strongPassword: "weak", // Should fail complex regex
ipv4: "192.168.1.1", // Valid IPv4
});
if (result2.success) {
console.log("โ
Unexpected success:", result2.data);
} else {
console.log("โ Expected errors:", result2.errors);
}