Class JsonSchemaValidator

java.lang.Object
org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString
org.mockserver.validator.jsonschema.JsonSchemaValidator
All Implemented Interfaces:
Validator<String>
Direct Known Subclasses:
JsonSchemaExpectationIdValidator, JsonSchemaExpectationValidator, JsonSchemaHttpRequestAndHttpResponseValidator, JsonSchemaHttpRequestValidator, JsonSchemaHttpResponseValidator, JsonSchemaOpenAPIExpectationValidator, JsonSchemaRequestDefinitionValidator, JsonSchemaVerificationSequenceValidator, JsonSchemaVerificationValidator

public class JsonSchemaValidator extends ObjectWithReflectiveEqualsHashCodeToString implements Validator<String>
Author:
jamesdbloom
  • Field Details

    • OPEN_API_SPECIFICATION_URL

      public static final String OPEN_API_SPECIFICATION_URL
    • JSON_SCHEMA_ALLOW_REMOTE_REFS_PROPERTY

      public static final String JSON_SCHEMA_ALLOW_REMOTE_REFS_PROPERTY
      System property opt-in to allow remote $ref resolution in JSON schemas.

      SECURITY: by default MockServer blocks resolution of remote $refs (http, https, ftp and external file URIs) when matching a request/response body against a JSON Schema, to avoid an SSRF / unexpected-network-fetch risk where a schema could cause the server to fetch arbitrary external resources. Schemas that use only internal references (#/...) and inline definitions are unaffected.

      Set -Dmockserver.jsonSchemaAllowRemoteRefs=true to opt in to remote $ref resolution for the rare case where it is genuinely required.

      See Also:
  • Constructor Details

    • JsonSchemaValidator

      public JsonSchemaValidator(MockServerLogger mockServerLogger, String schema)
    • JsonSchemaValidator

      public JsonSchemaValidator(MockServerLogger mockServerLogger, String schema, com.fasterxml.jackson.databind.JsonNode schemaJsonNode)
    • JsonSchemaValidator

      public JsonSchemaValidator(MockServerLogger mockServerLogger, Class<?> type, String routePath, String mainSchemeFile, String... referenceFiles)
  • Method Details

    • cachedJsonSchemaValidator

      public static JsonSchemaValidator cachedJsonSchemaValidator(MockServerLogger mockServerLogger, String schemaJson)
      Returns a JsonSchemaValidator for schemaJson that reuses a cached one-time compilation of the schema when the same content has been seen before. The returned validator is behaviour-identical to new JsonSchemaValidator(logger, schemaJson) for the same input — same validation outcomes and error messages, the caller's own logger, and an independent per-call instance — only the expensive Schema/SchemaRegistry compilation is elided on a cache hit. Intended for the per-request OpenAPI validation path.
    • getSchema

      public String getSchema()
    • isValid

      public String isValid(String json)
      Specified by:
      isValid in interface Validator<String>
    • isValid

      public String isValid(String json, boolean addOpenAPISpecificationMessage)