Class CsvValidator

java.lang.Object
cloud.opencode.base.csv.validator.CsvValidator

public final class CsvValidator extends Object
CSV Validator - Declarative validation framework for CSV data CSV验证器 - CSV数据的声明式验证框架

Provides a fluent builder API for defining validation rules on CSV columns. Rules are evaluated against all rows in a CsvDocument, collecting every error (not fail-fast). Supports common constraints like notBlank, numeric range, regex pattern, length bounds, allowed values, and custom predicates.

提供用于定义CSV列验证规则的流式构建器API。 规则针对 CsvDocument 中的所有行进行评估,收集所有错误(非快速失败)。 支持常见约束,如非空、数字范围、正则模式、长度限制、允许值和自定义谓词。

Features | 主要功能:

  • Builder pattern for rule definition - Builder模式定义规则
  • Collects all errors (not fail-fast) - 收集所有错误(非快速失败)
  • Built-in rules: notBlank, range, pattern, minLength, maxLength, oneOf - 内置规则
  • Custom predicate rules - 自定义谓词规则
  • Column existence validation at validate time - 验证时检查列存在性

Usage Examples | 使用示例:

CsvValidator validator = CsvValidator.builder()
    .notBlank("name")
    .range("age", 0, 150)
    .pattern("email", "^[\\w@.]+$")
    .oneOf("status", "active", "inactive")
    .build();

CsvValidationResult result = validator.validate(doc);
if (!result.valid()) {
    result.errors().forEach(System.out::println);
}

Security | 安全性:

  • Thread-safe: Yes (immutable after build) - 线程安全: 是(构建后不可变)
  • Regex compiled once at build time - 正则在构建时编译一次
Since:
JDK 25, opencode-base-csv V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • builder

      public static CsvValidator.Builder builder()
      Creates a new validator builder 创建新的验证器构建器
      Returns:
      a new Builder | 新的Builder
    • validate

      public CsvValidationResult validate(CsvDocument doc)
      Validates the given CSV document against all configured rules 使用所有已配置规则验证给定的CSV文档

      Iterates all rows and all rules, collecting every error. Column existence is validated at this point.

      遍历所有行和所有规则,收集所有错误。此时验证列的存在性。

      Parameters:
      doc - the CSV document to validate | 要验证的CSV文档
      Returns:
      the validation result | 验证结果
      Throws:
      NullPointerException - if doc is null | 如果doc为null