everit-org / json-schema

JSON Schema validator for java, based on the org.json API
Apache License 2.0
854 stars 282 forks source link

Is org.everit.json.schema.Validator thread-safe #511

Closed jacob2221 closed 6 hours ago

jacob2221 commented 1 week ago

Hi,

I am planning to create a custom validator with lenient flag turned on for primitives as shown in the code below -

Validator validator = Validator.builder()
    .primitiveValidationStrategry(PrimitiveValidationStrategy.LENIENT)
    .build();
validator.performValidation(schema, input);

I am using spring boot. Can I create one instance of this validator and use it across the application or do you recommend creating new Validator each time I need to apply schema validation?

erosb commented 1 week ago

Hello, as the README says:

Note: the Validator class is immutable and thread-safe, so you don't have to create a new one for each validation, it is enough to configure it only once.

On the other hand it is also cheap to create an instance, so if you want, there is no issue with creating a new instance for each validation, and expose only the Schema object as a spring bean.

jacob2221 commented 6 hours ago

Thank you