Nested Key
You can validate deeply nested object properties using dot notation. For example, to validate the name
property inside the profile
object which is nested within user
, you would use user.profile.name
.
Example
Request Body
{
"user": {
"profile": {
"name": "John Doe",
"age": 30
}
}
}
Validation Rule
const rules = [
{
key: "user.profile.name",
type: "string",
required: true,
min: 3,
max: 50,
}
];
Explanation
-
key
:"user.profile.name"
specifies the deeply nested property in the request body to be validated using dot notation. -
type
:"string"
indicates that the value ofuser.profile.name
should be a string. -
required
:true
means that theuser.profile.name
property is mandatory. -
min
:3
specifies that the minimum length of thename
string should be 3 characters. -
max
:50
specifies that the maximum length of thename
string should be 50 characters.