From 92141e3caffcc2e3b24c7613a93e00f8c868d7f8 Mon Sep 17 00:00:00 2001 From: Holger Partsch Date: Fri, 17 Nov 2023 02:38:31 +0100 Subject: [PATCH] Adapt collector context documentation (#876) After https://github.com/networknt/json-schema-validator/pull/812 some adaptions are required to retrieve data from the CollectorContext. --- doc/collector-context.md | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/doc/collector-context.md b/doc/collector-context.md index 6be3d0af5..8d687b53b 100644 --- a/doc/collector-context.md +++ b/doc/collector-context.md @@ -35,17 +35,34 @@ collectorContext.add(SAMPLE_COLLECTOR,"sample-string") ``` -To validate the schema with the ability to use CollectorContext, validateAndCollect method has to be invoked on the JsonSchema class. This class returns a ValidationResult that contains the errors encountered during validation and a CollectorContext instance. Objects constructed by Collectors or directly added to CollectorContext can be retrieved from CollectorContext by using the name they were added with. - +To retrieve the `CollectorContext` after validation or walking, automatic reseting of collector contexts has to be disabled: ```java - ValidationResult validationResult = jsonSchema.validateAndCollect(jsonNode); - CollectorContext context = validationResult.getCollectorContext(); - List contextValue = (List)context.get(SAMPLE_COLLECTOR); - +SchemaValidatorsConfig configuration = new SchemaValidatorsConfig(); +configuration.setResetCollectorContext(false); +JsonSchema jsonSchema = factory.getSchema(uri, configuration); + ``` -Note that CollectorContext will be removed from ThreadLocal once validateAndCollect method returns. Also the data collected by Collectors is loaded into CollectorContext only after all the validations are done. +To use the `CollectorContext` while validating, the `validateAndCollect` method has to be invoked on the `JsonSchema` class. +This method returns a `ValidationResult` that contains the errors encountered during validation and a `CollectorContext` instance. +Objects constructed by collectors or directly added to `CollectorContext` can be retrieved from `CollectorContext` by using the name they were added with. + + +```java +List contextValue; +try { + ValidationResult validationResult = jsonSchema.validateAndCollect(jsonNode); + CollectorContext context = validationResult.getCollectorContext(); + contextValue = (List)context.get(SAMPLE_COLLECTOR); +} finally { + // Remove all data from collector context. + // Otherwise, data is kept between validation runs which is usually unintended + // and a potential memory leak. + if (CollectorContext.getInstance() != null) CollectorContext.getInstance().reset(); +} +// do something with contextValue +``` There might be usecases where a collector needs to collect the data at multiple touch points. For example one usecase might be collecting data in a validator and a formatter. If you are using a Collector rather than a Object, the combine method of the Collector allows to define how we want to combine the data into existing Collector. CollectorContext combineWithCollector method calls the combine method on the Collector. User just needs to call the CollectorContext combineWithCollector method every time some data needs to merged into existing Collector. The collect method on the Collector is called by the framework at the end of validation to return the data that was collected.