Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* eslint-env jest */
/* eslint-disable no-underscore-dangle,no-console */
import createSandbox from 'jest-sandbox';
import { makeAssert } from '@colony/colony-js-utils';
import * as validation from '../modules/paramValidation';
import * as types from '../modules/paramTypes';
const failureMessage = 'Validation failed for SampleObject';
const assertValid = makeAssert(failureMessage);
describe('validateParams', () => {
const sandbox = createSandbox();
beforeEach(() => {
sandbox.clear();
});
test('Empty parameters validate correctly', () => {
expect(validation.validateParams({}, [])).toBe(true);
});
test('Parameters validate correctly', () => {
const spec = [
['taskId', 'number'],
['potId', 'number'],
/* @flow */
/* eslint-disable import/no-cycle */
import isPlainObject from 'lodash.isplainobject';
import { makeAssert } from '@colony/colony-js-utils';
import { validateValueType } from './paramTypes';
import type { Params, Param } from '../flowtypes';
const defaultAssert = makeAssert('Parameter Validation');
type AssertionMethod = (assertion: boolean, reason: string) => any;
export const isBoolean = (value: any) => typeof value === 'boolean';
export const areParamPairsEmpty = (paramPairs: Params) =>
paramPairs == null || (Array.isArray(paramPairs) && paramPairs.length === 0);
export const isInputEmpty = (input: any) =>
input == null ||
(isPlainObject(input) && Object.getOwnPropertyNames(input).length === 0);
export function validateValue(
value: any,
[name, type]: Param,
assertValid?: AssertionMethod = defaultAssert,
) {
/* @flow */
import BigNumber from 'bn.js';
import isPlainObject from 'lodash.isplainobject';
import { isValidAddress, makeAssert } from '@colony/colony-js-utils';
import type { ParamTypes, ParamTypePairs } from '../flowtypes';
const assert = makeAssert('Validation failed');
const TYPE_MAP = new Map([
['address', isValidAddress],
['string', value => typeof value === 'string'], // empty strings are allowed
['number', value => typeof value === 'number' || BigNumber.isBN(value)],
['boolean', value => typeof value === 'boolean'],
]);
export const validateParam = (
key: string,
type: ParamTypes,
value: any,
): boolean => {
assert(TYPE_MAP.has(type), `Parameter type "${type}" not defined`);
const check = TYPE_MAP.get(type);
client,
argsDef,
}: {
eventName: string,
client: ContractClient,
argsDef: Params,
}) {
this.eventName = eventName;
this.client = client;
this.argsDef = argsDef;
if (!this.interface)
throw new Error(`No such event "${eventName}" in loaded ABI`);
this._wrappedHandlers = new Map();
this.assertValid = makeAssert(`Validation failed for event ${eventName}`);
}
static _validatePayload(payload: any) {
const assert = makeAssert('Invalid payload');
assert(isPlainObject(payload), 'Payload must be an object');
const { data, destinationAddress, sourceAddress, value } = payload || {};
assert(isHexStrict(data), 'data must be a hex string');
assert(
isValidAddress(destinationAddress),
'destinationAddress must be a valid address',
);
assert(
isValidAddress(sourceAddress),
'sourceAddress must be a valid address',
);
constructor({
client,
defaultValues,
functionName,
name,
input,
output,
}: ContractMethodArgs = {}) {
this.name = name;
this.client = client;
this.input = input;
this.functionName = functionName;
this.assertValid = makeAssert(`Validation failed for ${name}`);
if (defaultValues) this.defaultValues = defaultValues;
if (output) this.output = output;
}
static _validateSigners(signers: any) {
const assert = makeAssert('Invalid _signers');
assert(isPlainObject(signers), 'Signers must be an object');
return Object.entries(signers || {}).every(
([address, signature]) =>
assert(
isValidAddress(address),
`"${address}" is not a valid address`,
) && this._validateSignature(signature, assert),
);
}