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 buildMnemonicValidator = () =>
64 buildValidator([validateNotEmpty, validateMnemonic]);
65export const buildCheckboxValidator = mustBe =>
67 validateOneOf(mustBe !== undefined ? [mustBe] : [true, false]),
69export const buildPassphraseValidator = () => buildValidator([]);
70// TODO: validate hdpath format
71export const buildHdPathValidator = () => buildValidator([validateHdPath]);
72export const buildPointValidator = (size = 4, validators = []) =>
75 validateMaximumPatpByteLength(size),
79export const buildSelectValidator = options =>
80 buildValidator([validateOneOf(options.map(option => option.value))]);
81export const buildHexValidator = length =>
86 validateHexLength(length),
88export const buildPrivateKeyValidator = () =>
92 validateLength(PRIVATE_KEY_CHAR_LENGTH),
94export const buildUploadValidator = () => buildValidator([validateNotEmpty]);
95export const buildAddressValidator = () =>
100 validateHexLength(ADDRESS_CHAR_LENGTH),
101 validateNotNullAddress,
102 validateEthereumAddress,
104export const buildNumberValidator = (min = 0) =>
105 buildValidator([validateGreaterThan(min)]);
106export const buildEmailValidator = validate =>
107 buildValidator([validateNotEmpty, validateEmail], validate);
109// the form validator is the composition of all of the field validators
110// plus an additional form validator function
111export const composeValidator = (
112 fieldValidators = {},
113 // the default form validator just returns field-level validations
114 formValidator = (values, errors) => errors
116 const names = Object.keys(fieldValidators);
118 const fieldLevelValidators = names.map(name => value =>
119 fieldValidators[name](value)
122 // async reduce errors per-field into an errors object
123 const fieldLevelValidator = values => {
124 const errorsOrPromises = names.map((name, i) =>
125 fieldLevelValidators[i](values[name])
128 const reduce = errors =>
130 (memo, name, i) => ({
137 if (some(errorsOrPromises, isPromise)) {
138 // if any of these results are a promise, await them all then reduce
139 return Promise.all(errorsOrPromises).then(reduce);
142 // otherwise return immediately
143 return reduce(errorsOrPromises);
146 // the final-form `validate` function
147 // NOTE: if we return a Promise to final-form it will toggle the `validating`
148 // state, which is expected. If our promise resolves immediately, however,
149 // that means our `validating` state flickers the UI and it looks pretty bad.
150 // The solution is to conditionally return a promise only when necessary.
152 // ask for field-level errors
153 const errorsOrPromise = fieldLevelValidator(values);
155 // pass the current values and their validity to the form-level validator
156 // that can implement conditional logic and more complex validations
157 const runFormValidator = errors => formValidator(values, errors);
159 if (isPromise(errorsOrPromise)) {
160 // if promise, promise
161 return errorsOrPromise.then(runFormValidator);
164 // otherwise, it's an errors object
165 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