Skip to content

Commit d1af451

Browse files
committed
docs(io.output): 📝 add some note about output formatters
1 parent e7e9191 commit d1af451

1 file changed

Lines changed: 71 additions & 1 deletion

File tree

docs/3_usage_api.rst

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,77 @@ representing the metadata and validates it against a given validation profile.
5757
rocrate_metadata = json.load(f)
5858
5959
# validate the metadata dictionary
60-
result = validation_report = validate_metadata_as_dict(rocrate_metadata, settings=settings)
60+
result = validate_metadata_as_dict(rocrate_metadata, settings=settings)
6161
6262
# process the validation result as needed
6363
...
64+
65+
66+
Formatting Validation Results
67+
-----------------------------
68+
69+
Validation results can be rendered using different output formatters provided by
70+
the library. Two formatter types are available: *text* and *JSON*.
71+
Both rely on the ``rich`` Python library and integrate with the
72+
``rocrate_validator.io.output.console.Console`` class, which extends
73+
``rich.console.Console`` to support custom formatter registration.
74+
75+
To format results, create a ``Console`` instance, register one formatter,
76+
and then print any validation output object (e.g., the full report or the
77+
aggregated statistics).
78+
79+
80+
TextOutputFormatter
81+
~~~~~~~~~~~~~~~~~~~
82+
83+
``TextOutputFormatter`` renders validation reports as human-readable, styled text.
84+
It is typically used for console output, report generation, or writing results
85+
to a file.
86+
87+
.. code-block:: python
88+
89+
from rocrate_validator.io.output.console import Console
90+
from rocrate_validator.io.output.text import TextOutputFormatter
91+
92+
console = Console()
93+
console.register_formatter(TextOutputFormatter())
94+
95+
# Print the main validation result
96+
console.print(result)
97+
98+
# Print aggregated statistics (violations by severity, executed checks, etc.)
99+
console.print(result.statistics)
100+
101+
# Write the output to a file
102+
with open("validation_report.txt", "w") as f:
103+
file_console = Console(file=f)
104+
file_console.register_formatter(TextOutputFormatter())
105+
file_console.print(result.statistics)
106+
file_console.print(result)
107+
108+
109+
JSONOutputFormatter
110+
~~~~~~~~~~~~~~~~~~~
111+
112+
``JSONOutputFormatter`` produces JSON-structured output, suitable for logging,
113+
programmatic processing, or integration with external tools.
114+
115+
.. code-block:: python
116+
117+
from rocrate_validator.io.output.console import Console
118+
from rocrate_validator.io.output.json import JSONOutputFormatter
119+
120+
console = Console()
121+
console.register_formatter(JSONOutputFormatter())
122+
123+
# Print the main validation result as JSON
124+
console.print(result)
125+
126+
# Print the aggregated statistics
127+
console.print(result.statistics)
128+
129+
# Write the output to a file
130+
with open("validation_report.json", "w") as f:
131+
file_console = Console(file=f)
132+
file_console.register_formatter(JSONOutputFormatter())
133+
file_console.print(result)

0 commit comments

Comments
 (0)