Skip to content

Latest commit

 

History

History
169 lines (126 loc) · 6.18 KB

File metadata and controls

169 lines (126 loc) · 6.18 KB

JSONPropertyString

extends JSONProperty

returns: String

Only allows strings.

Usage

Once you have instantiated the 'JSONPropertyString' class, you can set the boundaries of the possible length of the strings via the 'set_min_length' and 'set_max_length' methods. The values that these functions receive as parameters are inclusive. To remove any boundary you can use the 'remove_min_length' and 'remove_max_length' methods.

This class also allows you to specify if the string must satisfy a pattern. Use the 'set_pattern' method to create this pattern. The pattern is a string that represents a regular expression. You must consider that the regular expression can be in any part of the string to be valid, so you might want to include ^ and $ to delimit the regular expression. To remove any pattern use the 'remove_pattern' method.

Example

In this example, the configuration structure has one required property. The property 'string' must be a string between [3, 5] characters long that only contains letters.

# Create a JSON configuration file
var json_config_file = JSONConfigFile.new()

# Create a string property, which length must be between [3, 5], and that
# only contains letters
var string_property = JSONPropertyString.new()
string_property.set_min_length(3)
string_property.set_max_length(5)
string_property.set_pattern("^[a-zA-Z]+$")

# Add a 'string' property, which must be a string with a length between
# [3, 5], and that only contains letters
json_config_file.add_property("string", string_property)

# Validate input
json_config_file.validate(json_file_path)

Valid JSON

This JSON has the required field, which is a string between [3, 5] characters long that only contains letters.

{
    "string": "ABCD"
}

Incorrect JSON: Wrong type

This JSON contains one error. The 'string' property is not the correct type.

{
    "string": 42
}

Returned error:

[
    {
        "error": JSONProperty.Errors.WRONG_TYPE,
        "expected": JSONProperty.Types.STRING,
        "context": "string",
        "as_text": "Wrong type: expected 'string', at 'string'."
    }
]

Incorrect JSON: Can not be shorter than the minimum length

This JSON contains one error. The 'string' property length is shorter than the minimum length.

{
    "string": "AB"
}

Returned error:

[
    {
        "error": JSONProperty.Errors.STRING_SHORTER_THAN_MIN,
        "length": 2,
        "min": 3,
        "context": "string",
        "as_text": "The string length (2) is shorter than the minimum length allowed (3), at 'string'."
    }
]

Incorrect JSON: Can not be longer than the maximum length

This JSON contains one error. The 'string' property value is longer than the maximum length.

{
    "string": "ABCDEF"
}

Returned error:

[
    {
        "error": JSONProperty.Errors.STRING_LONGER_THAN_MAX,
        "length": 6,
        "max": 5,
        "context": "string",
        "as_text": "The string length (6) is longer than the maximum length allowed (5), at 'string'."
    }
]

Incorrect JSON: Does not match the regular expression

This JSON contains one error. The 'string' property does not match the regular expression.

{
    "string": "ABC4"
}

Returned error:

[
    {
        "error": JSONProperty.Errors.STRING_DO_NOT_MATCH_PATTERN,
        "value": "ABC4",
        "pattern": "^[a-zA-Z]+$",
        "context": "string",
        "as_text": "'ABC4' does not match the specified pattern (^[a-zA-Z]+$), at 'string'."
    }
]

Functions

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_length min_value -> int:
The minimum length allowed for the string.
Sets the minimum length allowed. Nothing.
remove_min_length None. Removes any minimum length boundary. Nothing.
set_max_length max_value -> int:
The maximum length allowed for the string.
Sets the maximum length allowed. Nothing.
remove_max_length None. Removes any maximum length boundary. Nothing.
set_pattern pattern -> String:
The pattern the string must contain.

NOTE: Check RegEx for more information.
Sets the pattern the string must contain. int:
The result of compiling the pattern.

NOTE: Check RegEx.compile for more information.
remove_pattern None. Removes any pattern. Nothing.

Errors

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 STRING.
Wrong type: expected 'string'
STRING_SHORTER_THAN_MIN The input is shorter than the minimum length. length -> int:
The input length.
min -> int:
The minimum lenght allowed.
The string length ({length}) is shorter than the minimum length allowed ({min})
STRING_LONGER_THAN_MAX The input is longer than the maximum length. length -> int:
The input length.
max -> int:
The maximum lenght allowed.
The string length ({length}) is longer than the maximum length allowed ({max})
STRING_DO_NOT_MATCH_PATTERN The input does not match the pattern. value -> String:
The input value.
pattern -> String:
The pattern the input should match.
'{value}' does not match the specified pattern ({pattern})