Class JsonSchemaValidator

java.lang.Object
cloud.opencode.base.json.schema.JsonSchemaValidator

public final class JsonSchemaValidator extends Object
JSON Schema Validator - JSON Schema Draft 2020-12 Implementation JSON Schema 验证器 - JSON Schema Draft 2020-12 实现

This class validates JSON documents against JSON Schema specifications. It supports a subset of JSON Schema Draft 2020-12 keywords.

此类根据 JSON Schema 规范验证 JSON 文档。 它支持 JSON Schema Draft 2020-12 关键字的子集。

Supported Keywords | 支持的关键字:

  • Type: type, enum, const
  • String: minLength, maxLength, pattern, format
  • Number: minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf
  • Array: minItems, maxItems, uniqueItems, items
  • Object: properties, required, additionalProperties, minProperties, maxProperties
  • Composition: allOf, anyOf, oneOf, not

Example | 示例:

JsonNode schema = OpenJson.parse("""
    {
        "type": "object",
        "properties": {
            "name": {"type": "string", "minLength": 1},
            "age": {"type": "integer", "minimum": 0}
        },
        "required": ["name"]
    }
    """);

JsonNode data = OpenJson.parse("{\"name\":\"John\",\"age\":30}");

ValidationResult result = JsonSchemaValidator.validate(data, schema);
if (!result.isValid()) {
    result.getErrors().forEach(System.out::println);
}

Features | 主要功能:

  • JSON Schema Draft 2020-12 keyword validation - JSON Schema Draft 2020-12关键字验证
  • Type, string, number, array, object, and composition validation - 类型、字符串、数字、数组、对象和组合验证
  • Cached regex pattern compilation for performance - 缓存的正则模式编译提升性能

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:
  • Method Details

    • of

      public static JsonSchemaValidator of(JsonNode schema)
      Creates a validator for the given schema. 为给定 schema 创建验证器。
      Parameters:
      schema - the JSON Schema - JSON Schema
      Returns:
      the validator - 验证器
    • validate

      public static JsonSchemaValidator.ValidationResult validate(JsonNode data, JsonNode schema)
      Validates data against a schema. 根据 schema 验证数据。
      Parameters:
      data - the data to validate - 要验证的数据
      schema - the JSON Schema - JSON Schema
      Returns:
      the validation result - 验证结果
    • validateOrThrow

      public static void validateOrThrow(JsonNode data, JsonNode schema)
      Validates data and throws if invalid. 验证数据,如果无效则抛出异常。
      Parameters:
      data - the data to validate - 要验证的数据
      schema - the JSON Schema - JSON Schema
      Throws:
      JsonSchemaException - if validation fails - 如果验证失败
    • validate

      Validates data against this validator's schema. 根据此验证器的 schema 验证数据。
      Parameters:
      data - the data to validate - 要验证的数据
      Returns:
      the validation result - 验证结果
    • validateOrThrow

      public void validateOrThrow(JsonNode data)
      Validates and throws if invalid. 验证,如果无效则抛出异常。
      Parameters:
      data - the data to validate - 要验证的数据
      Throws:
      JsonSchemaException - if validation fails - 如果验证失败