extends JSONProperty
returns: float
Only allows real numbers.
Once you have instantiated the 'JSONPropertyNumber' class, you can set the boundaries of the possible values via the 'set_min_value' and 'set_max_value' methods. The values that these functions receive as parameters are inclusive. To remove any boundary you can use the 'remove_min_value' and 'remove_max_value' methods.
In this example, the configuration structure has one required property. The property 'number' must be a real number between [0, 10].
# Create a JSON configuration file
var json_config_file = JSONConfigFile.new()
# Create a number property, which value must be between [0, 10]
var number_property = JSONPropertyNumber.new()
number_property.set_min_value(0)
number_property.set_max_value(10)
# Add a 'number' property, which must be a real number between [0, 10]
json_config_file.add_property("number", number_property)
# Validate input
json_config_file.validate(json_file_path)This JSON has the required field, which is a real number between [0, 10]:
{
"number": 3.14159
}This JSON contains one error. The 'number' property is not the correct type.
{
"number": "42"
}Returned error:
[
{
"error": JSONProperty.Errors.WRONG_TYPE,
"expected": JSONProperty.Types.NUMBER,
"context": "number",
"as_text": "Wrong type: expected 'number', at 'number'."
}
]This JSON contains one error. The 'number' property value is less than the minimum value.
{
"number": -0.1
}Returned error:
[
{
"error": JSONProperty.Errors.NUMBER_VALUE_LESS_THAN_MIN,
"value": -0.1,
"min": 0.0,
"context": "number",
"as_text": "-0.100 is less than the minimum allowed (0.000), at 'number'."
}
]This JSON contains one error. The 'number' property value is more than the maximum value.
{
"number": 10.1
}Returned error:
[
{
"error": JSONProperty.Errors.NUMBER_VALUE_MORE_THAN_MAX,
"value": 10.1,
"max": 10.0,
"context": "number",
"as_text": "10.100 is more than the maximum allowed (10.000), at 'number'."
}
]The public methods of this class are:
| Name | Params | Description | Returns |
|---|---|---|---|
| set_preprocessor | processor -> JSONConfigProcessor: Object that defines the function to execute before the validation process. |
Sets the process to execute before the validation process. | Nothing. |
| set_postprocessor | processor -> JSONConfigProcessor: Object that defines the function to execute after the validation process. |
Sets the process to execute after the validation process. | Nothing. |
| set_min_value | min_value -> float: The minimum value allowed. |
Sets the minimum value allowed. | Nothing. |
| remove_min_value | None. | Removes any minimum boundary. | Nothing. |
| set_max_value | max_value -> float: The maximum value allowed. |
Sets the maximum value allowed. | Nothing. |
| remove_max_value | None. | Removes any maximum boundary. | Nothing. |
This class could directly raise any of the following errors:
| Enum value | Description | Params | As text |
|---|---|---|---|
| WRONG_TYPE | The type of the input does not match the expected one. | expected -> int: Takes the value NUMBER. |
Wrong type: expected 'number' |
| NUMBER_VALUE_LESS_THAN_MIN | The value of the input is less than the minimum. | value -> float: The input value. min -> float: The minimum value allowed. |
{value} is less than the minimum allowed ({min}) |
| NUMBER_VALUE_MORE_THAN_MAX | The value of the input is more than the maximum. | value -> float: The input value. max -> float: The maximum value allowed. |
{value} is more than the maximum allowed ({max}) |