Unify inline and bound schema preparation

This commit is contained in:
2026-11-20 15:31:30 +00:00
parent be5a508554
commit 2d96dfa696
7 changed files with 223 additions and 389 deletions

View File

@@ -237,6 +237,8 @@ snapshots agree without eager schema I/O or a second composition pass.
## Stage 4 — Unify Inline And File Schema Preparation
Status: complete.
Work in `internal/validate/validator.go` and the current validator implementation.
1. Adapt the preparer to `validate.Input`. Replace duplicated engine-root-specific

View File

@@ -19,14 +19,14 @@ func TestValidationCancellationBeforeWorkDoesNotOpenSchemaSource(t *testing.T) {
source := &countingSchemaFS{FS: fstest.MapFS{
"schema.json": {Data: []byte(`{"type":"object"}`)},
}}
validator := NewFSValidator(source, ".")
validator := NewValidator()
ctx, cancel := context.WithCancel(context.Background())
cancel()
plan, err := validator.PrepareValidation(ctx, domain.OutputContract{
plan, err := validator.PrepareValidation(ctx, Input{Contract: domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "schema.json",
})
}, File: &domain.ResourceBinding{FS: source, Path: "schema.json"}})
if plan != nil || !errors.Is(err, context.Canceled) {
t.Fatalf("PrepareValidation() = (%v, %v), want nil plan and context cancellation", plan, err)
}
@@ -44,20 +44,20 @@ func TestValidationCancellationBetweenRootSchemaReadChunks(t *testing.T) {
target: "root.json",
cancel: cancel,
}
validator := NewFSValidator(source, ".")
validator := NewValidator()
plan, err := validator.PrepareValidation(ctx, domain.OutputContract{
plan, err := validator.PrepareValidation(ctx, Input{Contract: domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "root.json",
})
}, File: &domain.ResourceBinding{FS: source, Path: "root.json"}})
if plan != nil || !errors.Is(err, context.Canceled) {
t.Fatalf("PrepareValidation() = (%v, %v), want nil plan and context cancellation", plan, err)
}
if reads := source.reads.Load(); reads != 1 {
t.Fatalf("controlled child reads = %d, want 1", reads)
}
if closes := source.closes.Load(); closes != 2 {
t.Fatalf("controlled root closes = %d, want stat and read handles closed", closes)
if closes := source.closes.Load(); closes != 1 {
t.Fatalf("controlled root closes = %d, want read handle closed", closes)
}
}
@@ -112,17 +112,17 @@ func TestCancellationDoesNotDetachBlockedSchemaRead(t *testing.T) {
started: started,
release: release,
}
validator := NewFSValidator(source, ".")
validator := NewValidator()
type outcome struct {
plan PreparedValidation
err error
}
result := make(chan outcome, 1)
go func() {
plan, err := validator.PrepareValidation(ctx, domain.OutputContract{
plan, err := validator.PrepareValidation(ctx, Input{Contract: domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "schema.json",
})
}, File: &domain.ResourceBinding{FS: source, Path: "schema.json"}})
result <- outcome{plan: plan, err: err}
}()

View File

