Multiple Min and Max
You can specify min
and max
as objects to apply different constraints based on the data type. This is particularly useful when a field can have multiple 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: { string: 3, number: 10 },
max: { string: 15, number: 100 }
}
];
-
key
:"user.profile.age"
specifies the field in the request body to be validated. -
type
:["string", "number"]
allows the value to be either a string or a number. The validation will succeed if the value matches either type. -
required
: true indicates that theuser.profile.age
property must be present in the request body. -
min
:{ string: 3, number: 10 }
specifies different minimum constraints based on the type of the value. -
For
string
type values, the minimum length should be 3 characters. -
For
number
type values, the minimum value should be 10.max
:{ string: 15, number: 100 }
specifies different maximum constraints based on the type of the value. -
For
string
type values, the maximum length should be 15 characters. -
For
number
type values, the maximum value should be 100.