Skip to content

Commit

Permalink
refactor(tx builder)!: inline VALIDATION_MESSAGE
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 29, 2021
1 parent 618f502 commit defb7e1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
11 changes: 10 additions & 1 deletion src/tx/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
TX_FEE_OTHER_GAS,
TX_SERIALIZATION_SCHEMA,
TX_TYPE,
VALIDATION_MESSAGE,
VSN
} from './schema'
import {
Expand Down Expand Up @@ -149,6 +148,16 @@ function serializeField (value, type, prefix) {
}

function validateField (value, key, type, prefix) {
const VALIDATION_MESSAGE = {
[FIELD_TYPES.int]: ({ value, isMinusValue }) => isMinusValue ? `${value} must be >= 0` : `${value} is not of type Number or BigNumber`,
[FIELD_TYPES.amount]: ({ value, isMinusValue }) => isMinusValue ? `${value} must be >= 0` : `${value} is not of type Number or BigNumber`,
[FIELD_TYPES.id]: ({ value, prefix }) => `'${value}' prefix doesn't match expected prefix '${prefix}' or ID_TAG for prefix not found`,
[FIELD_TYPES.binary]: ({ prefix, value }) => `'${value}' prefix doesn't match expected prefix '${prefix}'`,
[FIELD_TYPES.string]: () => 'Not a string',
[FIELD_TYPES.pointers]: () => 'Value must be of type Array and contains only object\'s like \'{key: "account_pubkey", id: "ak_lkamsflkalsdalksdlasdlasdlamd"}\'',
[FIELD_TYPES.ctVersion]: () => 'Value must be an object with "vmVersion" and "abiVersion" fields'
}

const assert = (valid, params) => valid ? {} : { [key]: VALIDATION_MESSAGE[type](params) }
// All fields are required
if (value === undefined || value === null) return { [key]: 'Field is required' }
Expand Down
11 changes: 0 additions & 11 deletions src/tx/builder/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,17 +421,6 @@ export const PREFIX_ID_TAG = {
ch: ID_TAG.channel
}
export const ID_TAG_PREFIX = revertObject(PREFIX_ID_TAG)
const VALIDATION_ERROR = (msg) => msg

export const VALIDATION_MESSAGE = {
[FIELD_TYPES.int]: ({ value, isMinusValue }) => isMinusValue ? VALIDATION_ERROR(`${value} must be >= 0`) : VALIDATION_ERROR(`${value} is not of type Number or BigNumber`),
[FIELD_TYPES.amount]: ({ value, isMinusValue }) => isMinusValue ? VALIDATION_ERROR(`${value} must be >= 0`) : VALIDATION_ERROR(`${value} is not of type Number or BigNumber`),
[FIELD_TYPES.id]: ({ value, prefix }) => VALIDATION_ERROR(`'${value}' prefix doesn't match expected prefix '${prefix}' or ID_TAG for prefix not found`),
[FIELD_TYPES.binary]: ({ prefix, value }) => VALIDATION_ERROR(`'${value}' prefix doesn't match expected prefix '${prefix}'`),
[FIELD_TYPES.string]: ({ value }) => VALIDATION_ERROR('Not a string'),
[FIELD_TYPES.pointers]: ({ value }) => VALIDATION_ERROR('Value must be of type Array and contains only object\'s like \'{key: "account_pubkey", id: "ak_lkamsflkalsdalksdlasdlasdlamd"}\''),
[FIELD_TYPES.ctVersion]: ({ value }) => VALIDATION_ERROR('Value must be an object with "vmVersion" and "abiVersion" fields')
}

const BASE_TX = [
TX_FIELD('tag', FIELD_TYPES.int),
Expand Down

0 comments on commit defb7e1

Please sign in to comment.