152 lines
3.9 KiB
Go
152 lines
3.9 KiB
Go
package validate
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
|
|
"github.com/santhosh-tekuri/jsonschema/v6"
|
|
)
|
|
|
|
// StandardValidator provides basic, JSON, and JSON Schema output validation.
|
|
type StandardValidator struct {
|
|
schemaBaseDir string
|
|
}
|
|
|
|
func NewStandardValidator(schemaBaseDir string) Validator {
|
|
return &StandardValidator{schemaBaseDir: schemaBaseDir}
|
|
}
|
|
|
|
func (v *StandardValidator) Validate(ctx context.Context, artifact *domain.Artifact, contract domain.OutputContract) (domain.ValidationResult, error) {
|
|
select {
|
|
case <-ctx.Done():
|
|
return domain.ValidationResult{}, ctx.Err()
|
|
default:
|
|
}
|
|
|
|
res := domain.ValidationResult{
|
|
Mode: contract.ValidationMode,
|
|
SchemaPath: contract.SchemaPath,
|
|
RepairAttempts: contract.RepairAttempts,
|
|
}
|
|
|
|
if artifact == nil {
|
|
return domain.ValidationResult{}, errors.New("artifact is required for validation")
|
|
}
|
|
|
|
switch contract.ValidationMode {
|
|
case domain.ValidationNone:
|
|
res.Status = domain.ValidationSkipped
|
|
res.IsValid = true
|
|
return res, nil
|
|
case domain.ValidationBasic:
|
|
if strings.TrimSpace(string(artifact.Body)) == "" {
|
|
res.Status = domain.ValidationFailed
|
|
res.IsValid = false
|
|
res.Errors = []string{"output is empty"}
|
|
return res, nil
|
|
}
|
|
res.Status = domain.ValidationPassed
|
|
res.IsValid = true
|
|
return res, nil
|
|
case domain.ValidationJSON:
|
|
_, jsonErr := parseJSON(artifact.Body)
|
|
if jsonErr != nil {
|
|
res.Status = domain.ValidationFailed
|
|
res.IsValid = false
|
|
res.Errors = []string{fmt.Sprintf("invalid JSON: %v", jsonErr)}
|
|
return res, nil
|
|
}
|
|
res.Status = domain.ValidationPassed
|
|
res.IsValid = true
|
|
return res, nil
|
|
case domain.ValidationJSONSchema:
|
|
instance, jsonErr := parseJSON(artifact.Body)
|
|
if jsonErr != nil {
|
|
res.Status = domain.ValidationFailed
|
|
res.IsValid = false
|
|
res.Errors = []string{fmt.Sprintf("invalid JSON: %v", jsonErr)}
|
|
return res, nil
|
|
}
|
|
|
|
schemaPath, err := v.resolveSchemaPath(contract.SchemaPath)
|
|
if err != nil {
|
|
return domain.ValidationResult{}, err
|
|
}
|
|
|
|
compiler := jsonschema.NewCompiler()
|
|
schema, err := compiler.Compile(schemaPath)
|
|
if err != nil {
|
|
return domain.ValidationResult{}, fmt.Errorf("failed to compile JSON schema %q: %w", schemaPath, err)
|
|
}
|
|
|
|
if err := schema.Validate(instance); err != nil {
|
|
res.Status = domain.ValidationFailed
|
|
res.IsValid = false
|
|
res.Errors = []string{fmt.Sprintf("json schema validation failed: %v", err)}
|
|
return res, nil
|
|
}
|
|
|
|
res.Status = domain.ValidationPassed
|
|
res.IsValid = true
|
|
return res, nil
|
|
default:
|
|
return domain.ValidationResult{}, fmt.Errorf("unsupported validation mode: %q", contract.ValidationMode)
|
|
}
|
|
}
|
|
|
|
func parseJSON(body []byte) (any, error) {
|
|
var v any
|
|
if err := json.Unmarshal(body, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
func (v *StandardValidator) LoadSchemaDocument(ctx context.Context, schemaPath string) (any, error) {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
default:
|
|
}
|
|
|
|
resolved, err := v.resolveSchemaPath(schemaPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
raw, err := os.ReadFile(resolved)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read schema file %q: %w", resolved, err)
|
|
}
|
|
|
|
var doc any
|
|
if err := json.Unmarshal(raw, &doc); err != nil {
|
|
return nil, fmt.Errorf("failed to decode JSON schema %q: %w", resolved, err)
|
|
}
|
|
return doc, nil
|
|
}
|
|
|
|
func (v *StandardValidator) resolveSchemaPath(schemaPath string) (string, error) {
|
|
if strings.TrimSpace(schemaPath) == "" {
|
|
return "", errors.New("schema path is required for json_schema validation")
|
|
}
|
|
|
|
resolved := schemaPath
|
|
if !filepath.IsAbs(schemaPath) {
|
|
resolved = filepath.Join(v.schemaBaseDir, schemaPath)
|
|
}
|
|
|
|
resolved = filepath.Clean(resolved)
|
|
if _, err := os.Stat(resolved); err != nil {
|
|
return "", fmt.Errorf("failed to access schema file %q: %w", resolved, err)
|
|
}
|
|
|
|
return resolved, nil
|
|
}
|