100 lines
2.1 KiB
Go
100 lines
2.1 KiB
Go
package excel
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func ValidateRequiredFields(rows []ParsedRow, specs []ColumnSpec) []RowIssue {
|
|
issues := make([]RowIssue, 0)
|
|
for _, row := range rows {
|
|
for _, spec := range specs {
|
|
if !spec.Required {
|
|
continue
|
|
}
|
|
value := Field(row, spec.Name)
|
|
if value != "" {
|
|
continue
|
|
}
|
|
issues = append(issues, RowIssue{
|
|
RowNo: row.RowNo,
|
|
Column: spec.Name,
|
|
Value: value,
|
|
Message: fmt.Sprintf("%s is required", spec.Name),
|
|
})
|
|
}
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func ValidateRowTypes(rows []ParsedRow, specs []ColumnSpec) []RowIssue {
|
|
issues := make([]RowIssue, 0)
|
|
for _, row := range rows {
|
|
for _, spec := range specs {
|
|
value := Field(row, spec.Name)
|
|
if value == "" {
|
|
continue
|
|
}
|
|
if _, err := normalizedValue(value, spec.Type); err != nil {
|
|
issues = append(issues, RowIssue{
|
|
RowNo: row.RowNo,
|
|
Column: spec.Name,
|
|
Value: value,
|
|
Message: fmt.Sprintf("%s format is invalid", spec.Name),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func ValidateFileConsistency(rows []ParsedRow, rules []ConsistencyRule) []RowIssue {
|
|
issues := make([]RowIssue, 0)
|
|
for _, rule := range rules {
|
|
baseValue := ""
|
|
baseSet := false
|
|
for _, row := range rows {
|
|
raw := Field(row, rule.Name)
|
|
normalized := ""
|
|
if strings.TrimSpace(raw) == "" {
|
|
if !rule.AllowEmpty {
|
|
issues = append(issues, RowIssue{
|
|
RowNo: row.RowNo,
|
|
Column: rule.Name,
|
|
Value: raw,
|
|
Message: fmt.Sprintf("%s must not be empty", rule.Name),
|
|
})
|
|
continue
|
|
}
|
|
} else {
|
|
value, err := normalizedValue(raw, rule.Type)
|
|
if err != nil {
|
|
issues = append(issues, RowIssue{
|
|
RowNo: row.RowNo,
|
|
Column: rule.Name,
|
|
Value: raw,
|
|
Message: fmt.Sprintf("%s format is invalid", rule.Name),
|
|
})
|
|
continue
|
|
}
|
|
normalized = value
|
|
}
|
|
|
|
if !baseSet {
|
|
baseValue = normalized
|
|
baseSet = true
|
|
continue
|
|
}
|
|
if normalized != baseValue {
|
|
issues = append(issues, RowIssue{
|
|
RowNo: row.RowNo,
|
|
Column: rule.Name,
|
|
Value: raw,
|
|
Message: fmt.Sprintf("%s must be consistent", rule.Name),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
return issues
|
|
}
|