@@ -0,0 +1,103 @@
package validate
import (
"context"
"encoding/json"
"errors"
"reflect"
"testing"
"testing/fstest"
"go.maximumdirect.net/promptkit/internal/domain"
)
func TestPrepareInlineAndBoundDocuments(t *testing.T) {
for _, tc := range []struct {
name string
document any
file string
instance string
status domain.ValidationStatus
}{
{"object", map[string]any{"type": "object", "required": []any{"x"}}, `{"type":"object","required":["x"]}`, `{"x":1}`, domain.ValidationPassed},
{"reject object", map[string]any{"type": "object"}, `{"type":"object"}`, `1`, domain.ValidationFailed},
{"true", true, `true`, `null`, domain.ValidationPassed},
{"false", false, `false`, `null`, domain.ValidationFailed},
} {
t.Run(tc.name, func(t *testing.T) {
validator := NewValidator()
contract := domain.OutputContract{ValidationMode: domain.ValidationJSONSchema, Schema: tc.document}
inline, err := validator.PrepareValidation(context.Background(), Input{Contract: contract})
if err != nil {
t.Fatal(err)
}
contract.Schema = nil
file := &domain.ResourceBinding{FS: fstest.MapFS{"schema.json": {Data: []byte(tc.file)}}, Path: "schema.json"}
bound, err := validator.PrepareValidation(context.Background(), Input{Contract: contract, File: file})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(inline.SchemaDocument(), bound.SchemaDocument()) {
t.Fatalf("documents differ: %#v %#v", inline.SchemaDocument(), bound.SchemaDocument())
}
for _, plan := range []PreparedValidation{inline, bound} {
result, err := plan.Validate(context.Background(), &domain.Artifact{Body: []byte(tc.instance)})
if err != nil || result.Status != tc.status {
t.Fatalf("result = %+v, %v", result, err)
}
}
})
}
}
func TestPrepareSchemaReferencesAndDialects(t *testing.T) {
for _, tc := range []struct {
name string
document any
valid bool
}{
{"internal ref", map[string]any{"$defs": map[string]any{"n": map[string]any{"type": "integer"}}, "$ref": "#/$defs/n"}, true},
{"external ref", map[string]any{"$ref": "https://example.invalid/schema"}, false},
{"invalid schema", map[string]any{"type": "invalid"}, false},
{"invalid dialect", map[string]any{"$schema": "https://invalid.example/schema"}, false},
} {
t.Run(tc.name, func(t *testing.T) {
_, err := NewValidator().PrepareValidation(context.Background(), Input{Contract: domain.OutputContract{ValidationMode: domain.ValidationJSONSchema, Schema: tc.document}})
if (err == nil) != tc.valid {
t.Fatalf("preparation error = %v", err)
}
})
}
}
func TestPrepareBoundSchemaPreservesNumbersAndCancellation(t *testing.T) {
file := &domain.ResourceBinding{FS: fstest.MapFS{"schema.json": {Data: []byte(`{"const":9007199254740993}`)}}, Path: "schema.json"}
input := Input{Contract: domain.OutputContract{ValidationMode: domain.ValidationJSONSchema}, File: file}
plan, err := NewValidator().PrepareValidation(context.Background(), input)
if err != nil {
t.Fatal(err)
}
if got := plan.SchemaDocument().(map[string]any)["const"]; got != json.Number("9007199254740993") {
t.Fatalf("number = %#v", got)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := NewValidator().PrepareValidation(ctx, input); !errors.Is(err, context.Canceled) {
t.Fatalf("cancellation = %v", err)
}
}
func TestExternalReferencesDoNotOpenAdditionalResources(t *testing.T) {
files := &countingSchemaFS{FS: fstest.MapFS{
"root.json": {Data: []byte(`{"$ref":"child.json"}`)},
"child.json": {Data: []byte(`{"type":"string"}`)},
}}
input := Input{Contract: domain.OutputContract{ValidationMode: domain.ValidationJSONSchema}, File: &domain.ResourceBinding{FS: files, Path: "root.json"}}
_, err := NewValidator().PrepareValidation(context.Background(), input)
if err == nil {
t.Fatal("external reference was accepted")
}
if opens := files.opens.Load(); opens != 1 {
t.Fatalf("schema file opens = %d, want one", opens)
}
}

View File

@@ -3,6 +3,7 @@ package validate
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
@@ -29,9 +30,9 @@ func TestSchemaPreparationDoesNotDependOnSourceName(t *testing.T) {
t.Fatal(err)
}
fsys := fstest.MapFS{"schema.json": &fstest.MapFile{Data: []byte(test.schema)}}
for i, validator := range []ValidationPreparer{NewStandardValidator(dir), NewFSValidator(fsys, ".")} {
for i, source := range []fs.FS{os.DirFS(dir), fsys} {
t.Run(fmt.Sprint(i), func(t *testing.T) {
_, err := validator.PrepareValidation(context.Background(), domain.OutputContract{Format: domain.FormatJSON, ValidationMode: domain.ValidationJSONSchema, SchemaPath: "schema.json"})
_, err := NewValidator().PrepareValidation(context.Background(), Input{Contract: domain.OutputContract{Format: domain.FormatJSON, ValidationMode: domain.ValidationJSONSchema, SchemaPath: "schema.json"}, File: &domain.ResourceBinding{FS: source, Path: "schema.json"}})
if test.rejected {
if err == nil || !strings.Contains(err.Error(), "source location") {
t.Fatalf("expected source-independent rejection, got %v", err)

View File

@@ -9,38 +9,21 @@ import (
"io"
"io/fs"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/santhosh-tekuri/jsonschema/v6"
"go.maximumdirect.net/promptkit/internal/domain"
"go.maximumdirect.net/promptkit/internal/filecatalog"
)
const jsonSchemaDraft2020 = "https://json-schema.org/draft/2020-12/schema"
const schemaReadChunkSize = 64 * 1024
// StandardValidator provides basic, JSON, and JSON Schema output validation.
type StandardValidator struct {
schemaBaseDir string
}
// Validator prepares source-neutral output validation plans.
type Validator struct{}
type FSValidator struct {
fsys fs.FS
root string
}
func NewStandardValidator(schemaBaseDir string) *StandardValidator {
return &StandardValidator{schemaBaseDir: schemaBaseDir}
}
func NewFSValidator(fsys fs.FS, root string) *FSValidator {
return &FSValidator{fsys: fsys, root: root}
}
func NewValidator() *Validator { return &Validator{} }
type preparedValidation struct {
contract domain.OutputContract
@@ -56,43 +39,43 @@ func (p *preparedValidation) SchemaDocument() any {
return p.schemaDocument
}
func (v *StandardValidator) PrepareValidation(ctx context.Context, contract domain.OutputContract) (PreparedValidation, error) {
func (v *Validator) PrepareValidation(ctx context.Context, input Input) (PreparedValidation, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
contract := input.Contract
prepared := &preparedValidation{contract: contract}
if contract.ValidationMode != domain.ValidationJSONSchema {
return prepared, nil
}
name, err := v.resolveSchemaPath(ctx, contract.SchemaPath)
if err != nil {
return nil, err
document := contract.Schema
name := "inline schema"
resourceURL := "promptkit-schema:///inline.json"
if input.File != nil {
if input.File.FS == nil {
return nil, errors.New("schema filesystem is nil")
}
name = input.File.Path
resourceURL = fsSchemaResourceURL(name).String()
raw, err := readSchemaFile(ctx, func() (fs.File, error) { return input.File.FS.Open(name) })
if err != nil {
return nil, fmt.Errorf("failed to read schema file %q: %w", name, err)
}
document, err = decodeJSONValue(ctx, raw)
if err != nil {
return nil, fmt.Errorf("failed to decode JSON schema %q: %w", name, err)
}
}
document, err := loadJSONSchemaFile(ctx, name)
if err != nil {
return nil, fmt.Errorf("failed to compile JSON schema %q: %w", name, err)
if document == nil && input.File == nil {
return nil, errors.New("JSON schema document is missing")
}
schema, err := compileSchemaDocument(ctx, fileSchemaResourceURL(name).String(), document)
if err != nil {
return nil, fmt.Errorf("failed to compile JSON schema %q: %w", name, err)
if err := validateSchemaDialect(document); err != nil {
if contextErr := ctx.Err(); contextErr != nil {
return nil, contextErr
}
return nil, fmt.Errorf("failed to decode JSON schema %q: %w", name, err)
}
prepared.schemaDocument, prepared.schema = document, schema
return prepared, nil
}
func (v *FSValidator) PrepareValidation(ctx context.Context, contract domain.OutputContract) (PreparedValidation, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
prepared := &preparedValidation{contract: contract}
if contract.ValidationMode != domain.ValidationJSONSchema {
return prepared, nil
}
name, document, err := v.loadSchemaDocument(ctx, contract.SchemaPath)
if err != nil {
return nil, err
}
schema, err := compileSchemaDocument(ctx, fsSchemaResourceURL(name).String(), document)
schema, err := compileSchemaDocument(ctx, resourceURL, document)
if err != nil {
return nil, fmt.Errorf("failed to compile JSON schema %q: %w", name, err)
}
@@ -185,7 +168,13 @@ func validateArtifact(ctx context.Context, artifact *domain.Artifact, contract d
// DecodeInlineSchema converts a caller-supplied value to an owned JSON document.
func DecodeInlineSchema(ctx context.Context, value any) (any, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
encoded, err := json.Marshal(value)
if contextErr := ctx.Err(); contextErr != nil {
return nil, contextErr
}
if err != nil {
return nil, err
}
@@ -221,159 +210,6 @@ func decodeJSONValue(ctx context.Context, body []byte) (any, error) {
return nil, errors.New("multiple JSON values")
}
func (v *StandardValidator) resolveSchemaPath(ctx context.Context, schemaPath string) (string, error) {
if err := ctx.Err(); err != nil {
return "", err
}
if strings.TrimSpace(schemaPath) == "" {
return "", errors.New("schema path is required for json_schema validation")
}
root, err := v.schemaRoot()
if err != nil {
return "", err
}
if err := ctx.Err(); err != nil {
return "", err
}
resolved, err := containedFilesystemPath(root, schemaPath)
if err != nil {
return "", err
}
if err := ctx.Err(); err != nil {
return "", err
}
if _, err := os.Stat(resolved); err != nil {
if contextErr := ctx.Err(); contextErr != nil {
return "", contextErr
}
return "", fmt.Errorf("failed to access schema file %q: %w", resolved, err)
}
if err := ctx.Err(); err != nil {
return "", err
}
return resolved, nil
}
func (v *StandardValidator) schemaRoot() (string, error) {
root := v.schemaBaseDir
if strings.TrimSpace(root) == "" {
root = "."
}
absolute, err := filepath.Abs(root)
if err != nil {
return "", fmt.Errorf("failed to resolve schema source %q: %w", root, err)
}
resolved, err := filepath.EvalSymlinks(absolute)
if err != nil {
return "", fmt.Errorf("failed to access schema source %q: %w", root, err)
}
return resolved, nil
}
func (v *FSValidator) loadSchemaDocument(ctx context.Context, schemaPath string) (string, any, error) {
resolved, err := v.resolveSchemaPath(ctx, schemaPath)
if err != nil {
return "", nil, err
}
raw, err := readSchemaFile(ctx, func() (fs.File, error) {
return v.fsys.Open(resolved)
})
if err != nil {
return "", nil, fmt.Errorf("failed to read schema file %q: %w", resolved, err)
}
doc, err := decodeJSONValue(ctx, raw)
if err != nil {
return "", nil, fmt.Errorf("failed to decode JSON schema %q: %w", resolved, err)
}
if err := validateSchemaDialect(doc); err != nil {
if contextErr := ctx.Err(); contextErr != nil {
return "", nil, contextErr
}
return "", nil, fmt.Errorf("failed to decode JSON schema %q: %w", resolved, err)
}
if err := ctx.Err(); err != nil {
return "", nil, err
}
return resolved, doc, nil
}
func (v *FSValidator) resolveSchemaPath(ctx context.Context, schemaPath string) (string, error) {
if err := ctx.Err(); err != nil {
return "", err
}
if strings.TrimSpace(schemaPath) == "" {
return "", errors.New("schema path is required for json_schema validation")
}
if v.fsys == nil {
return "", errors.New("schema filesystem is nil")
}
cleanRoot := filecatalog.CleanFSRoot(v.root)
rootInfo, err := fs.Stat(v.fsys, cleanRoot)
if err != nil {
if contextErr := ctx.Err(); contextErr != nil {
return "", contextErr
}
return "", fmt.Errorf("failed to access schema source %q: %w", cleanRoot, err)
}
if err := ctx.Err(); err != nil {
return "", err
}
var resolved string
if rootInfo.IsDir() {
resolvedPath, _, err := filecatalog.ResolveFSPath(cleanRoot, cleanRoot, schemaPath)
if err != nil {
return "", err
}
resolved = resolvedPath
} else {
cleanSchemaPath, err := cleanSchemaFSPath(schemaPath)
if err != nil {
return "", err
}
if cleanSchemaPath != strings.TrimSpace(path.Base(cleanRoot)) {
return "", fmt.Errorf("schema path %q does not match schema file %q", cleanSchemaPath, path.Base(cleanRoot))
}
resolved = cleanRoot
}
if _, err := fs.Stat(v.fsys, resolved); err != nil {
if contextErr := ctx.Err(); contextErr != nil {
return "", contextErr
}
return "", fmt.Errorf("failed to access schema file %q: %w", resolved, err)
}
if err := ctx.Err(); err != nil {
return "", err
}
return resolved, nil
}
func cleanSchemaFSPath(schemaPath string) (string, error) {
cleaned := strings.TrimSpace(schemaPath)
if cleaned == "" {
return "", errors.New("schema path is required for json_schema validation")
}
cleaned = path.Clean(cleaned)
if path.IsAbs(cleaned) {
return "", fmt.Errorf("schema path %q must be relative", schemaPath)
}
return cleaned, nil
}
func fileSchemaResourceURL(schemaName string) *url.URL {
filePath := filepath.ToSlash(schemaName)
if runtime.GOOS == "windows" && !strings.HasPrefix(filePath, "/") {
filePath = "/" + filePath
}
return &url.URL{Scheme: "file", Path: filePath}
}
func fsSchemaResourceURL(schemaName string) *url.URL {
return &url.URL{
Scheme: "promptkit-schema",
@@ -475,52 +311,6 @@ func validateSchemaDialect(doc any) error {
return nil
}
func containedFilesystemPath(root, name string) (string, error) {
candidate := name
if !filepath.IsAbs(candidate) {
candidate = filepath.Join(root, candidate)
}
candidate, err := filepath.Abs(candidate)
if err != nil {
return "", fmt.Errorf("failed to resolve schema path %q: %w", name, err)
}
candidate, err = filepath.EvalSymlinks(candidate)
if err != nil {
return "", fmt.Errorf("failed to access schema file %q: %w", candidate, err)
}
relative, err := filepath.Rel(root, candidate)
if err != nil {
return "", fmt.Errorf("failed to compare schema path %q with source root: %w", candidate, err)
}
if relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
return "", fmt.Errorf("schema path %q escapes source root", name)
}
return candidate, nil
}
func loadJSONSchemaFile(ctx context.Context, name string) (any, error) {
raw, err := readSchemaFile(ctx, func() (fs.File, error) {
return os.Open(name)
})
if err != nil {
return nil, err
}
doc, err := decodeJSONValue(ctx, raw)
if err != nil {
return nil, err
}
if err := validateSchemaDialect(doc); err != nil {
if contextErr := ctx.Err(); contextErr != nil {
return nil, contextErr
}
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
return doc, nil
}
func readSchemaFile(ctx context.Context, open func() (fs.File, error)) ([]byte, error) {
if err := ctx.Err(); err != nil {
return nil, err

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"io/fs"
"net/url"
"os"
"path/filepath"
@@ -17,9 +18,32 @@ import (
"go.maximumdirect.net/promptkit/internal/domain"
)
func prepareForTest(t testing.TB, preparer ValidationPreparer, contract domain.OutputContract) PreparedValidation {
func schemaDirectoryFS(root string) fs.FS {
if root == "" {
root = "."
}
return os.DirFS(root)
}
func rootedSchemaFS(source fs.FS, root string) fs.FS {
sub, err := fs.Sub(source, root)
if err != nil {
panic(err)
}
return sub
}
func prepareSchema(ctx context.Context, source fs.FS, contract domain.OutputContract) (PreparedValidation, error) {
input := Input{Contract: contract}
if contract.ValidationMode == domain.ValidationJSONSchema {
input.File = &domain.ResourceBinding{FS: source, Path: filepath.ToSlash(contract.SchemaPath)}
}
return NewValidator().PrepareValidation(ctx, input)
}
func prepareForTest(t testing.TB, preparer fs.FS, contract domain.OutputContract) PreparedValidation {
t.Helper()
plan, err := preparer.PrepareValidation(context.Background(), contract)
plan, err := prepareSchema(context.Background(), preparer, contract)
if err != nil {
t.Fatalf("prepare validation: %v", err)
}
@@ -27,7 +51,7 @@ func prepareForTest(t testing.TB, preparer ValidationPreparer, contract domain.O
}
func TestStandardValidatorNoneSkipped(t *testing.T) {
v := NewStandardValidator("")
v := schemaDirectoryFS("")
res, err := prepareForTest(t, v, domain.OutputContract{
ValidationMode: domain.ValidationNone,
@@ -44,7 +68,7 @@ func TestStandardValidatorNoneSkipped(t *testing.T) {
}
func TestStandardValidatorBasicSuccess(t *testing.T) {
v := NewStandardValidator("")
v := schemaDirectoryFS("")
for _, tc := range []struct {
name string
body []byte
@@ -67,7 +91,7 @@ func TestStandardValidatorBasicSuccess(t *testing.T) {
}
func TestStandardValidatorBasicFailureEmpty(t *testing.T) {
v := NewStandardValidator("")
v := schemaDirectoryFS("")
for _, tc := range []struct {
name string
body []byte
@@ -94,7 +118,7 @@ func TestStandardValidatorBasicFailureEmpty(t *testing.T) {
}
func TestStandardValidatorReportsZeroRepairAttempts(t *testing.T) {
v := NewStandardValidator("")
v := schemaDirectoryFS("")
for _, tc := range []struct {
name string
@@ -131,7 +155,7 @@ func TestStandardValidatorReportsZeroRepairAttempts(t *testing.T) {
}
func TestStandardValidatorJSONSuccess(t *testing.T) {
v := NewStandardValidator("")
v := schemaDirectoryFS("")
res, err := prepareForTest(t, v, domain.OutputContract{
ValidationMode: domain.ValidationJSON,
@@ -145,7 +169,7 @@ func TestStandardValidatorJSONSuccess(t *testing.T) {
}
func TestStandardValidatorJSONFailure(t *testing.T) {
v := NewStandardValidator("")
v := schemaDirectoryFS("")
res, err := prepareForTest(t, v, domain.OutputContract{
ValidationMode: domain.ValidationJSON,
@@ -177,7 +201,7 @@ func TestJSONValidationChecksCompleteSyntaxWithoutChangingArtifact(t *testing.T)
{name: "trailing value", body: `1 2`, wantValid: false},
}
validator := NewStandardValidator("")
validator := schemaDirectoryFS("")
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
body := []byte(tc.body)
@@ -220,7 +244,7 @@ func TestStandardValidatorJSONSchemaSuccess(t *testing.T) {
t.Fatal(err)
}
v := NewStandardValidator(tmp)
v := schemaDirectoryFS(tmp)
res, err := prepareForTest(t, v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
@@ -259,8 +283,8 @@ func TestStandardValidatorPreparedSchemaSurvivesSourceRemoval(t *testing.T) {
t.Fatal(err)
}
validator := NewStandardValidator(tmp)
prepared, err := validator.PrepareValidation(context.Background(), domain.OutputContract{
validator := schemaDirectoryFS(tmp)
prepared, err := prepareSchema(context.Background(), validator, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "schema.json",
})
@@ -310,7 +334,7 @@ func TestStandardValidatorJSONSchemaNestedSchemaPathSuccess(t *testing.T) {
t.Fatal(err)
}
v := NewStandardValidator(tmp)
v := schemaDirectoryFS(tmp)
res, err := prepareForTest(t, v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
@@ -325,9 +349,9 @@ func TestStandardValidatorJSONSchemaNestedSchemaPathSuccess(t *testing.T) {
}
func TestStandardValidatorJSONSchemaNestedSchemaPathMissing(t *testing.T) {
v := NewStandardValidator(t.TempDir())
v := schemaDirectoryFS(t.TempDir())
_, err := v.PrepareValidation(context.Background(), domain.OutputContract{
_, err := prepareSchema(context.Background(), v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: filepath.Join("dnd", "missing.json"),
})
@@ -350,7 +374,7 @@ func TestStandardValidatorJSONSchemaFailure(t *testing.T) {
t.Fatal(err)
}
v := NewStandardValidator(tmp)
v := schemaDirectoryFS(tmp)
res, err := prepareForTest(t, v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
@@ -368,9 +392,9 @@ func TestStandardValidatorJSONSchemaFailure(t *testing.T) {
}
func TestStandardValidatorJSONSchemaSchemaLoadError(t *testing.T) {
v := NewStandardValidator(t.TempDir())
v := schemaDirectoryFS(t.TempDir())
_, err := v.PrepareValidation(context.Background(), domain.OutputContract{
_, err := prepareSchema(context.Background(), v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "missing.json",
})
@@ -384,9 +408,9 @@ func TestStandardValidatorJSONSchemaCompilationError(t *testing.T) {
if err := os.WriteFile(filepath.Join(tmp, "schema.json"), []byte(`{"type":42}`), 0o644); err != nil {
t.Fatal(err)
}
v := NewStandardValidator(tmp)
v := schemaDirectoryFS(tmp)
res, err := v.PrepareValidation(context.Background(), domain.OutputContract{
res, err := prepareSchema(context.Background(), v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "schema.json",
})
@@ -396,7 +420,7 @@ func TestStandardValidatorJSONSchemaCompilationError(t *testing.T) {
}
func TestFSValidatorJSONSchemaSuccess(t *testing.T) {
v := NewFSValidator(fstest.MapFS{
v := rootedSchemaFS(fstest.MapFS{
"schemas/events.schema.json": &fstest.MapFile{Data: []byte(`{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
@@ -438,8 +462,8 @@ func TestFSValidatorPreparedSchemaSurvivesSourceMutation(t *testing.T) {
"minimum": 2
}`)},
}
validator := NewFSValidator(fsys, ".")
prepared, err := validator.PrepareValidation(context.Background(), domain.OutputContract{
validator := rootedSchemaFS(fsys, ".")
prepared, err := prepareSchema(context.Background(), validator, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "schema.json",
})
@@ -477,7 +501,7 @@ func TestFSValidatorPreparedSchemaSurvivesSourceMutation(t *testing.T) {
func TestFSValidatorEscapesSchemaResourcePath(t *testing.T) {
for _, schemaName := range []string{"%zz.json", "space name.json", "hash#.json", "query?.json", "rún.json"} {
t.Run(schemaName, func(t *testing.T) {
v := NewFSValidator(fstest.MapFS{
v := rootedSchemaFS(fstest.MapFS{
"schemas/" + schemaName: &fstest.MapFile{Data: []byte(`{"type":"object"}`)},
}, "schemas")
@@ -503,7 +527,7 @@ func TestSchemaReferencesPreserveEscapedFilenames(t *testing.T) {
childSchema := []byte(`{"type":"integer","minimum":2}`)
t.Run("fs.FS", func(t *testing.T) {
validator := NewFSValidator(fstest.MapFS{
validator := rootedSchemaFS(fstest.MapFS{
"schemas/" + rootName: &fstest.MapFile{Data: rootSchema},
"schemas/" + childPath: &fstest.MapFile{Data: childSchema},
}, "schemas")
@@ -524,15 +548,15 @@ func TestSchemaReferencesPreserveEscapedFilenames(t *testing.T) {
if err := os.WriteFile(filepath.Join(root, filepath.FromSlash(childPath)), childSchema, 0o644); err != nil {
t.Fatal(err)
}
assertSchemaValidation(t, NewStandardValidator(root), rootName)
assertSchemaValidation(t, schemaDirectoryFS(root), rootName)
})
})
}
}
func assertSchemaValidation(t *testing.T, validator ValidationPreparer, schemaPath string) {
func assertSchemaValidation(t *testing.T, validator fs.FS, schemaPath string) {
t.Helper()
result, err := validator.PrepareValidation(context.Background(), domain.OutputContract{
result, err := prepareSchema(context.Background(), validator, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: schemaPath,
})
@@ -541,92 +565,6 @@ func assertSchemaValidation(t *testing.T, validator ValidationPreparer, schemaPa
}
}
func TestFSValidatorJSONSchemaPathContainment(t *testing.T) {
t.Run("nested schema inside root succeeds", func(t *testing.T) {
v := NewFSValidator(fstest.MapFS{
"schemas/nested/events.schema.json": &fstest.MapFile{Data: []byte(`{
"type": "object",
"required": ["events"],
"properties": {
"events": {"type": "array"}
}
}`)},
}, "schemas")
res, err := prepareForTest(t, v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "nested/events.schema.json",
}).Validate(context.Background(), &domain.Artifact{Body: []byte(`{"events":[]}`)})
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if res.Status != domain.ValidationPassed || !res.IsValid {
t.Fatalf("expected passed/valid, got status=%q valid=%v", res.Status, res.IsValid)
}
})
tests := []struct {
name string
schemaPath string
wantErr string
}{
{name: "parent escape rejected", schemaPath: "../outside.schema.json", wantErr: "escapes source root"},
{name: "absolute path rejected", schemaPath: "/outside.schema.json", wantErr: "must be relative"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
v := NewFSValidator(fstest.MapFS{
"schemas/events.schema.json": &fstest.MapFile{Data: []byte(`{"type":"object"}`)},
"outside.schema.json": &fstest.MapFile{Data: []byte(`{"type":"object"}`)},
"schemas/outside.schema.json": &fstest.MapFile{Data: []byte(`{"type":"object"}`)},
}, "schemas")
_, err := v.PrepareValidation(context.Background(), domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: tc.schemaPath,
})
if err == nil {
t.Fatal("expected schema path error")
}
if !strings.Contains(err.Error(), tc.wantErr) {
t.Fatalf("expected error to contain %q, got %v", tc.wantErr, err)
}
})
}
}
func TestFSValidatorSingleSchemaFileUsesBaseName(t *testing.T) {
v := NewFSValidator(fstest.MapFS{
"events.schema.json": &fstest.MapFile{Data: []byte(`{
"type": "object",
"required": ["events"],
"properties": {
"events": {"type": "array"}
}
}`)},
}, "events.schema.json")
res, err := prepareForTest(t, v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "events.schema.json",
}).Validate(context.Background(), &domain.Artifact{Body: []byte(`{"events":[]}`)})
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if res.Status != domain.ValidationPassed || !res.IsValid {
t.Fatalf("expected passed/valid, got status=%q valid=%v", res.Status, res.IsValid)
}
_, err = v.PrepareValidation(context.Background(), domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "other.schema.json",
})
if err == nil {
t.Fatal("expected schema path mismatch error")
}
}
func TestStandardValidatorJSONSchemaReferenceBoundaries(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "child.json"), []byte(`{
@@ -662,8 +600,8 @@ func TestStandardValidatorJSONSchemaReferenceBoundaries(t *testing.T) {
t.Fatal(err)
}
v := NewStandardValidator(root)
plan, err := v.PrepareValidation(context.Background(), domain.OutputContract{
v := schemaDirectoryFS(root)
plan, err := prepareSchema(context.Background(), v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "root.json",
})
@@ -703,12 +641,12 @@ func TestFSValidatorJSONSchemaReferenceBoundaries(t *testing.T) {
"$defs": {"value": {"type": "string"}},
"$ref": ` + strconv.Quote(tc.reference) + `
}`
v := NewFSValidator(fstest.MapFS{
v := rootedSchemaFS(fstest.MapFS{
"schemas/root.json": &fstest.MapFile{Data: []byte(rootSchema)},
"schemas/child.json": &fstest.MapFile{Data: []byte(`{"type":"string"}`)},
"outside.json": &fstest.MapFile{Data: []byte(`{"type":"string"}`)},
}, "schemas")
plan, err := v.PrepareValidation(context.Background(), domain.OutputContract{
plan, err := prepareSchema(context.Background(), v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "root.json",
})
@@ -818,7 +756,7 @@ func TestJSONSchemaDecodingRequiresOneCompleteDocument(t *testing.T) {
t.Run(source.name, func(t *testing.T) {
for _, schema := range []string{`{"type":`, `{} {}`} {
validator := source.new(t, []byte(schema))
_, err := validator.PrepareValidation(context.Background(), domain.OutputContract{
_, err := prepareSchema(context.Background(), validator, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "schema.json",
})
@@ -861,7 +799,7 @@ func TestPreparedSchemaDocumentsRetainExactNumbers(t *testing.T) {
for _, source := range jsonSchemaValidatorSources() {
t.Run(source.name, func(t *testing.T) {
validator := source.new(t, []byte(schema))
prepared, err := validator.PrepareValidation(context.Background(), domain.OutputContract{
prepared, err := prepareSchema(context.Background(), validator, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "schema.json",
})
@@ -887,27 +825,27 @@ func TestPreparedSchemaDocumentsRetainExactNumbers(t *testing.T) {
type jsonSchemaValidatorSource struct {
name string
new func(*testing.T, []byte) ValidationPreparer
new func(*testing.T, []byte) fs.FS
}
func jsonSchemaValidatorSources() []jsonSchemaValidatorSource {
return []jsonSchemaValidatorSource{
{
name: "operating system files",
new: func(t *testing.T, schema []byte) ValidationPreparer {
new: func(t *testing.T, schema []byte) fs.FS {
t.Helper()
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "schema.json"), schema, 0o644); err != nil {
t.Fatalf("write schema: %v", err)
}
return NewStandardValidator(root)
return schemaDirectoryFS(root)
},
},
{
name: "fs.FS",
new: func(t *testing.T, schema []byte) ValidationPreparer {
new: func(t *testing.T, schema []byte) fs.FS {
t.Helper()
return NewFSValidator(fstest.MapFS{
return rootedSchemaFS(fstest.MapFS{
"schema.json": &fstest.MapFile{Data: schema},
}, ".")
},
@@ -925,7 +863,7 @@ func BenchmarkJSONValidation(b *testing.B) {
{name: "object", body: []byte(`{"name":"eris","count":9007199254740993,"enabled":true}`)},
{name: "large array", body: largeArray},
}
validator := NewStandardValidator("")
validator := schemaDirectoryFS("")
contract := domain.OutputContract{ValidationMode: domain.ValidationJSON}
plan := prepareForTest(b, validator, contract)
@@ -961,7 +899,7 @@ func BenchmarkBasicValidation(b *testing.B) {
{name: "whitespace_only_text", body: largeWhitespace},
{name: "unicode_whitespace", body: unicodeWhitespace},
}
validator := NewStandardValidator("")
validator := schemaDirectoryFS("")
contract := domain.OutputContract{ValidationMode: domain.ValidationBasic}
ctx := context.Background()
plan := prepareForTest(b, validator, contract)
@@ -1010,10 +948,10 @@ func TestJSONSchemaDialectIsDraft2020(t *testing.T) {
if err != nil {
t.Fatal(err)
}
v := NewFSValidator(fstest.MapFS{
v := rootedSchemaFS(fstest.MapFS{
"schema.json": &fstest.MapFile{Data: data},
}, ".")
_, err = v.PrepareValidation(context.Background(), domain.OutputContract{
_, err = prepareSchema(context.Background(), v, domain.OutputContract{
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: "schema.json",
})

View File

@@ -28,5 +28,5 @@ type PreparedValidation interface {
// decoding and compilation. Filesystem and compiler calls remain synchronous,
// so cancellation becomes authoritative when an in-progress call returns.
type ValidationPreparer interface {
PrepareValidation(ctx context.Context, contract domain.OutputContract) (PreparedValidation, error)
PrepareValidation(ctx context.Context, input Input) (PreparedValidation, error)
}