How to use the @colony/colony-js-utils.makeAssert function in @colony/colony-js-utils

To help you get started, we’ve selected a few @colony/colony-js-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github JoinColony / colonyJS / packages / colony-js-contract-client / src / __tests__ / paramValidation.js View on Github external
/* 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'],
github JoinColony / colonyJS / packages / colony-js-contract-client / src / modules / paramValidation.js View on Github external
/* @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,
) {
github JoinColony / colonyJS / packages / colony-js-contract-client / src / modules / validate.js View on Github external
/* @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);
github JoinColony / colonyJS / packages / colony-js-contract-client / src / classes / ContractEvent.js View on Github external
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}`);
  }
github JoinColony / colonyJS / packages / colony-js-contract-client / src / classes / MultisigOperation.js View on Github external
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',
    );
github JoinColony / colonyJS / packages / colony-js-contract-client / src / classes / ContractMethod.js View on Github external
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;
  }
github JoinColony / colonyJS / packages / colony-js-contract-client / src / classes / MultisigOperation.js View on Github external
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),
    );
  }