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.
174 lines (136 loc) โข 6.06 kB
text/typescript
/**
* VSCode Extension Test File
* This file tests all the extension issues mentioned in extensionBugs.md
*/
import { Interface } from "../schema/mode/interfaces/Interface";
// ====================================================================
// TEST 1: Quote Type Support
// ====================================================================
const QuoteTypesSchema = Interface({
// Double quotes (should work)
doubleQuote: "string",
// Single quotes (should work after fix)
singleQuote: 'string',
// Backticks (should work after fix)
backtick: `string`,
// Mixed conditional with different quotes
mixedQuotes: 'when data.$exists() *? "string" : =default',
});
// ====================================================================
// TEST 2: @fortify-ignore Support
// ====================================================================
const IgnoreTestSchema = Interface({
// Single line ignore (should work)
// @fortify-ignore
singleLineIgnore: "invalidtype",
// Inline ignore (should work)
inlineIgnore: "anotherbadtype", // @fortify-ignore
/* @fortify-ignore
* Multi-line ignore block
* This should ignore all validation errors
*/
multiLineIgnore1: "badtype1",
multiLineIgnore2: "badtype2",
multiLineIgnore3: "badtype3",
/* End of ignore block */
// This should show errors again
shouldShowError: "invalidtype",
});
// ====================================================================
// TEST 3: Special Character Property Access
// ====================================================================
const SpecialCharsSchema = Interface({
config: "any?",
// Properties with hyphens (should work after fix)
hasAdminOverride: 'when config["admin-override"].$exists() *? boolean : =false',
// Properties with spaces (should work after fix)
hasSpecialConfig: 'when config["special config"].$exists() *? boolean : =false',
// Properties with dots (should work after fix)
hasDottedProp: 'when config["app.version"].$exists() *? boolean : =false',
});
// ====================================================================
// TEST 4: Array Property Access
// ====================================================================
const ArrayAccessSchema = Interface({
data: "any?",
// Array index access (should work after fix)
hasFirstItem: 'when data.items[0].$exists() *? boolean : =false',
// Nested array access (should work after fix)
hasNestedItem: 'when data.users[0].profile.$exists() *? boolean : =false',
// Array with special chars (should work after fix)
hasSpecialArrayItem: 'when data["special-items"][0].$exists() *? boolean : =false',
});
// ====================================================================
// TEST 5: Union with Regex
// ====================================================================
const UnionRegexSchema = Interface({
// Union with regex patterns (should highlight properly after fix)
codeOrNumber: 'string(/^[A-Z]{3}$/)|string(/^\\d{6}$/)',
// Complex union with multiple regex patterns
complexPattern: 'string(/^[A-Z]{2,3}$/)|string(/^\\d{4,6}$/)|string(/^[a-z]+$/)',
// Union with mixed types and regex
mixedUnion: 'string(/^[A-Z]+$/)|number(1,100)|boolean',
});
// ====================================================================
// TEST 6: All New Types from ALL_TYPES.md
// ====================================================================
const NewTypesSchema = Interface({
// New format types (should highlight properly)
color: "hexcolor",
encodedData: "base64",
token: "jwt",
version: "semver",
// With constraints and arrays
colors: "hexcolor[]",
optionalToken: "jwt?",
versionRange: "semver(1.0.0,2.0.0)",
});
// ====================================================================
// TEST 7: Complex Conditional Validation
// ====================================================================
const ComplexConditionalSchema = Interface({
user: "any?",
config: "any?",
// Complex method combinations (should work)
accessLevel: 'when user.role.$in(admin,premium) && config.features.$contains(advanced) *? string : =basic',
// Nested conditionals (should work)
permission: 'when user.type=admin *? when config.superAdmin.$exists() *? =super : =admin : =user',
// With special characters and arrays
specialAccess: 'when user["admin-rights"][0].$exists() && config["special-features"].$contains(beta) *? boolean : =false',
});
// Test data
const testData = {
config: {
"admin-override": true,
"special config": true,
"app.version": "1.0.0",
"special-features": ["beta", "alpha"]
},
data: {
items: [{ id: 1 }, { id: 2 }],
users: [{ profile: { name: "John" } }],
"special-items": [{ type: "special" }]
},
user: {
role: "admin",
type: "admin",
"admin-rights": [{ level: "super" }]
}
};
// Run tests
console.log("๐งช Testing VSCode Extension Features...");
const results = [
QuoteTypesSchema.safeParse({ doubleQuote: "test", singleQuote: "test", backtick: "test", mixedQuotes: "test" }),
SpecialCharsSchema.safeParse({ ...testData, hasAdminOverride: true, hasSpecialConfig: true, hasDottedProp: true }),
ArrayAccessSchema.safeParse({ ...testData, hasFirstItem: true, hasNestedItem: true, hasSpecialArrayItem: true }),
UnionRegexSchema.safeParse({ codeOrNumber: "ABC", complexPattern: "AB", mixedUnion: "TEST" }),
NewTypesSchema.safeParse({ color: "#FF0000", encodedData: "SGVsbG8=", token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ", version: "1.0.0", colors: ["#FF0000"], optionalToken: undefined }),
ComplexConditionalSchema.safeParse({ ...testData, accessLevel: "premium", permission: "super", specialAccess: true })
];
results.forEach((result, i) => {
if (result.success) {
console.log(`โ
Test ${i + 1}: Success`);
} else {
console.log(`โ Test ${i + 1}: ${result.errors.join(", ")}`);
}
});