Class JsonSchemaException

All Implemented Interfaces:
Serializable

public class JsonSchemaException extends OpenJsonProcessingException
JSON Schema Exception - Exception for Schema Validation Failures JSON Schema 异常 - Schema 验证失败异常

This exception is thrown when JSON Schema validation fails. It contains detailed information about all validation errors encountered.

当 JSON Schema 验证失败时抛出此异常。它包含遇到的所有验证错误的详细信息。

Example | 示例:

try {
    JsonSchemaValidator.validate(json, schema);
} catch (JsonSchemaException e) {
    for (ValidationError error : e.getErrors()) {
        System.err.println(error.getPath() + ": " + error.getMessage());
    }
}

Features | 主要功能:

  • Detailed validation error information - 详细的验证错误信息
  • Multiple error collection and reporting - 多错误收集和报告
  • Builder pattern for constructing complex validation exceptions - 构建复杂验证异常的构建器模式

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
Since:
JDK 25, opencode-base-json V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • JsonSchemaException

      public JsonSchemaException(String message)
      Constructs a new exception with a single error message. 使用单个错误消息构造新异常。
      Parameters:
      message - the detail message - 详细消息
    • JsonSchemaException

      public JsonSchemaException(String message, List<JsonSchemaException.ValidationError> errors)
      Constructs a new exception with validation errors. 使用验证错误列表构造新异常。
      Parameters:
      message - the detail message - 详细消息
      errors - the validation errors - 验证错误列表
    • JsonSchemaException

      public JsonSchemaException(String message, List<JsonSchemaException.ValidationError> errors, String schemaUri)
      Constructs a new exception with validation errors and schema URI. 使用验证错误列表和 Schema URI 构造新异常。
      Parameters:
      message - the detail message - 详细消息
      errors - the validation errors - 验证错误列表
      schemaUri - the schema URI - Schema URI
  • Method Details

    • getErrors

      Returns the list of validation errors. 返回验证错误列表。
      Returns:
      unmodifiable list of validation errors - 不可变的验证错误列表
    • getErrorCount

      public int getErrorCount()
      Returns the number of validation errors. 返回验证错误数量。
      Returns:
      the error count - 错误数量
    • getSchemaUri

      public String getSchemaUri()
      Returns the schema URI that was validated against. 返回验证所使用的 Schema URI。
      Returns:
      the schema URI, or null if not specified - Schema URI,未指定时返回 null
    • hasErrors

      public boolean hasErrors()
      Returns whether there are any errors. 返回是否存在错误。
      Returns:
      true if there are errors - 如果存在错误则返回 true
    • getErrorReport

      public String getErrorReport()
      Returns a formatted error report. 返回格式化的错误报告。
      Returns:
      the error report - 错误报告
    • getMessage

      public String getMessage()
      Overrides:
      getMessage in class OpenJsonProcessingException
    • typeMismatch

      public static JsonSchemaException typeMismatch(String path, String expected, String actual)
      Creates an exception for a type mismatch error. 创建类型不匹配错误的异常。
      Parameters:
      path - the JSON path - JSON 路径
      expected - the expected type - 预期类型
      actual - the actual type - 实际类型
      Returns:
      the exception - 异常
    • missingRequired

      public static JsonSchemaException missingRequired(String path, String property)
      Creates an exception for a required property error. 创建必需属性错误的异常。
      Parameters:
      path - the JSON path - JSON 路径
      property - the missing property name - 缺失的属性名
      Returns:
      the exception - 异常
    • patternMismatch

      public static JsonSchemaException patternMismatch(String path, String pattern, String value)
      Creates an exception for a pattern mismatch error. 创建模式不匹配错误的异常。
      Parameters:
      path - the JSON path - JSON 路径
      pattern - the expected pattern - 预期模式
      value - the actual value - 实际值
      Returns:
      the exception - 异常
    • constraintViolation

      public static JsonSchemaException constraintViolation(String path, String constraint, String message)
      Creates an exception for a constraint violation error. 创建约束违反错误的异常。
      Parameters:
      path - the JSON path - JSON 路径
      constraint - the constraint keyword - 约束关键字
      message - the error message - 错误消息
      Returns:
      the exception - 异常
    • builder

      public static JsonSchemaException.Builder builder()
      Creates a new builder. 创建新的构建器。
      Returns:
      a new builder - 新构建器