{"record":{"id":"14c3146686d137d1","repo":"sickn33/agentic-awesome-skills","slug":"invalid-amount","errorCode":null,"errorMessage":"Invalid amount","messagePattern":"Invalid amount","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"info","filePath":"skills/kaizen/SKILL.md","lineNumber":216,"sourceCode":"\n// Caller must prove array is non-empty\nconst items: number[] = [1, 2, 3];\nif (items.length > 0) {\n  firstItem(items as NonEmptyArray<number>); // Safe\n}\n````\n\nFunction signature guarantees safety\n</Good>\n\n#### Validation Error Proofing\n\n<Good>\n```typescript\n// Error: Validation after use\nconst processPayment = (amount: number) => {\n  const fee = amount * 0.03; // Used before validation!\n  if (amount <= 0) throw new Error('Invalid amount');\n  // ...\n};\n\n// Good: Validate immediately\nconst processPayment = (amount: number) => {\nif (amount <= 0) {\nthrow new Error('Payment amount must be positive');\n}\nif (amount > 10000) {\nthrow new Error('Payment exceeds maximum allowed');\n}\n\nconst fee = amount \\* 0.03;\n// ... now safe to use\n};\n\n// Better: Validation at boundary with branded type\ntype PositiveNumber = number & { readonly \\_\\_brand: 'PositiveNumber' };","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/sickn33/agentic-awesome-skills/blob/58d857988fcfac6986206bca2b2fe223aa437e4b/skills/kaizen/SKILL.md#L198-L234","documentation":"This error comes from a TypeScript snippet inside the kaizen skill documentation that demonstrates the 'validate before use' principle. processPayment throws Error('Invalid amount') when amount <= 0, but only after the fee was already computed from it. It is example code, not a shipped library function; the throw exists to teach that inputs must be validated before any computation or state change.","triggerScenarios":"Calling the example processPayment(amount) with amount <= 0 (e.g. processPayment(0) or processPayment(-5)), or passing NaN, which slips past the amount <= 0 check while the already-computed fee is NaN.","commonSituations":"Developers copying the teaching snippet into real payment code without adding NaN/non-finite/type checks; validating only after logging, persisting, or computing with the amount; test fixtures using 0 or negative amounts.","solutions":["Move all validation (typeof amount === 'number', Number.isFinite(amount), amount > 0) to the top of the function before any computation","Add NaN and non-finite guards, since NaN <= 0 is false and NaN passes the original check","Throw a typed/domain error instead of a bare Error so callers can branch on validation failures"],"exampleFix":"// before\nconst processPayment = (amount: number) => {\n  const fee = amount * 0.03; // used before validation\n  if (amount <= 0) throw new Error('Invalid amount');\n};\n\n// after\nconst processPayment = (amount: number) => {\n  if (typeof amount !== 'number' || !Number.isFinite(amount) || amount <= 0) {\n    throw new Error('Invalid amount');\n  }\n  const fee = amount * 0.03;\n};","handlingStrategy":"validation","validationCode":"function isValidAmount(a: unknown): a is number {\n  return typeof a === 'number' && Number.isFinite(a) && a > 0;\n}\n// call before processPayment\nif (!isValidAmount(input)) throw new TypeError('amount must be a positive finite number');","typeGuard":"function isValidAmount(a: unknown): a is number {\n  return typeof a === 'number' && Number.isFinite(a) && a > 0;\n}","tryCatchPattern":null,"preventionTips":["Validate all inputs before any computation or state mutation","Treat boundary inputs as unknown and narrow with type guards instead of trusting declared types"],"tags":["documentation","validation","typescript","example-code"],"backgroundTag":"input-validation-failed","analyzedSha":"58d857988fcfac6986206bca2b2fe223aa437e4b","analyzedAt":"2026-08-26T11:55:59.350Z","schemaVersion":2},"datasetVersion":"2026-08-26T14:46:13.012Z"}