Util/validation update (#4051)
* Fix code validation for null inputs * tests WIP * tests WIP * Update validation tests * Adjust block names, slice (PR comments)
This commit is contained in:
@@ -16,10 +16,12 @@
|
||||
|
||||
import BigNumber from 'bignumber.js';
|
||||
|
||||
import util from '~/api/util';
|
||||
import apiutil from '~/api/util';
|
||||
|
||||
import { NULL_ADDRESS } from './constants';
|
||||
|
||||
// TODO: Convert to FormattedMessages as soon as comfortable with the impact, i.e. errors
|
||||
// not being concatted into strings in components, all supporting a non-string format
|
||||
export const ERRORS = {
|
||||
invalidAddress: 'address is an invalid network address',
|
||||
invalidAmount: 'the supplied amount should be a valid positive number',
|
||||
@@ -42,9 +44,14 @@ export function validateAbi (abi) {
|
||||
try {
|
||||
abiParsed = JSON.parse(abi);
|
||||
|
||||
if (!util.isArray(abiParsed)) {
|
||||
if (!apiutil.isArray(abiParsed)) {
|
||||
abiError = ERRORS.invalidAbi;
|
||||
return { abi, abiError, abiParsed };
|
||||
|
||||
return {
|
||||
abi,
|
||||
abiError,
|
||||
abiParsed
|
||||
};
|
||||
}
|
||||
|
||||
// Validate each elements of the Array
|
||||
@@ -54,8 +61,15 @@ export function validateAbi (abi) {
|
||||
|
||||
if (invalidIndex !== -1) {
|
||||
const invalid = abiParsed[invalidIndex];
|
||||
|
||||
// TODO: Needs seperate error when using FormattedMessage (no concats)
|
||||
abiError = `${ERRORS.invalidAbi} (#${invalidIndex}: ${invalid.name || invalid.type})`;
|
||||
return { abi, abiError, abiParsed };
|
||||
|
||||
return {
|
||||
abi,
|
||||
abiError,
|
||||
abiParsed
|
||||
};
|
||||
}
|
||||
|
||||
abi = JSON.stringify(abiParsed);
|
||||
@@ -76,7 +90,7 @@ function isValidAbiFunction (object) {
|
||||
}
|
||||
|
||||
return ((object.type === 'function' && object.name) || object.type === 'constructor') &&
|
||||
(object.inputs && util.isArray(object.inputs));
|
||||
(object.inputs && apiutil.isArray(object.inputs));
|
||||
}
|
||||
|
||||
function isAbiFallback (object) {
|
||||
@@ -94,7 +108,7 @@ function isValidAbiEvent (object) {
|
||||
|
||||
return (object.type === 'event') &&
|
||||
(object.name) &&
|
||||
(object.inputs && util.isArray(object.inputs));
|
||||
(object.inputs && apiutil.isArray(object.inputs));
|
||||
}
|
||||
|
||||
export function validateAddress (address) {
|
||||
@@ -102,10 +116,10 @@ export function validateAddress (address) {
|
||||
|
||||
if (!address) {
|
||||
addressError = ERRORS.invalidAddress;
|
||||
} else if (!util.isAddressValid(address)) {
|
||||
} else if (!apiutil.isAddressValid(address)) {
|
||||
addressError = ERRORS.invalidAddress;
|
||||
} else {
|
||||
address = util.toChecksumAddress(address);
|
||||
address = apiutil.toChecksumAddress(address);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -114,12 +128,12 @@ export function validateAddress (address) {
|
||||
};
|
||||
}
|
||||
|
||||
export function validateCode (code, api) {
|
||||
export function validateCode (code) {
|
||||
let codeError = null;
|
||||
|
||||
if (!code.length) {
|
||||
if (!code || !code.length) {
|
||||
codeError = ERRORS.invalidCode;
|
||||
} else if (!api.util.isHex(code)) {
|
||||
} else if (!apiutil.isHex(code)) {
|
||||
codeError = ERRORS.invalidCode;
|
||||
}
|
||||
|
||||
@@ -130,7 +144,9 @@ export function validateCode (code, api) {
|
||||
}
|
||||
|
||||
export function validateName (name) {
|
||||
const nameError = !name || name.trim().length < 2 ? ERRORS.invalidName : null;
|
||||
const nameError = !name || name.trim().length < 2
|
||||
? ERRORS.invalidName
|
||||
: null;
|
||||
|
||||
return {
|
||||
name,
|
||||
@@ -162,6 +178,7 @@ export function validateUint (value) {
|
||||
|
||||
try {
|
||||
const bn = new BigNumber(value);
|
||||
|
||||
if (bn.lt(0)) {
|
||||
valueError = ERRORS.negativeNumber;
|
||||
} else if (!bn.isInteger()) {
|
||||
|
||||
Reference in New Issue
Block a user