1import { some } from 'lodash';
8 validateMaximumPatpByteLength,
12 validateEthereumAddress,
15 validateNotNullAddress,
16} from 'lib/validators';
17import isPromise from 'lib/isPromise';
19// iterate over validators, exiting early if there's an error
20const buildValidator = (validators = [], validate) => value => {
21 for (const validator of validators) {
23 const error = validator(value);
34 // the final validate function can optionally return a promise
35 return validate(value);
39// maps a validator across an array of values
40export const buildArrayValidator = validator => values => {
41 const errorsOrPromises = values.map(validator);
42 if (some(errorsOrPromises, isPromise)) {
43 return Promise.all(errorsOrPromises);
46 return errorsOrPromises;
49// error object has errors if some of its fields are
50// 1) an array with any defined values
51// 2) any defined values
52export const hasErrors = iter =>
53 some(iter, v => (Array.isArray(v) ? hasErrors(v) : v !== undefined));
55export const buildPatqValidator = (validators = []) =>
56 buildValidator([validateNotEmpty, validatePatq, ...validators]);
57export const buildMnemonicValidator = () =>
58 buildValidator([validateNotEmpty, validateMnemonic]);
59export const buildCheckboxValidator = mustBe =>
61 validateOneOf(mustBe !== undefined ? [mustBe] : [true, false]),
63export const buildPassphraseValidator = () => buildValidator([]);
64// TODO: validate hdpath format
65export const buildHdPathValidator = () => buildValidator([validateNotEmpty]);
66export const buildPointValidator = (size = 4, validators = []) =>
69 validateMaximumPatpByteLength(size),
73export const buildSelectValidator = options =>
74 buildValidator([validateOneOf(options.map(option => option.value))]);
75export const buildHexValidator = length =>
79 validateHexLength(length),
81export const buildUploadValidator = () => buildValidator([validateNotEmpty]);
82export const buildAddressValidator = () =>
86 validateHexLength(40),
87 validateNotNullAddress,
88 validateEthereumAddress,
90export const buildNumberValidator = (min = 0) =>
91 buildValidator([validateGreaterThan(min)]);
92export const buildEmailValidator = validate =>
93 buildValidator([validateNotEmpty, validateEmail], validate);
95// the form validator is the composition of all of the field validators
96// plus an additional form validator function
97export const composeValidator = (
99 // the default form validator just returns field-level validations
100 formValidator = (values, errors) => errors
102 const names = Object.keys(fieldValidators);
104 const fieldLevelValidators = names.map(name => value =>
105 fieldValidators[name](value)
108 // async reduce errors per-field into an errors object
109 const fieldLevelValidator = values => {
110 const errorsOrPromises = names.map((name, i) =>
111 fieldLevelValidators[i](values[name])
114 const reduce = errors =>
116 (memo, name, i) => ({
123 if (some(errorsOrPromises, isPromise)) {
124 // if any of these results are a promise, await them all then reduce
125 return Promise.all(errorsOrPromises).then(reduce);
128 // otherwise return immediately
129 return reduce(errorsOrPromises);
132 // the final-form `validate` function
133 // NOTE: if we return a Promise to final-form it will toggle the `validating`
134 // state, which is expected. If our promise resolves immediately, however,
135 // that means our `validating` state flickers the UI and it looks pretty bad.
136 // The solution is to conditionally return a promise only when necessary.
138 // ask for field-level errors
139 const errorsOrPromise = fieldLevelValidator(values);
141 // pass the current values and their validity to the form-level validator
142 // that can implement conditional logic and more complex validations
143 const runFormValidator = errors => formValidator(values, errors);
145 if (isPromise(errorsOrPromise)) {
146 // if promise, promise
147 return errorsOrPromise.then(runFormValidator);
150 // otherwise, it's an errors object
151 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