Share self-contained schema compilation

This commit is contained in:
2026-11-05 13:56:03 +00:00
parent 1e122f0473
commit e056cb1607
4 changed files with 60 additions and 205 deletions

View File

@@ -232,8 +232,9 @@ func TestStandardValidatorPreparedSchemaSurvivesSourceRemoval(t *testing.T) {
"title": "original root",
"type": "object",
"required": ["value"],
"$defs": {"value": {"type":"integer", "minimum":2}},
"properties": {
"value": {"$ref": "value.json"}
"value": {"$ref": "#/$defs/value"}
}
}`)
rootPath := filepath.Join(tmp, "schema.json")
@@ -415,8 +416,9 @@ func TestFSValidatorPreparedSchemaSurvivesSourceMutation(t *testing.T) {
"title": "original root",
"type": "object",
"required": ["value"],
"$defs": {"value": {"type":"integer", "minimum":2}},
"properties": {
"value": {"$ref": "value.json"}
"value": {"$ref": "#/$defs/value"}
}
}`)
fsys := fstest.MapFS{
@@ -525,8 +527,8 @@ func assertSchemaValidation(t *testing.T, validator Validator, schemaPath string
ValidationMode: domain.ValidationJSONSchema,
SchemaPath: schemaPath,
})
if err != nil || !result.IsValid {
t.Fatalf("validate schema %q: result=%#v error=%v", schemaPath, result, err)
if err == nil || !strings.Contains(err.Error(), "external schema resource") {
t.Fatalf("external reference schema %q: result=%#v error=%v", schemaPath, result, err)
}
}
@@ -631,9 +633,9 @@ func TestStandardValidatorJSONSchemaReferenceBoundaries(t *testing.T) {
wantError string
writeOuter bool
}{
{name: "contained relative reference", reference: "child.json"},
{name: "remote reference", reference: "https://example.test/schema.json", wantError: "not a contained file reference"},
{name: "escaping reference", reference: "../outside.json", wantError: "escapes source root", writeOuter: true},
{name: "contained relative reference", reference: "child.json", wantError: "external schema resource"},
{name: "remote reference", reference: "https://example.test/schema.json", wantError: "external schema resource"},
{name: "escaping reference", reference: "../outside.json", wantError: "external schema resource", writeOuter: true},
}
for _, tc := range tests {
@@ -676,9 +678,9 @@ func TestFSValidatorJSONSchemaReferenceBoundaries(t *testing.T) {
wantError string
}{
{name: "same document fragment", reference: "#/$defs/value"},
{name: "contained relative reference", reference: "child.json"},
{name: "remote reference", reference: "https://example.test/schema.json", wantError: "is not allowed"},
{name: "escaping reference", reference: "../outside.json", wantError: "escapes source root"},
{name: "contained relative reference", reference: "child.json", wantError: "external schema resource"},
{name: "remote reference", reference: "https://example.test/schema.json", wantError: "external schema resource"},
{name: "escaping reference", reference: "../outside.json", wantError: "external schema resource"},
}
for _, tc := range tests {