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.nameshould be a string. -
required:truemeans that theuser.profile.nameproperty is mandatory. -
min:3specifies that the minimum length of thenamestring should be 3 characters. -
max:50specifies that the maximum length of thenamestring should be 50 characters.