index/functions/validateDecimalRangeStrict.md
validateDecimalRangeStrict(
value,min,max,fieldName):void
Validates decimal number within range (handles NaN, Infinity, type checking)
Centralizes strict decimal range validation for numeric inputs.
field name
Replaces Pattern
Before (Duplicate across services):
const num = parseFloat(value);
if (isNaN(num) || num < min || num > max) {
throw new ValidationError(`${fieldName} must be between ${min} and ${max}`);
}
After (Single call):
validateDecimalRangeStrict(value, min, max, 'slippageTolerance');
Parameters
| Parameter | Type | Description |
|---|---|---|
value | string | number | The value to validate (string or number) |
min | number | Minimum allowed value (inclusive) |
max | number | Maximum allowed value (inclusive) |
fieldName | string | Field name for error messages (e.g., 'slippageTolerance', 'priceImpact') |
field name
Returns
void
Throws
If value is not a valid number within range, or if NaN/Infinity detected
Examples
Slippage tolerance validation
import { validateDecimalRangeStrict } from '../utils/validation-patterns';
function swap(slippage: unknown) {
validateDecimalRangeStrict(slippage, 0, 1, 'slippageTolerance');
// Validation passed - slippage is a valid decimal between 0 and 1
}
Price impact validation
import { validateDecimalRangeStrict } from '../utils/validation-patterns';
function calculateImpact(impactPercent: unknown) {
validateDecimalRangeStrict(impactPercent, 0, 100, 'priceImpact');
// Validation passed - impact is valid percentage between 0 and 100
}
Since
6.25.0