Class YmlSchema
java.lang.Object
cloud.opencode.base.yml.schema.YmlSchema
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for constructing YmlSchema instances. -
Method Summary
Modifier and TypeMethodDescriptionstatic YmlSchema.Builderbuilder()Creates a new schema builder.validate(YmlDocument doc) Validates a YmlDocument against this schema.Validates a Map against this schema.
-
Method Details
-
builder
Creates a new schema builder. 创建新的模式构建器。- Returns:
- a new builder | 新的构建器
-
validate
Validates a Map against this schema. 根据此模式验证 Map。- Parameters:
data- the data to validate | 要验证的数据- Returns:
- the validation result | 验证结果
-
validate
Validates a YmlDocument against this schema. 根据此模式验证 YmlDocument。- Parameters:
doc- the document to validate | 要验证的文档- Returns:
- the validation result | 验证结果
-