Skip to content

Latest commit

 

History

History
101 lines (72 loc) · 3.48 KB

JSON-PROPERTY-FILE.md

File metadata and controls

101 lines (72 loc) · 3.48 KB

JSONPropertyFile

extends JSONProperty

returns: File

Only allows strings representing a path to a file. This path can be absolute or relative. When the file path is relative, it would be relative to the directory that contains the JSON configuration file that specifies this field. Please, use '/' as a path delimiter.

Usage

Once you have instantiated the 'JSONPropertyFile' class, you can determine how to open the file via the 'set_mode_flag' method. It would be File.READ by default.

Example

In this example, the configuration structure has one required property. The property 'file' must be a path to another file.

# Create a JSON configuration file
var json_config_file = JSONConfigFile.new()
	
# Add the 'file' property, which is a path to a file
json_config_file.add_property("file", JSONPropertyFile.new())

# Validate input
json_config_file.validate(json_file_path)

Valid JSON

This JSON has the required field.

{
    "file": "file.txt"
}

Incorrect JSON: Wrong type

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

{
    "file": 42
}

Returned error:

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

Incorrect JSON: Missing image

This JSON contains one error. The 'file' property indicates a path to a file that does not exist.

{
    "file": "missing_file.txt"
}

Returned error:

[
    {
        "error": JSONProperty.Errors.COULD_NOT_OPEN_IMAGE,
        "code": ERR_FILE_NOT_FOUND,
        "context": "file",
        "as_text": "Could not open the file, at 'file'."
    }
]

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_mode_flag mode_flag -> int:
The flag that determines how to open the file.

NOTE: Check File.ModeFlags for more information.
Determines how to open the file. 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 FILE.
Wrong type: expected 'file path'
COULD_NOT_OPEN_FILE The file path leads to a nonexistent file or an error occurred when opening the file. code -> int:
Error code returned by File.open().
Could not open the file