1import { some } from 'lodash';
9 validateMaximumPatpByteLength,
13 validateEthereumAddress,
16 validateNotNullAddress,
19} from 'lib/validators';
20import isPromise from 'lib/isPromise';
22const PRIVATE_KEY_CHAR_LENGTH = 64; // 64 hex characters
23const ADDRESS_CHAR_LENGTH = 40; // 40 hex characters
25// iterate over validators, exiting early if there's an error
26const buildValidator = (validators = [], validate) => value => {
27 for (const validator of validators) {
29 const error = validator(value);
40 // the final validate function can optionally return a promise
41 return validate(value);
45// maps a validator across an array of values
46export const buildArrayValidator = validator => values => {
47 const errorsOrPromises = values.map(validator);
48 if (some(errorsOrPromises, isPromise)) {
49 return Promise.all(errorsOrPromises);
52 return errorsOrPromises;
55// error object has errors if some of its fields are
56// 1) an array with any defined values
57// 2) any defined values
58export const hasErrors = iter =>
59 some(iter, v => (Array.isArray(v) ? hasErrors(v) : v !== undefined));
61export const buildPatqValidator = (validators = []) =>
62 buildValidator([validateNotEmpty, validatePatq, ...validators]);
63export const buildAnyMnemonicValidator = () =>
64 buildValidator([validateNotEmpty]);
65export const buildMnemonicValidator = () =>
66 buildValidator([validateNotEmpty, validateMnemonic]);
67export const buildCheckboxValidator = mustBe =>
69 validateOneOf(mustBe !== undefined ? [mustBe] : [true, false]),
71export const buildPassphraseValidator = () => buildValidator([]);
72// TODO: validate hdpath format
73export const buildHdPathValidator = () => buildValidator([validateHdPath]);
74export const buildPointValidator = (size = 4, validators = []) =>
77 validateMaximumPatpByteLength(size),
81export const buildSelectValidator = options =>
82 buildValidator([validateOneOf(options.map(option => option.value))]);
83export const buildHexValidator = length =>
88 validateHexLength(length),
90export const buildPrivateKeyValidator = () =>
94 validateLength(PRIVATE_KEY_CHAR_LENGTH),
96export const buildUploadValidator = () => buildValidator([validateNotEmpty]);
97export const buildAddressValidator = () =>
102 validateHexLength(ADDRESS_CHAR_LENGTH),
103 validateNotNullAddress,
104 validateEthereumAddress,
106export const buildNumberValidator = (min = 0) =>
107 buildValidator([validateGreaterThan(min)]);
108export const buildEmailValidator = validate =>
109 buildValidator([validateNotEmpty, validateEmail], validate);
111// the form validator is the composition of all of the field validators
112// plus an additional form validator function
113export const composeValidator = (
114 fieldValidators = {},
115 // the default form validator just returns field-level validations
116 formValidator = (values, errors) => errors
118 const names = Object.keys(fieldValidators);
120 const fieldLevelValidators = names.map(name => value =>
121 fieldValidators[name](value)
124 // async reduce errors per-field into an errors object
125 const fieldLevelValidator = values => {
126 const errorsOrPromises = names.map((name, i) =>
127 fieldLevelValidators[i](values[name])
130 const reduce = errors =>
132 (memo, name, i) => ({
139 if (some(errorsOrPromises, isPromise)) {
140 // if any of these results are a promise, await them all then reduce
141 return Promise.all(errorsOrPromises).then(reduce);
144 // otherwise return immediately
145 return reduce(errorsOrPromises);
148 // the final-form `validate` function
149 // NOTE: if we return a Promise to final-form it will toggle the `validating`
150 // state, which is expected. If our promise resolves immediately, however,
151 // that means our `validating` state flickers the UI and it looks pretty bad.
152 // The solution is to conditionally return a promise only when necessary.
154 // ask for field-level errors
155 const errorsOrPromise = fieldLevelValidator(values);
157 // pass the current values and their validity to the form-level validator
158 // that can implement conditional logic and more complex validations
159 const runFormValidator = errors => formValidator(values, errors);
161 if (isPromise(errorsOrPromise)) {
162 // if promise, promise
163 return errorsOrPromise.then(runFormValidator);
166 // otherwise, it's an errors object
167 return runFormValidator(errorsOrPromise);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list