Strickland
Search…
Nested Objects
The composition ability for combining validators together on props and objects opens up complex possibilities. Another great example is nested objects.
1
import validate, {
2
objectProps, required, length, range, every
3
} from 'strickland';
4
5
const personValidator = objectProps({
6
name: every([required(), length(5, 40)]),
7
address: objectProps({
8
street: every([required(), objectProps({
9
number: every([required(), range(1, 99999)]),
10
name: every([required(), length(2, 40)])
11
})]),
12
city: required(),
13
state: every([required(), length(2, 2)])
14
})
15
});
16
17
const person = {
18
name: 'Marty McFly',
19
address: {
20
street: {
21
number: 9303,
22
name: 'Lyon Drive'
23
},
24
city: 'Hill Valley',
25
state: 'CA'
26
}
27
};
28
29
const result = validate(personValidator, person);
Copied!
Objects can be nested without any limits on depth. And any type of validator can be used anywhere in the tree.
Last modified 1yr ago
Copy link