Constraining Number
You may want to constrain the value of a number to a specific range or define a minimum or maximum value.
Let's add a dateOfBirth property. now we want to set specific ranges for day, month and year. we will also need to set range constraints for the age property
1{ 2 "name": "John Doe ", 3 "age": 25, 4 "dateOfBirth": { 5 "day": 1, 6 "month": 1, 7 "year": 1995 8 } 9}
Schema Definition
To apply constraints on number values, you can use the following keywords:
- x ≥ minimum
- x ≤ maximum
Example: age property
1{ 2 "age":{ 3 "type": "integer", 4 "minimum": 18, 5 "maximum": 60 6 } 7}
Example: dateOfBirth property
1{ 2 "dateOfBirth": { 3 "type": "object", 4 "properties": { 5 "year": { 6 "type": "integer", 7 "minimum": 1954, 8 "maximum": 2024 9 }, 10 ... 11 } 12 } 13}
now try to modify the age and dateOfBirth properties in the side editor with the constraints mentioned below:
- age should be between 18 and 60
- dateOfBirth.year should be between 1964 and 2024
- dateOfBirth.month should be between 1 and 12
- dateOfBirth.day should be between 1 and 31
Loading...
Output
Please click the button or use
Shift+Enter
to view the output