Utilizing functions

High-order validators

Consider writing a set of pure high-order validator functions that accept optional set of parameters and always return a validator function expected by React Advanced Form:
validators/minLength.js
1
export default function minLength(length) {
2
// returns a validator function expected by RAF
3
return ({ value, fieldProps, form }) => {
4
return value.length >= length
5
}
6
}
Copied!
Use the minLength function parametrically whenever necessary:
validationRules.js
1
import minLength from './validators/minLength'
2
3
const validationRules = {
4
type: {
5
tel: {
6
minLength: minLength(8),
7
},
8
},
9
name: {
10
firstName: {
11
minLength: minLength(2),
12
},
13
},
14
}
Copied!
Last modified 3yr ago
Copy link