Multiple Types
If you provide an array of types (e.g., ['string', 'number']
), the value will be considered valid if it matches any one of the specified types.
Example
Request Body
{
"user": {
"profile": {
"name": "John Doe",
"age": 30
}
}
}
Validation Rules
const rules = [
{
key: "user.profile.age",
type: ["string", "number"],
required: true,
min: 18,
max: 100,
}
];
-
key
: "user.profile.age" specifies the field in the request body to be validated. -
type
: ["string", "number"] indicates that the value can be either a string or a number. The validation will succeed if the value matches any one of these types. -
required
: true means that theuser.profile.age
property must be present in the request body. -
min
: 18 specifies that the minimum allowed value for theage
should be 18. This applies if the value is of typenumber
. -
max
: 100 specifies that the maximum allowed value for theage
should be 100. This applies if the value is of typenumber
.