Record Class ValidationResult

java.lang.Object
java.lang.Record
cloud.opencode.base.yml.schema.ValidationResult

public record ValidationResult(boolean valid, List<ValidationError> errors) extends Record
ValidationResult - Result of YAML schema validation ValidationResult - YAML 模式验证的结果

An immutable record representing the outcome of validating YAML data against a schema. Contains a validity flag and an immutable list of errors (empty when valid).

一个不可变记录,表示根据模式验证 YAML 数据的结果。 包含有效性标志和不可变的错误列表(有效时为空)。

Features | 主要功能:

  • Factory methods for success and failure - 成功和失败的工厂方法
  • Immutable error list - 不可变的错误列表
  • Convenience isValid() check - 便捷的 isValid() 检查

Usage Examples | 使用示例:

// Success
ValidationResult ok = ValidationResult.success();
assert ok.isValid();

// Failure
ValidationResult fail = ValidationResult.failure(List.of(
    new ValidationError("port", "Required key missing", ErrorType.MISSING_REQUIRED)
));
assert !fail.isValid();
assert fail.getErrors().size() == 1;

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
Since:
JDK 25, opencode-base-yml V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ValidationResult

      public ValidationResult(boolean valid, List<ValidationError> errors)
      Canonical constructor that ensures the error list is immutable. 规范构造函数,确保错误列表不可变。
      Parameters:
      valid - whether validation passed | 验证是否通过
      errors - the list of validation errors | 验证错误列表
  • Method Details

    • success

      public static ValidationResult success()
      Creates a successful validation result with no errors. 创建没有错误的成功验证结果。
      Returns:
      a successful result | 成功结果
    • failure

      public static ValidationResult failure(List<ValidationError> errors)
      Creates a failure validation result with the given errors. 创建带有给定错误的失败验证结果。
      Parameters:
      errors - the validation errors | 验证错误
      Returns:
      a failure result | 失败结果
    • isValid

      public boolean isValid()
      Returns whether the validation passed. 返回验证是否通过。
      Returns:
      true if valid | 如果有效则返回 true
    • getErrors

      public List<ValidationError> getErrors()
      Returns the list of validation errors. 返回验证错误列表。
      Returns:
      immutable list of errors | 不可变的错误列表
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • valid

      public boolean valid()
      Returns the value of the valid record component.
      Returns:
      the value of the valid record component
    • errors

      public List<ValidationError> errors()
      Returns the value of the errors record component.
      Returns:
      the value of the errors record component