Strickland
Search…
objectProps
The objectProps validator performs validation on every object property for which validators are defined. The validators are supplied as an object with the shape matching the object.

Parameters

The first parameter to the objectProps validator factory is an object defining the properties to be validated on objects. Validator props can also be supplied either as an object or as a function that accepts context and returns a validator props object.
1
import {objectProps} from 'strickland';
2
3
const personValidator = objectProps({
4
firstName: every([required(), length(2, 20)]),
5
lastName: every([required(), length(2, 20)]),
6
birthYear: range(1900, 2018)
7
}, {
8
message: 'The person must be valid'
9
});
Copied!

Validation Context

When validation context needs to be supplied to specific validators, an objectProps context can be used with a shape matching how the context should be applied.
1
import {objectProps} from 'strickland';
2
3
const personValidator = objectProps({
4
firstName: every([
5
required(),
6
length((context) => ({
7
minLength: context.minLength,
8
maxLength: context.maxLength
9
}))
10
]),
11
lastName: every([
12
required(),
13
length((context) => ({
14
minLength: context.minLength,
15
maxLength: context.maxLength
16
}))
17
]),
18
birthYear: range((context) => ({
19
min: context.min,
20
max: context.max
21
}))
22
}, {
23
message: 'The person must be valid'
24
});
25
26
// Create a person
27
const person = {
28
firstName: 'Stanford',
29
lastName: 'Strickland',
30
birthYear: 1925
31
};
32
33
const result = validate(personValidator, person, {
34
objectProps: {
35
firstName: {
36
minLength: 5,
37
maxLength: 20
38
},
39
lastName: {
40
minLength: 8,
41
maxLength: 23
42
},
43
birthYear: {
44
min: 1900,
45
max: 2018
46
}
47
}
48
});
Copied!

Result Properties

  • objectProps: An object with properties matching those validated, with the values of the properties representing the validation results produced during validation
The objectProps validator adds an objectProps property to the validation result with validation results of every property that was validated in the object's validators. The validation result property is named objectProps to match the name of the validator (this is a common pattern in Strickland).
1
import validate, {
2
objectProps, required, length, range, every
3
} from 'strickland';
4
5
// Define the rules for first name, last name, and birthYear
6
const personValidator = objectProps({
7
firstName: every([required(), length(2, 20)]),
8
lastName: every([required(), length(2, 20)]),
9
birthYear: range(1900, 2018)
10
});
11
12
// Create a person
13
const person = {
14
firstName: 'Stanford',
15
lastName: 'Strickland',
16
birthYear: 1925
17
};
18
19
const result = validate(personValidator, person);
20
21
/*
22
result = {
23
isValid: true,
24
value: person,
25
objectProps: {
26
firstName: {
27
isValid: true,
28
value: 'Stanford',
29
required: true,
30
minLength: 2,
31
maxLength: 25,
32
every: [
33
{
34
isValid: true,
35
value: 'Stanford',
36
required: true
37
},
38
{
39
isValid: true,
40
value: 'Stanford',
41
minLength: 2,
42
maxLength: 25
43
}
44
]
45
},
46
lastName: {
47
isValid: true,
48
value: 'Strickland',
49
required: true,
50
minLength: 2,
51
maxLength: 30,
52
every: [
53
{
54
isValid: true,
55
value: 'Strickland',
56
required: true
57
},
58
{
59
isValid: true,
60
value: 'Strickland',
61
minLength: 2,
62
maxLength: 30
63
}
64
]
65
},
66
birthYear: {
67
isValid: true,
68
value: 1925,
69
min: 1900,
70
max: 2018
71
}
72
}
73
}
74
*/
Copied!
Last modified 1yr ago