1import { some } from 'lodash';
9 validateMaximumPatpByteLength,
13 validateEthereumAddress,
17 validateNotNullAddress,
21} from 'lib/validators';
22import isPromise from 'lib/isPromise';
24const PRIVATE_KEY_CHAR_LENGTH = 64; // 64 hex characters
25const ADDRESS_CHAR_LENGTH = 40; // 40 hex characters
27// iterate over validators, exiting early if there's an error
28const buildValidator = (validators = [], validate) => value => {
29 for (const validator of validators) {
31 const error = validator(value);
42 // the final validate function can optionally return a promise
43 return validate(value);
47// maps a validator across an array of values
48export const buildArrayValidator = validator => values => {
49 const errorsOrPromises = values.map(validator);
50 if (some(errorsOrPromises, isPromise)) {
51 return Promise.all(errorsOrPromises);
54 return errorsOrPromises;
57// error object has errors if some of its fields are
58// 1) an array with any defined values
59// 2) any defined values
60export const hasErrors = iter =>
61 some(iter, v => (Array.isArray(v) ? hasErrors(v) : v !== undefined));
63export const buildPatqValidator = (validators = []) =>
64 buildValidator([validateNotEmpty, validatePatq, ...validators]);
65export const buildAnyMnemonicValidator = () =>
66 buildValidator([validateNotEmpty]);
67export const buildMnemonicValidator = () =>
68 buildValidator([validateNotEmpty, validateMnemonic]);
69export const buildCheckboxValidator = mustBe =>
71 validateOneOf(mustBe !== undefined ? [mustBe] : [true, false]),
73export const buildPassphraseValidator = () => buildValidator([]);
74// TODO: validate hdpath format
75export const buildHdPathValidator = () => buildValidator([validateHdPath]);
76export const buildPointValidator = (size = 4, validators = []) =>
79 validateMaximumPatpByteLength(size),
83export const buildSelectValidator = options =>
84 buildValidator([validateOneOf(options.map(option => option.value))]);
85export const buildHexValidator = length =>
90 validateHexLength(length),
92export const buildPrivateKeyValidator = () =>
96 validateLength(PRIVATE_KEY_CHAR_LENGTH),
98export const buildUploadValidator = () => buildValidator([validateNotEmpty]);
99export const buildAddressValidator = () =>
104 validateHexLength(ADDRESS_CHAR_LENGTH),
105 validateNotNullAddress,
106 validateEthereumAddress,
108export const buildNumberValidator = (min = 0, max = null) => {
109 let validators = [validateGreaterThan(min)];
111 validators.push(validateLessThan(max));
113 return buildValidator(validators);
116export const buildEmailValidator = validate =>
117 buildValidator([validateNotEmpty, validateEmail], validate);
119export const buildEmailArrayValidator = () =>
120 buildValidator([validateUnique, buildArrayValidator(buildEmailValidator())]);
121// the form validator is the composition of all of the field validators
122// plus an additional form validator function
123export const composeValidator = (
124 fieldValidators = {},
125 // the default form validator just returns field-level validations
126 formValidator = (values, errors) => errors
128 const names = Object.keys(fieldValidators);
130 const fieldLevelValidators = names.map(name => value =>
131 fieldValidators[name](value)
134 // async reduce errors per-field into an errors object
135 const fieldLevelValidator = values => {
136 const errorsOrPromises = names.map((name, i) =>
137 fieldLevelValidators[i](values[name])
140 const reduce = errors =>
142 (memo, name, i) => ({
149 if (some(errorsOrPromises, isPromise)) {
150 // if any of these results are a promise, await them all then reduce
151 return Promise.all(errorsOrPromises).then(reduce);
154 // otherwise return immediately
155 return reduce(errorsOrPromises);
158 // the final-form `validate` function
159 // NOTE: if we return a Promise to final-form it will toggle the `validating`
160 // state, which is expected. If our promise resolves immediately, however,
161 // that means our `validating` state flickers the UI and it looks pretty bad.
162 // The solution is to conditionally return a promise only when necessary.
164 // ask for field-level errors
165 const errorsOrPromise = fieldLevelValidator(values);
167 // pass the current values and their validity to the form-level validator
168 // that can implement conditional logic and more complex validations
169 const runFormValidator = errors => formValidator(values, errors);
171 if (isPromise(errorsOrPromise)) {
172 // if promise, promise
173 return errorsOrPromise.then(runFormValidator);
176 // otherwise, it's an errors object
177 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