1import { some } from 'lodash';
8 validateMaximumPatpByteLength,
12 validateEthereumAddress,
15 validateNotNullAddress,
16} from 'lib/validators';
18// iterate over validators, exiting early if there's an error
19const buildValidator = (validators = []) => async value => {
20 for (const validator of validators) {
22 const error = await validator(value);
32// error object has errors if some of its fields are
33// 1) an array with any defined values
34// 2) any defined values
35export const hasErrors = iter =>
36 some(iter, v => (Array.isArray(v) ? hasErrors(v) : v !== undefined));
38export const buildPatqValidator = (validators = []) =>
39 buildValidator([validateNotEmpty, validatePatq, ...validators]);
40export const buildMnemonicValidator = () =>
41 buildValidator([validateNotEmpty, validateMnemonic]);
42export const buildCheckboxValidator = mustBe =>
44 validateOneOf(mustBe !== undefined ? [mustBe] : [true, false]),
46export const buildPassphraseValidator = () => buildValidator([]);
47// TODO: validate hdpath format
48export const buildHdPathValidator = () => buildValidator([validateNotEmpty]);
49export const buildPointValidator = (size = 4, validators = []) =>
52 validateMaximumPatpByteLength(size),
56export const buildSelectValidator = options =>
57 buildValidator([validateOneOf(options.map(option => option.value))]);
58export const buildHexValidator = length =>
62 validateHexLength(length),
64export const buildUploadValidator = () => buildValidator([validateNotEmpty]);
65export const buildAddressValidator = () =>
69 validateHexLength(40),
70 validateNotNullAddress,
71 validateEthereumAddress,
73export const buildNumberValidator = (min = 0) =>
74 buildValidator([validateGreaterThan(min)]);
75export const buildEmailValidator = () =>
76 buildValidator([validateNotEmpty, validateEmail]);
78// the default form validator just returns field-level validations
79const kDefaultFormValidator = (values, errors) => errors;
81// the form validator is the composition of all of the field validators
82// plus an additional form validator function
83export const composeValidator = (
85 formValidator = kDefaultFormValidator
87 const names = Object.keys(fieldValidators);
89 const fieldLevelValidators = names.map(name => value =>
90 fieldValidators[name](value)
93 // async reduce errors per-field into an errors object
94 const fieldLevelValidator = async values => {
95 const errors = await Promise.all(
96 names.map((name, i) => fieldLevelValidators[i](values[name]))
100 (memo, name, i) => ({
108 // final-form `validate` function
109 return async values => {
110 // ask for field-level errors
111 const errors = await fieldLevelValidator(values);
112 // pass the current values and their validity to the form-level validator
113 // that can implement conditional logic and more complex validations
114 return await formValidator(values, errors);
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