DEV Community

Cover image for Engineering "Safe-by-Design" Health Tools: Input Validation & Logic
kandz
kandz

Posted on Originally published at tools.kandz.me

Engineering "Safe-by-Design" Health Tools: Input Validation & Logic

When developing tools that assist with medical calculations—like a dosage calculator—you aren't just writing code; you are building an interface that requires high-integrity validation. Unlike a standard e-commerce counter, a medical tool must fail gracefully and prevent dangerous input scenarios.

The "Defense-in-Depth" Approach

In a dosage calculator, the greatest risk isn't just the math; it's the edge cases. What if the user enters a negative number? What if they use a decimal point where a whole number is expected? We handle this using a strict validation layer before our calculation engine ever fires.

Robust Input Handling in JavaScript

Here is how we implement a validation wrapper to ensure the safety of our inputs before performing the conversion:

/**
 * A safe wrapper for medical dosage calculations.
 * Ensures input is numeric, positive, and within reasonable physiological limits.
 */
function getSafeDosage(weight) {
  const sanitizedWeight = parseFloat(weight);

  // Guard Clause: Prevent non-numeric or impossible values
  if (isNaN(sanitizedWeight)) {
    throw new Error("Input must be a valid number.");
  }

  // Guard Clause: Prevent extreme outliers that might indicate a typo
  if (sanitizedWeight <= 0 || sanitizedWeight > 200) {
    throw new Error("Weight out of realistic range for domestic canine calculation.");
  }

  // Calculate: 1mg per lb standard guideline
  const dosage = sanitizedWeight * 1.0;

  return {
    status: 'success',
    weight: sanitizedWeight,
    recommendedMg: dosage.toFixed(2),
    timestamp: new Date().toISOString()
  };
}

// Example usage
try {
  const result = getSafeDosage("45.5");
  console.log(`Target Dose: ${result.recommendedMg} mg`);
} catch (e) {
  console.error("Calculation Blocked:", e.message);
}
Enter fullscreen mode Exit fullscreen mode

Why This Matters

  1. Sanitization: By using parseFloat and strict range checks, we mitigate the risk of a user typing an invalid character or a weight that is physiologically impossible (e.g., a 200lb+ dog or a negative weight).
  2. Atomic Logic: The calculation is separated from the UI logic. This allows you to write unit tests for the getSafeDosage function independently of the React/Vue/Svelte framework you choose to use.
  3. User Transparency: Always provide clear feedback. If the calculation is blocked, explain why (e.g., "Weight out of range") rather than showing a generic error.

Real-World Application

Building these tools requires balancing technical accuracy with accessibility. We have applied these defensive programming principles to our own implementation, ensuring that the logic remains predictable and reliable for pet owners in a pinch.

You can explore the full implementation of this "safe-by-design" approach in our live tool: Dog Benadryl (Diphenhydramine) Dosage Calculator

Top comments (0)