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// maps a validator across an array of values
33export const buildArrayValidator = validator => async values =>
34 await Promise.all(values.map(validator));
36// error object has errors if some of its fields are
37// 1) an array with any defined values
38// 2) any defined values
39export const hasErrors = iter =>
40 some(iter, v => (Array.isArray(v) ? hasErrors(v) : v !== undefined));
42export const buildPatqValidator = (validators = []) =>
43 buildValidator([validateNotEmpty, validatePatq, ...validators]);
44export const buildMnemonicValidator = () =>
45 buildValidator([validateNotEmpty, validateMnemonic]);
46export const buildCheckboxValidator = mustBe =>
48 validateOneOf(mustBe !== undefined ? [mustBe] : [true, false]),
50export const buildPassphraseValidator = () => buildValidator([]);
51// TODO: validate hdpath format
52export const buildHdPathValidator = () => buildValidator([validateNotEmpty]);
53export const buildPointValidator = (size = 4, validators = []) =>
56 validateMaximumPatpByteLength(size),
60export const buildSelectValidator = options =>
61 buildValidator([validateOneOf(options.map(option => option.value))]);
62export const buildHexValidator = length =>
66 validateHexLength(length),
68export const buildUploadValidator = () => buildValidator([validateNotEmpty]);
69export const buildAddressValidator = () =>
73 validateHexLength(40),
74 validateNotNullAddress,
75 validateEthereumAddress,
77export const buildNumberValidator = (min = 0) =>
78 buildValidator([validateGreaterThan(min)]);
79export const buildEmailValidator = (validators = []) =>
80 buildValidator([validateNotEmpty, validateEmail, ...validators]);
82// the form validator is the composition of all of the field validators
83// plus an additional form validator function
84export const composeValidator = (
86 // the default form validator just returns field-level validations
87 formValidator = (values, errors) => errors
89 const names = Object.keys(fieldValidators);
91 const fieldLevelValidators = names.map(name => value =>
92 fieldValidators[name](value)
95 // async reduce errors per-field into an errors object
96 const fieldLevelValidator = async values => {
97 const errors = await Promise.all(
98 names.map((name, i) => fieldLevelValidators[i](values[name]))
102 (memo, name, i) => ({
110 // the final-form `validate` function
111 return async values => {
112 // ask for field-level errors
113 const errors = await fieldLevelValidator(values);
114 // pass the current values and their validity to the form-level validator
115 // that can implement conditional logic and more complex validations
116 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