1import { some } from 'lodash';
9 validateMaximumPatpByteLength,
13 validateEthereumAddress,
17 validateNotNullAddress,
20} from 'lib/validators';
21import isPromise from 'lib/isPromise';
23const PRIVATE_KEY_CHAR_LENGTH = 64; // 64 hex characters
24const ADDRESS_CHAR_LENGTH = 40; // 40 hex characters
26// iterate over validators, exiting early if there's an error
27const buildValidator = (validators = [], validate) => value => {
28 for (const validator of validators) {
30 const error = validator(value);
41 // the final validate function can optionally return a promise
42 return validate(value);
46// maps a validator across an array of values
47export const buildArrayValidator = validator => values => {
48 const errorsOrPromises = values.map(validator);
49 if (some(errorsOrPromises, isPromise)) {
50 return Promise.all(errorsOrPromises);
53 return errorsOrPromises;
56// error object has errors if some of its fields are
57// 1) an array with any defined values
58// 2) any defined values
59export const hasErrors = iter =>
60 some(iter, v => (Array.isArray(v) ? hasErrors(v) : v !== undefined));
62export const buildPatqValidator = (validators = []) =>
63 buildValidator([validateNotEmpty, validatePatq, ...validators]);
64export const buildAnyMnemonicValidator = () =>
65 buildValidator([validateNotEmpty]);
66export const buildMnemonicValidator = () =>
67 buildValidator([validateNotEmpty, validateMnemonic]);
68export const buildCheckboxValidator = mustBe =>
70 validateOneOf(mustBe !== undefined ? [mustBe] : [true, false]),
72export const buildPassphraseValidator = () => buildValidator([]);
73// TODO: validate hdpath format
74export const buildHdPathValidator = () => buildValidator([validateHdPath]);
75export const buildPointValidator = (size = 4, validators = []) =>
78 validateMaximumPatpByteLength(size),
82export const buildSelectValidator = options =>
83 buildValidator([validateOneOf(options.map(option => option.value))]);
84export const buildHexValidator = length =>
89 validateHexLength(length),
91export const buildPrivateKeyValidator = () =>
95 validateLength(PRIVATE_KEY_CHAR_LENGTH),
97export const buildUploadValidator = () => buildValidator([validateNotEmpty]);
98export const buildAddressValidator = () =>
103 validateHexLength(ADDRESS_CHAR_LENGTH),
104 validateNotNullAddress,
105 validateEthereumAddress,
107export const buildNumberValidator = (min = 0, max = null) => {
108 let validators = [validateGreaterThan(min)];
110 validators.push(validateLessThan(max));
112 return buildValidator(validators);
115export const buildEmailValidator = validate =>
116 buildValidator([validateNotEmpty, validateEmail], validate);
118// the form validator is the composition of all of the field validators
119// plus an additional form validator function
120export const composeValidator = (
121 fieldValidators = {},
122 // the default form validator just returns field-level validations
123 formValidator = (values, errors) => errors
125 const names = Object.keys(fieldValidators);
127 const fieldLevelValidators = names.map(name => value =>
128 fieldValidators[name](value)
131 // async reduce errors per-field into an errors object
132 const fieldLevelValidator = values => {
133 const errorsOrPromises = names.map((name, i) =>
134 fieldLevelValidators[i](values[name])
137 const reduce = errors =>
139 (memo, name, i) => ({
146 if (some(errorsOrPromises, isPromise)) {
147 // if any of these results are a promise, await them all then reduce
148 return Promise.all(errorsOrPromises).then(reduce);
151 // otherwise return immediately
152 return reduce(errorsOrPromises);
155 // the final-form `validate` function
156 // NOTE: if we return a Promise to final-form it will toggle the `validating`
157 // state, which is expected. If our promise resolves immediately, however,
158 // that means our `validating` state flickers the UI and it looks pretty bad.
159 // The solution is to conditionally return a promise only when necessary.
161 // ask for field-level errors
162 const errorsOrPromise = fieldLevelValidator(values);
164 // pass the current values and their validity to the form-level validator
165 // that can implement conditional logic and more complex validations
166 const runFormValidator = errors => formValidator(values, errors);
168 if (isPromise(errorsOrPromise)) {
169 // if promise, promise
170 return errorsOrPromise.then(runFormValidator);
173 // otherwise, it's an errors object
174 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