Commit e2a73e42 authored by tom's avatar tom

contract method: format all args to string

parent 354193e3
...@@ -18,7 +18,7 @@ export default function useFormatFieldValue({ argType, argTypeMatchInt }: Params ...@@ -18,7 +18,7 @@ export default function useFormatFieldValue({ argType, argTypeMatchInt }: Params
if (argTypeMatchInt) { if (argTypeMatchInt) {
const formattedString = value.replace(/\s/g, ''); const formattedString = value.replace(/\s/g, '');
return parseInt(formattedString); return formattedString;
} }
if (argType === 'bool') { if (argType === 'bool') {
...@@ -26,11 +26,11 @@ export default function useFormatFieldValue({ argType, argTypeMatchInt }: Params ...@@ -26,11 +26,11 @@ export default function useFormatFieldValue({ argType, argTypeMatchInt }: Params
switch (formattedValue) { switch (formattedValue) {
case 'true': { case 'true': {
return true; return 'true';
} }
case 'false':{ case 'false':{
return false; return 'false';
} }
default: default:
......
...@@ -18,15 +18,13 @@ export default function useValidateField({ isOptional, argType, argTypeMatchInt ...@@ -18,15 +18,13 @@ export default function useValidateField({ isOptional, argType, argTypeMatchInt
return argType.match(BYTES_REGEXP); return argType.match(BYTES_REGEXP);
}, [ argType ]); }, [ argType ]);
// some values are formatted before they are sent to the validator return React.useCallback((value: string | undefined) => {
// see ./useFormatFieldValue.tsx hook
return React.useCallback((value: string | number | boolean | undefined) => {
if (value === undefined || value === '') { if (value === undefined || value === '') {
return isOptional ? true : 'Field is required'; return isOptional ? true : 'Field is required';
} }
if (argType === 'address') { if (argType === 'address') {
if (typeof value !== 'string' || !isAddress(value)) { if (!isAddress(value)) {
return 'Invalid address format'; return 'Invalid address format';
} }
...@@ -41,11 +39,13 @@ export default function useValidateField({ isOptional, argType, argTypeMatchInt ...@@ -41,11 +39,13 @@ export default function useValidateField({ isOptional, argType, argTypeMatchInt
} }
if (argTypeMatchInt) { if (argTypeMatchInt) {
if (typeof value !== 'number' || Object.is(value, NaN)) { const formattedValue = Number(value);
if (Object.is(formattedValue, NaN)) {
return 'Invalid integer format'; return 'Invalid integer format';
} }
if (value > argTypeMatchInt.max || value < argTypeMatchInt.min) { if (formattedValue > argTypeMatchInt.max || formattedValue < argTypeMatchInt.min) {
const lowerBoundary = argTypeMatchInt.isUnsigned ? '0' : `-1 * 2 ^ ${ Number(argTypeMatchInt.power) - 1 }`; const lowerBoundary = argTypeMatchInt.isUnsigned ? '0' : `-1 * 2 ^ ${ Number(argTypeMatchInt.power) - 1 }`;
const upperBoundary = argTypeMatchInt.isUnsigned ? `2 ^ ${ argTypeMatchInt.power } - 1` : `2 ^ ${ Number(argTypeMatchInt.power) - 1 } - 1`; const upperBoundary = argTypeMatchInt.isUnsigned ? `2 ^ ${ argTypeMatchInt.power } - 1` : `2 ^ ${ Number(argTypeMatchInt.power) - 1 } - 1`;
return `Value should be in range from "${ lowerBoundary }" to "${ upperBoundary }" inclusively`; return `Value should be in range from "${ lowerBoundary }" to "${ upperBoundary }" inclusively`;
...@@ -55,7 +55,7 @@ export default function useValidateField({ isOptional, argType, argTypeMatchInt ...@@ -55,7 +55,7 @@ export default function useValidateField({ isOptional, argType, argTypeMatchInt
} }
if (argType === 'bool') { if (argType === 'bool') {
if (typeof value !== 'boolean') { if (value !== 'true' && value !== 'false') {
return 'Invalid boolean format. Allowed values: true, false'; return 'Invalid boolean format. Allowed values: true, false';
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment