53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
|
|
package contracts
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
"unicode/utf8"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
MaxValidationReasonCodeBytes = 128
|
||
|
|
MaxValidationCorrectionGuidanceBytes = 4 * 1024
|
||
|
|
)
|
||
|
|
|
||
|
|
func ValidateValidationResult(result ValidationResult) error {
|
||
|
|
if err := ValidateProducerDiagnostics(result.Diagnostics); err != nil {
|
||
|
|
return fmt.Errorf("validation diagnostics: %w", err)
|
||
|
|
}
|
||
|
|
if !result.Approved && result.ReasonCode == "" {
|
||
|
|
return errors.New("validation rejection reason code must not be empty")
|
||
|
|
}
|
||
|
|
if result.ReasonCode != "" {
|
||
|
|
if err := validateBoundedText(result.ReasonCode, MaxValidationReasonCodeBytes, "validation reason code", false); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !result.Approved && result.CorrectionGuidance == "" {
|
||
|
|
return errors.New("validation rejection correction guidance must not be empty")
|
||
|
|
}
|
||
|
|
if result.CorrectionGuidance != "" {
|
||
|
|
if err := validateBoundedText(result.CorrectionGuidance, MaxValidationCorrectionGuidanceBytes, "validation correction guidance", false); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func validateBoundedText(value string, maximum int, name string, optional bool) error {
|
||
|
|
if value == "" && optional {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if !utf8.ValidString(value) {
|
||
|
|
return fmt.Errorf("%s must be valid UTF-8", name)
|
||
|
|
}
|
||
|
|
if strings.TrimSpace(value) == "" {
|
||
|
|
return fmt.Errorf("%s must not be blank", name)
|
||
|
|
}
|
||
|
|
if len(value) > maximum {
|
||
|
|
return fmt.Errorf("%s exceeds maximum length", name)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|