Package org.trustdeck.service
Class JsonSchemaService
java.lang.Object
org.trustdeck.service.JsonSchemaService
Class used to handle the entity type's JSON schemas and their validation.
Includes a simple LRU caching for compiled schemas.
- Author:
- Armin Müller
-
Constructor Summary
ConstructorsConstructorDescriptionJsonSchemaService(com.fasterxml.jackson.databind.ObjectMapper om) Constructor that defines the object mapper and initializes the schema factory and the meta schema. -
Method Summary
Modifier and TypeMethodDescriptioncom.fasterxml.jackson.databind.JsonNodebuildInstanceSchema(com.fasterxml.jackson.databind.JsonNode definition) Build an instance JSON schema from a validated definition in JsonNode form that can be used for validating entity instances.com.fasterxml.jackson.databind.JsonNodebuildInstanceSchema(org.jooq.JSONB definition) Build an instance JSON schema from a validated definition in JSONB form that can be used for validating entity instances.com.networknt.schema.JsonSchemagetCompiledSchemaFromDefinition(com.fasterxml.jackson.databind.JsonNode typeDefinition) Build or reuse a compiled schema for an entity type (build from its definition or get from LRU-cached).com.networknt.schema.JsonSchemagetCompiledSchemaFromDefinition(org.jooq.JSONB typeDefinition) Build or reuse a compiled schema for an entity type (build from its definition or get from LRU-cached).validateDefinition(com.fasterxml.jackson.databind.JsonNode definition) Validates the given definition.validateInstance(com.fasterxml.jackson.databind.JsonNode instance, com.fasterxml.jackson.databind.JsonNode instanceSchema) Validate an instance JSON against an instance schema.validateInstance(com.fasterxml.jackson.databind.JsonNode data, com.networknt.schema.JsonSchema compiledInstanceSchema) Validate an instance JsonNode against a previously compiled instance schema.validateInstance(org.jooq.JSONB instanceData, com.fasterxml.jackson.databind.JsonNode instanceSchema) Validate an instance JSONB against an instance schema.validateInstance(org.jooq.JSONB instanceData, com.networknt.schema.JsonSchema compiledInstanceSchema) Validate an instance JSONB against a previously compiled instance schema.validateProjectTypeIsSuperset(com.fasterxml.jackson.databind.JsonNode baseDef, com.fasterxml.jackson.databind.JsonNode projectDef) Ensure the project specific type definition (projectDef) is a superset of a base type (baseDef): Contains all base attributes (no deletions) Same type for shared attributes Constraints can only be stricter, never weaker: numbers: min ≥ base.min; max ≤ base.max strings: minLength ≥ base.minLength; maxLength ≤ base.maxLength pattern: require equality to base type
-
Constructor Details
-
JsonSchemaService
public JsonSchemaService(com.fasterxml.jackson.databind.ObjectMapper om) Constructor that defines the object mapper and initializes the schema factory and the meta schema.- Parameters:
om- the object mapper to map from the meta schema file to a JsonNode (will be auto injected by spring boot)
-
-
Method Details
-
validateDefinition
Validates the given definition.- Parameters:
definition- the schema definition- Returns:
- a list of human-readable errors (an empty list means its valid)
-
buildInstanceSchema
public com.fasterxml.jackson.databind.JsonNode buildInstanceSchema(com.fasterxml.jackson.databind.JsonNode definition) Build an instance JSON schema from a validated definition in JsonNode form that can be used for validating entity instances.- Parameters:
definition- the validated schema definition- Returns:
- an instance JSON schema
-
buildInstanceSchema
public com.fasterxml.jackson.databind.JsonNode buildInstanceSchema(org.jooq.JSONB definition) Build an instance JSON schema from a validated definition in JSONB form that can be used for validating entity instances.- Parameters:
definition- the validated schema definition- Returns:
- an instance JSON schema
-
validateProjectTypeIsSuperset
public List<String> validateProjectTypeIsSuperset(com.fasterxml.jackson.databind.JsonNode baseDef, com.fasterxml.jackson.databind.JsonNode projectDef) Ensure the project specific type definition (projectDef) is a superset of a base type (baseDef):- Contains all base attributes (no deletions)
- Same type for shared attributes
- Constraints can only be stricter, never weaker:
- numbers: min ≥ base.min; max ≤ base.max
- strings: minLength ≥ base.minLength; maxLength ≤ base.maxLength
- pattern: require equality to base type
- Parameters:
baseDef- the type definition of the base typeprojectDef- the type definition for the project-specific type- Returns:
- a list of errors encountered during validation
-
validateInstance
public List<String> validateInstance(com.fasterxml.jackson.databind.JsonNode instance, com.fasterxml.jackson.databind.JsonNode instanceSchema) Validate an instance JSON against an instance schema.- Parameters:
instance- the instance as a JsonNodeinstanceSchema- the schema to evaluate against- Returns:
- a list of validation errors (an empty list means its valid)
-
validateInstance
public List<String> validateInstance(org.jooq.JSONB instanceData, com.fasterxml.jackson.databind.JsonNode instanceSchema) Validate an instance JSONB against an instance schema.- Parameters:
instanceData- the instance as a JSONBinstanceSchema- the schema to evaluate against- Returns:
- a list of validation errors (an empty list means its valid)
-
validateInstance
public List<String> validateInstance(org.jooq.JSONB instanceData, com.networknt.schema.JsonSchema compiledInstanceSchema) Validate an instance JSONB against a previously compiled instance schema.- Parameters:
instanceData- the instance as a JSONBcompiledInstanceSchema- the already compiled schema to evaluate against- Returns:
- a list of validation errors (an empty list means its valid)
-
validateInstance
public List<String> validateInstance(com.fasterxml.jackson.databind.JsonNode data, com.networknt.schema.JsonSchema compiledInstanceSchema) Validate an instance JsonNode against a previously compiled instance schema.- Parameters:
data- the JSON data as a JsonNodecompiledInstanceSchema- the already compiled schema to evaluate against- Returns:
- a list of validation errors (an empty list means its valid)
-
getCompiledSchemaFromDefinition
public com.networknt.schema.JsonSchema getCompiledSchemaFromDefinition(org.jooq.JSONB typeDefinition) Build or reuse a compiled schema for an entity type (build from its definition or get from LRU-cached). Convenience method that allows using the JSONB directly.- Parameters:
typeDefinition- the type definition- Returns:
- the compiled schema
-
getCompiledSchemaFromDefinition
public com.networknt.schema.JsonSchema getCompiledSchemaFromDefinition(com.fasterxml.jackson.databind.JsonNode typeDefinition) Build or reuse a compiled schema for an entity type (build from its definition or get from LRU-cached).- Parameters:
typeDefinition- the type definition- Returns:
- the compiled schema
-