Unify inline and bound schema preparation
This commit is contained in:
@@ -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",
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user