Class YmlSchema

java.lang.Object
cloud.opencode.base.yml.schema.YmlSchema

public final class YmlSchema extends Object
YmlSchema - Lightweight structural validator for YAML documents YmlSchema - 轻量级 YAML 文档结构验证器

Provides a builder-based API for defining validation rules on YAML data, including required keys, type constraints, value ranges, regex patterns, nested schemas, and custom predicate rules.

提供基于构建器的 API 来定义 YAML 数据的验证规则, 包括必需键、类型约束、值范围、正则模式、嵌套模式和自定义谓词规则。

Features | 主要功能:

  • Required key validation at root level - 根级别必需键验证
  • Type constraint validation via path - 通过路径的类型约束验证
  • Value range validation for Comparable types - Comparable 类型的值范围验证
  • Regex pattern validation for string values - 字符串值的正则模式验证
  • Nested schema validation for sub-documents - 子文档的嵌套模式验证
  • Custom predicate-based validation rules - 基于自定义谓词的验证规则

Usage Examples | 使用示例:

YmlSchema schema = YmlSchema.builder()
    .required("name", "version")
    .type("name", String.class)
    .type("port", Integer.class)
    .range("port", 1, 65535)
    .pattern("email", "^[\\w@.]+$")
    .nested("database", YmlSchema.builder()
        .required("url")
        .build())
    .rule("name", v -> ((String) v).length() <= 100, "Name too long")
    .build();

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

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构建后不可变)
Since:
JDK 25, opencode-base-yml V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • builder

      public static YmlSchema.Builder builder()
      Creates a new schema builder. 创建新的模式构建器。
      Returns:
      a new builder | 新的构建器
    • validate

      public ValidationResult validate(Map<String,Object> data)
      Validates a Map against this schema. 根据此模式验证 Map。
      Parameters:
      data - the data to validate | 要验证的数据
      Returns:
      the validation result | 验证结果
    • validate

      public ValidationResult validate(YmlDocument doc)
      Validates a YmlDocument against this schema. 根据此模式验证 YmlDocument。
      Parameters:
      doc - the document to validate | 要验证的文档
      Returns:
      the validation result | 验证结果