Package dev.harrel.jsonschema
Class ValidatorFactory
java.lang.Object
dev.harrel.jsonschema.ValidatorFactory
Configurable factory for
Validator class. Provides methods for ad-hoc validation, which parse schema each time they are invoked.- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates new instance ofValidatorusing current configuration.Validates JSON instance against schema.Validates JSON instance against schema.Validates JSON instance against schema.Validates JSON instance against schema.Validates JSON instance against schema.Validates JSON instance against schema.Validates JSON instance against schema.Validates JSON instance against schema.Validates JSON instance against schema.withDefaultDialect(Dialect dialect) Sets defaultDialectwhich will be used when$schemakeyword is absent.withDialect(Dialect dialect) Registers aDialectusingDialect.getMetaSchema()value.withDisabledSchemaValidation(boolean disabledSchemaValidation) Disables schema validation against meta-schemas.withEvaluatorFactory(EvaluatorFactory evaluatorFactory) Sets additionalEvaluatorFactoryto be used alongside with the coreEvaluatorFactoryprovided byDialect.withJsonNodeFactories(JsonNodeFactory schemaNodeFactory, JsonNodeFactory instanceNodeFactory) Sets oneJsonNodeFactoryfor schema parsing and one for data parsing.withJsonNodeFactory(JsonNodeFactory jsonNodeFactory) SetsJsonNodeFactoryto be used for both schema and data parsing.withSchemaResolver(SchemaResolver schemaResolver) ComposesSchemaResolverwith default schema resolver.
-
Constructor Details
-
ValidatorFactory
public ValidatorFactory()
-
-
Method Details
-
createValidator
Creates new instance ofValidatorusing current configuration.- Returns:
- new
Validatorinstance
-
withDialect
Registers aDialectusingDialect.getMetaSchema()value. IfDialect.getMetaSchema()returns null, the dialect will not be registered. This method can be called multiple times to register multiple dialects. Keep in mind that overriding an official dialect is also possible if you provide the same meta-schema. Generally, this method should be used only when:- You need your meta-schema to be recursive (same value in
$schemaand$id). - You want to validate vocabularies integrity based on your dialect's required and supported vocabularies.
- You use
withDisabledSchemaValidation()and want to define active vocabularies for your meta-schema.
- Parameters:
dialect-Dialectto be registered- Returns:
- self
- You need your meta-schema to be recursive (same value in
-
withDefaultDialect
Sets defaultDialectwhich will be used when$schemakeyword is absent. Provided default isDialects.Draft2020Dialect.- Parameters:
dialect-Dialectto be used as default- Returns:
- self
-
withEvaluatorFactory
Sets additionalEvaluatorFactoryto be used alongside with the coreEvaluatorFactoryprovided byDialect. This is the best way to provide custom keyword support.- Parameters:
evaluatorFactory-EvaluatorFactoryto be used- Returns:
- self
-
withJsonNodeFactory
SetsJsonNodeFactoryto be used for both schema and data parsing. If you require different factories for schemas and data, please usewithJsonNodeFactories(dev.harrel.jsonschema.JsonNodeFactory, dev.harrel.jsonschema.JsonNodeFactory)Provided default isJacksonNode.Factory.- Parameters:
jsonNodeFactory-JsonNodeFactoryto be used- Returns:
- self
-
withJsonNodeFactories
public ValidatorFactory withJsonNodeFactories(JsonNodeFactory schemaNodeFactory, JsonNodeFactory instanceNodeFactory) Sets oneJsonNodeFactoryfor schema parsing and one for data parsing. Might be useful when you expect schemas to be in a different format than data (JSON/YAML). In most cases having two different factories is not required, so please just usewithJsonNodeFactory(dev.harrel.jsonschema.JsonNodeFactory)whenever possible. Provided default isJacksonNode.Factory.- Parameters:
schemaNodeFactory-JsonNodeFactoryto be used for parsing schemasinstanceNodeFactory-JsonNodeFactoryto be used for parsing data- Returns:
- self
-
withSchemaResolver
ComposesSchemaResolverwith default schema resolver. The default schema resolver resolves only official specification meta-schemas. Meta-schemas are loaded from classpath.- Parameters:
schemaResolver-SchemaResolverto be used- Returns:
- self
- See Also:
-
withDisabledSchemaValidation
Disables schema validation against meta-schemas.- Parameters:
disabledSchemaValidation- if schema validation should be disabled- Returns:
- self
-
validate
Validates JSON instance against schema. Each invocation creates temporaryValidatorwhich parses schema from scratch. If you want to validate multiple JSON instances against same schema - please usecreateValidator()and useValidatorclass directly.- Parameters:
rawSchema- string representation of schema JSONrawInstance- string representation of instance JSON- Returns:
- validation result
-
validate
Validates JSON instance against schema. Each invocation creates temporaryValidatorwhich parses schema from scratch. If you want to validate multiple JSON instances against same schema - please usecreateValidator()and useValidatorclass directly.- Parameters:
schemaProviderNode- object representing schema JSON for currently setJsonNodeFactory. E.g.com.fasterxml.jackson.databind.JsonNodefor defaultJsonNodeFactory(JacksonNode.Factory)rawInstance- string representation of instance JSON- Returns:
- validation result
- See Also:
-
validate
Validates JSON instance against schema. Each invocation creates temporaryValidatorwhich parses schema from scratch. If you want to validate multiple JSON instances against same schema - please usecreateValidator()and useValidatorclass directly.- Parameters:
schemaNode-JsonNodeschema JSON, which could be created viaJsonNodeFactoryrawInstance- string representation of instance JSON- Returns:
- validation result
- See Also:
-
validate
Validates JSON instance against schema. Each invocation creates temporaryValidatorwhich parses schema from scratch. If you want to validate multiple JSON instances against same schema - please usecreateValidator()and useValidatorclass directly.- Parameters:
rawSchema- string representation of schema JSONinstanceProviderNode- object representing instance JSON for currently setJsonNodeFactory. E.g.com.fasterxml.jackson.databind.JsonNodefor defaultJsonNodeFactory(JacksonNode.Factory)- Returns:
- validation result
- See Also:
-
validate
Validates JSON instance against schema. Each invocation creates temporaryValidatorwhich parses schema from scratch. If you want to validate multiple JSON instances against same schema - please usecreateValidator()and useValidatorclass directly.- Parameters:
schemaProviderNode- object representing schema JSON for currently setJsonNodeFactory. E.g.com.fasterxml.jackson.databind.JsonNodefor defaultJsonNodeFactory(JacksonNode.Factory)instanceProviderNode- object representing instance JSON for currently setJsonNodeFactory. E.g.com.fasterxml.jackson.databind.JsonNodefor defaultJsonNodeFactory(JacksonNode.Factory)- Returns:
- validation result
- See Also:
-
validate
Validates JSON instance against schema. Each invocation creates temporaryValidatorwhich parses schema from scratch. If you want to validate multiple JSON instances against same schema - please usecreateValidator()and useValidatorclass directly.- Parameters:
schemaNode-JsonNodeschema JSON, which could be created viaJsonNodeFactoryinstanceProviderNode- object representing instance JSON for currently setJsonNodeFactory. E.g.com.fasterxml.jackson.databind.JsonNodefor defaultJsonNodeFactory(JacksonNode.Factory)- Returns:
- validation result
- See Also:
-
validate
Validates JSON instance against schema. Each invocation creates temporaryValidatorwhich parses schema from scratch. If you want to validate multiple JSON instances against same schema - please usecreateValidator()and useValidatorclass directly.- Parameters:
rawSchema- string representation of schema JSONinstanceNode-JsonNodeinstance JSON, which could be created viaJsonNodeFactory- Returns:
- validation result
- See Also:
-
validate
Validates JSON instance against schema. Each invocation creates temporaryValidatorwhich parses schema from scratch. If you want to validate multiple JSON instances against same schema - please usecreateValidator()and useValidatorclass directly.- Parameters:
schemaProviderNode- object representing schema JSON for currently setJsonNodeFactory. E.g.com.fasterxml.jackson.databind.JsonNodefor defaultJsonNodeFactory(JacksonNode.Factory)instanceNode-JsonNodeinstance JSON, which could be created viaJsonNodeFactory- Returns:
- validation result
- See Also:
-
validate
Validates JSON instance against schema. Each invocation creates temporaryValidatorwhich parses schema from scratch. If you want to validate multiple JSON instances against same schema - please usecreateValidator()and useValidatorclass directly.- Parameters:
schemaNode-JsonNodeschema JSON, which could be created viaJsonNodeFactoryinstanceNode-JsonNodeinstance JSON, which could be created viaJsonNodeFactory- Returns:
- validation result
- See Also:
-