- Add schema to
paramSchema.ts:
mytopic: {
params: [
{ key: "arr", labelDe: "Array", labelFa: "آرایه", type: "array", aliases: ["array"] },
{ key: "value", labelDe: "Wert", labelFa: "مقدار", type: "number" },
],
modeDefaults: {
trace: { arr: [1, 2, 3], value: 5 },
explain: { arr: [1, 2, 3], value: 5 },
},
}- Add to
topicRegistry.ts:
mytopic: {
label: "My Topic",
description: "Description...",
requiredParams: ["arr", "value"],
defaultParams: { arr: [1, 2, 3], value: 5 },
allowedModes: ["trace", "explain"],
}That's it! Validation works automatically.
{ key: "arr", type: "array" }
// Valid: [1, 2, 3]
// Invalid: [], "not-array", null{ key: "count", type: "number" }
// Valid: 42, 0, -5
// Invalid: "42", NaN, null{ key: "case", type: "string" }
// Valid: "contains", "compute"
// Invalid: 123, null{ key: "arr", aliases: ["array"] }
// User types: { "array": [1, 2] }
// Normalized to: { "arr": [1, 2] }{ key: "array", aliases: ["arr"] } // selectionsort uses this
// User types: { "arr": [1, 2] }
// Normalized to: { "array": [1, 2] }modeDefaults: {
trace: { arr: [1, 2, 3] },
trace_exam: { arr: [1, 2, 3] },
explain: { arr: [1, 2, 3] },
}modeDefaults: {
trace: { arr: [1, 2, 3, 4, 5] }, // Full example
trace_exam: { arr: [1, 2, 3, 4, 5] }, // Same
explain: { arr: [1, 2, 3] }, // Simpler for teaching
quiz: { arr: [2, 4, 6] }, // Different for quiz
}1. User edits params
↓
2. JSON.parse()
↓
3. normalizeParamKeys() → arr/array fixed
↓
4. Merge with mode defaults
↓
5. Check required params exist
↓
6. Check types (array, number, string)
↓
7. Check non-empty (arrays, values)
↓
8. Return: { ok, normalizedParams, errors, warnings }
Error: ❌ Missing required parameter: target
Fix: Add { "target": 7 }
Error: ❌ Parameter 'arr' array cannot be empty
Fix: Change [] to [1, 2, 3]
Error: ❌ Parameter 'target' must be a number, got: string
Fix: Change "7" to 7 (remove quotes)
Error: ❌ JSON syntax error: Unexpected token }
Fix: Check for missing commas, quotes, brackets
- ✅ No errors shown
- ✅ Run button enabled
- ✅ Params editor has normal border
- ❌ Errors shown below params editor
- ❌ Run button disabled
- ❌ Params editor has red border
- ❌ Error badge (
⚠️ ) on params toggle
- Select topic:
binarysearch - Remove "target" from params
- Verify: Error shown, Run disabled
- Add back "target"
- Verify: Error clears, Run enabled
- Click Run
- Verify: Core executes successfully
describe("paramValidator", () => {
it("normalizes array → arr", () => {
const result = normalizeParamKeys("bubblesort", { array: [1, 2] });
expect(result).toEqual({ arr: [1, 2] });
});
});- Check browser console for validation errors
- Try refreshing the page
- Reset params (change topic and back)
- Check
modeDefaultsin paramSchema.ts - Verify mode is in modeDefaults object
- Check browser console for errors
- Check
aliasesin paramSchema.ts - Verify canonical key is correct
- Test with both
arrandarray
normalizeAndValidateParams(
topicId: "bubblesort",
mode: "trace",
userParams: { array: [1, 2] } // Wrong key
)
// Returns:
{
ok: true,
normalizedParams: { arr: [1, 2] }, // Normalized!
errors: [],
warnings: []
}autoPopulateParams(
topicId: "binarysearch",
mode: "trace",
currentParams: { arr: [99] } // User edited
)
// Returns:
{
arr: [99], // User value preserved
target: 7 // Default added
}- Use canonical keys in schema (
arrfor most topics) - Provide mode-specific defaults when needed
- Add bilingual labels (de/fa)
- Test with empty arrays, wrong types
- Mix arr and array in same schema
- Forget to add to both paramSchema and topicRegistry
- Use string "7" instead of number 7 in defaults
- Leave mode defaults empty
params: [
{ key: "arr", type: "array", aliases: ["array"] }
],
modeDefaults: {
trace: { arr: [64, 25, 12, 22, 11] }
}params: [
{ key: "arr", type: "array", aliases: ["array"] },
{ key: "target", type: "number" }
],
modeDefaults: {
trace: { arr: [1, 3, 5, 7, 9], target: 7 }
}params: [
{ key: "case", type: "string" },
{ key: "arr", type: "array", aliases: ["array"] },
{ key: "target", type: "number" }
],
modeDefaults: {
trace: {
case: "contains",
variant: "int_asc",
arr: [1, 3, 5, 7, 9],
target: 7
}
}| Task | File | Function |
|---|---|---|
| Add new topic | paramSchema.ts |
Add to PARAM_SCHEMAS |
| Change defaults | paramSchema.ts |
Update modeDefaults |
| Add alias | paramSchema.ts |
Add to aliases array |
| Debug validation | Browser console | Check validation logs |
| Test manually | Browser | See VALIDATION_TEST_GUIDE.md |
- Full docs: VALIDATION_SYSTEM.md
- Test guide: VALIDATION_TEST_GUIDE.md
- Summary: PARAM_VALIDATION_SUMMARY.md