From 3a0a0b3940303ea2083bd9d665364db22c8cb198 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sun, 24 May 2026 06:51:50 -0500 Subject: [PATCH] Moved JSON response schemas into separate files --- .../assets/correction_set.v1.json | 41 +++++++++++++++++++ .../assets/validator_decision_set.v1.json | 39 ++++++++++++++++++ internal/framework/responseschema/registry.go | 20 +++++++-- .../framework/responseschema/registry_test.go | 20 ++++++++- 4 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 internal/framework/responseschema/assets/correction_set.v1.json create mode 100644 internal/framework/responseschema/assets/validator_decision_set.v1.json diff --git a/internal/framework/responseschema/assets/correction_set.v1.json b/internal/framework/responseschema/assets/correction_set.v1.json new file mode 100644 index 0000000..5f53592 --- /dev/null +++ b/internal/framework/responseschema/assets/correction_set.v1.json @@ -0,0 +1,41 @@ +{ + "type": "object", + "additionalProperties": false, + "required": [ + "corrections" + ], + "properties": { + "corrections": { + "type": "array", + "items": { + "type": "object", + "additionalProperties": false, + "required": [ + "id", + "original_text", + "corrected_text", + "confidence" + ], + "properties": { + "id": { + "type": "integer", + "minimum": 1 + }, + "original_text": { + "type": "string", + "minLength": 1 + }, + "corrected_text": { + "type": "string", + "minLength": 1 + }, + "confidence": { + "type": "number", + "minimum": 0, + "maximum": 1 + } + } + } + } + } +} diff --git a/internal/framework/responseschema/assets/validator_decision_set.v1.json b/internal/framework/responseschema/assets/validator_decision_set.v1.json new file mode 100644 index 0000000..608dafa --- /dev/null +++ b/internal/framework/responseschema/assets/validator_decision_set.v1.json @@ -0,0 +1,39 @@ +{ + "type": "object", + "additionalProperties": false, + "required": [ + "validations" + ], + "properties": { + "validations": { + "type": "array", + "items": { + "type": "object", + "additionalProperties": false, + "required": [ + "correction_index", + "approved", + "confidence", + "reason" + ], + "properties": { + "correction_index": { + "type": "integer", + "minimum": 0 + }, + "approved": { + "type": "boolean" + }, + "confidence": { + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "reason": { + "type": "string" + } + } + } + } + } +} diff --git a/internal/framework/responseschema/registry.go b/internal/framework/responseschema/registry.go index f970570..ada4425 100644 --- a/internal/framework/responseschema/registry.go +++ b/internal/framework/responseschema/registry.go @@ -2,6 +2,7 @@ package responseschema import ( "crypto/sha256" + "embed" "encoding/hex" "encoding/json" "fmt" @@ -20,6 +21,9 @@ const ( schemaVersionV1 = "v1" ) +//go:embed assets/*.json +var schemaAssets embed.FS + // Schema describes one registered structured response schema. type Schema struct { ID string `json:"id"` @@ -39,17 +43,17 @@ func (s Schema) DiagnosticsMap() map[string]any { } var registry = map[Key]Schema{ - CorrectionSetKey: mustBuildSchema( + CorrectionSetKey: mustBuildSchemaFromAsset( correctionSetSchemaID, schemaVersionV1, "audita_correction_set_v1", - []byte(`{"type":"object","additionalProperties":false,"required":["corrections"],"properties":{"corrections":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["id","original_text","corrected_text","confidence"],"properties":{"id":{"type":"integer","minimum":1},"original_text":{"type":"string","minLength":1},"corrected_text":{"type":"string","minLength":1},"confidence":{"type":"number","minimum":0,"maximum":1}}}}}}`), + "assets/correction_set.v1.json", ), - ValidatorDecisionSetKey: mustBuildSchema( + ValidatorDecisionSetKey: mustBuildSchemaFromAsset( validatorDecisionSchemaID, schemaVersionV1, "audita_validator_decision_set_v1", - []byte(`{"type":"object","additionalProperties":false,"required":["validations"],"properties":{"validations":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["correction_index","approved","confidence","reason"],"properties":{"correction_index":{"type":"integer","minimum":0},"approved":{"type":"boolean"},"confidence":{"type":"number","minimum":0,"maximum":1},"reason":{"type":"string"}}}}}}`), + "assets/validator_decision_set.v1.json", ), } @@ -93,6 +97,14 @@ func cloneSchema(in Schema) Schema { return out } +func mustBuildSchemaFromAsset(id string, version string, name string, assetPath string) Schema { + rawSchema, err := schemaAssets.ReadFile(assetPath) + if err != nil { + panic(fmt.Sprintf("read response schema asset %q: %v", assetPath, err)) + } + return mustBuildSchema(id, version, name, rawSchema) +} + func mustBuildSchema(id string, version string, name string, rawSchema []byte) Schema { id = strings.TrimSpace(id) version = strings.TrimSpace(version) diff --git a/internal/framework/responseschema/registry_test.go b/internal/framework/responseschema/registry_test.go index 9e77dbc..5dc8bef 100644 --- a/internal/framework/responseschema/registry_test.go +++ b/internal/framework/responseschema/registry_test.go @@ -1,14 +1,16 @@ package responseschema import ( + "bytes" "crypto/sha256" "encoding/hex" + "encoding/json" "testing" ) const ( - expectedCorrectionSetSHA256 = "05f8ff3fa04f68115c0cb1859d2656f51aa5c0bae8ff2470b2d4f6f531953195" - expectedValidatorDecisionSetSHA256 = "b73f4790b98fbb955f0aec5496dd8ce9a8fe14aa2f35c700b4b4e5634f106fd5" + expectedCorrectionSetSHA256 = "b86a2dde38d7f440d26470fa8830167512bb0aa1b35e5fee5547057be583c388" + expectedValidatorDecisionSetSHA256 = "2fe90d450e2595b57885aba91c8cc5cedf783dd36758ab430309e3eace401f54" ) func TestLookupKnownSchemas(t *testing.T) { @@ -47,6 +49,20 @@ func TestLookupUnknownSchema(t *testing.T) { } } +func TestRegisteredSchemasLoadReadableEmbeddedJSON(t *testing.T) { + for _, schema := range Registered() { + if len(schema.JSONSchema) == 0 { + t.Fatalf("expected non-empty JSON schema for %q", schema.ID) + } + if !json.Valid(schema.JSONSchema) { + t.Fatalf("expected valid JSON schema for %q", schema.ID) + } + if !bytes.Contains(schema.JSONSchema, []byte("\n ")) { + t.Fatalf("expected readable formatted JSON schema for %q", schema.ID) + } + } +} + func TestSchemaHashesMatchRegisteredJSON(t *testing.T) { expectedByKey := map[Key]string{ CorrectionSetKey: expectedCorrectionSetSHA256,