package ledger

// ValidationIssue describes a single validation problem.
type ValidationIssue struct {
	Field string `json:"field"`
	Code  string `json:"code"`
	Msg   string `json:"msg"`
}

// ValidationError aggregates issues. Implements error without fmt/errors.
type ValidationError struct {
	Issues []ValidationIssue `json:"issues"`
}

func (e *ValidationError) Error() string {
	if e == nil || len(e.Issues) == 0 {
		return ""
	}
	if len(e.Issues) == 1 {
		return e.Issues[0].Field + ": " + e.Issues[0].Msg
	}
	return "validation failed"
}

// veAdd appends a new issue into a (possibly nil) *ValidationError.
func veAdd(e **ValidationError, field, code, msg string) {
	if *e == nil {
		*e = &ValidationError{Issues: make([]ValidationIssue, 0, 4)}
	}
	(*e).Issues = append((*e).Issues, ValidationIssue{Field: field, Code: code, Msg: msg})
}
