Skip to content

Commit 8c144e7

Browse files
- Changed interface.
1 parent 7866398 commit 8c144e7

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

pkg/docval/docval_fs.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,39 @@ import (
55
"encoding/json"
66
"fmt"
77
"os"
8+
"path"
89
"path/filepath"
910
"strings"
1011
)
1112

13+
type FileValidator interface {
14+
ValidateAndParseFile(docPath string, schemaPath string) (map[string]any, error)
15+
}
16+
17+
type fileValidator struct {
18+
rootSchemaDir string
19+
}
20+
21+
func NewFileValidator(rootSchemaDir string) FileValidator {
22+
return &fileValidator{
23+
rootSchemaDir: rootSchemaDir,
24+
}
25+
}
26+
1227
// ValidateAndParseFile loads a document and its schema from the local filesystem,
1328
// rewrites external $ref values to absolute file:// URLs, and delegates to ValidateAndParse().
1429
//
1530
// Example:
1631
//
1732
// parsed, err := docval.ValidateAndParseFile("fragment.yaml", "fragmented-resources.schema.json", "fragment")
18-
func ValidateAndParseFile(docPath string, schemaPath string) (map[string]any, error) {
33+
func (v *fileValidator) ValidateAndParseFile(docPath string, schemaPath string) (map[string]any, error) {
1934
docType := "" // reserved for future use
2035
docBytes, err := os.ReadFile(docPath)
2136
if err != nil {
2237
return nil, fmt.Errorf("read document %q: %w", docPath, err)
2338
}
2439

25-
rewritten, err := rewriteSchemaRefsToFileURLs(schemaPath)
40+
rewritten, err := v.rewriteSchemaRefsToFileURLs(schemaPath)
2641
if err != nil {
2742
return nil, fmt.Errorf("prepare schema %q: %w", schemaPath, err)
2843
}
@@ -35,20 +50,26 @@ func ValidateAndParseFile(docPath string, schemaPath string) (map[string]any, er
3550
// rewriteSchemaRefsToFileURLs loads a JSON schema, finds all objects with a "$ref" string,
3651
// and for refs that are *external* (not starting with "#", not http/https, not file://),
3752
// rewrites them into absolute file:// URLs using schemaPath as the base.
38-
func rewriteSchemaRefsToFileURLs(schemaPath string) ([]byte, error) {
39-
absSchema, err := filepath.Abs(schemaPath)
53+
func (v *fileValidator) rewriteSchemaRefsToFileURLs(schemaPath string) ([]byte, error) {
54+
absSchema, err := filepath.Abs(path.Join(v.rootSchemaDir, schemaPath))
55+
if err != nil {
56+
return nil, err
57+
}
58+
59+
rootPath, err := filepath.Abs(v.rootSchemaDir)
4060
if err != nil {
4161
return nil, err
4262
}
43-
rootDir := filepath.Dir(absSchema)
4463

4564
var m map[string]any
4665
if err := readJSONSchema(absSchema, &m); err != nil {
4766
return nil, err
4867
}
4968

5069
// walk and rewrite
51-
rewriteRefs(m, rootDir)
70+
rewriteRefs(m, rootPath)
71+
72+
// marshal back to JSON
5273

5374
// re-encode pretty
5475
var buf bytes.Buffer

pkg/docval/docval_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"github.com/stackql/any-sdk/pkg/docval"
77
)
88

9+
var (
10+
validator docval.FileValidator = docval.NewFileValidator("testdata/schema-definitions")
11+
)
12+
913
func TestValidateAndParse_ValidJSON(t *testing.T) {
1014
jsonDoc := []byte(`{"name": "Alice", "age": 30}`)
1115
jsonSchema := []byte(`{
@@ -27,7 +31,7 @@ func TestValidateAndParse_ValidJSON(t *testing.T) {
2731
}
2832

2933
func TestLocalValidateAndParseValidProviderFile(t *testing.T) {
30-
rv, err := docval.ValidateAndParseFile("testdata/docs/local_openssl/v0.1.0/provider.yaml", "testdata/schema-definitions/provider.schema.json")
34+
rv, err := validator.ValidateAndParseFile("testdata/docs/local_openssl/v0.1.0/provider.yaml", "provider.schema.json")
3135
if err != nil {
3236
t.Fatalf("expected no error, got %v", err)
3337
}
@@ -37,7 +41,7 @@ func TestLocalValidateAndParseValidProviderFile(t *testing.T) {
3741
}
3842

3943
func TestValidateAndParseGoogleProviderFile(t *testing.T) {
40-
rv, err := docval.ValidateAndParseFile("testdata/docs/googleapis.com/v0.1.2/provider.yaml", "testdata/schema-definitions/provider.schema.json")
44+
rv, err := validator.ValidateAndParseFile("testdata/docs/googleapis.com/v0.1.2/provider.yaml", "provider.schema.json")
4145
if err == nil {
4246
t.Fatalf("expected an error, got none")
4347
}
@@ -47,7 +51,7 @@ func TestValidateAndParseGoogleProviderFile(t *testing.T) {
4751
}
4852

4953
func TestFragmentedResourcesFile(t *testing.T) {
50-
rv, err := docval.ValidateAndParseFile("testdata/docs/googleapis.com/v0.1.2/resources/compute-v1.yaml", "testdata/schema-definitions/fragmented-resources.schema.json")
54+
rv, err := validator.ValidateAndParseFile("testdata/docs/googleapis.com/v0.1.2/resources/compute-v1.yaml", "fragmented-resources.schema.json")
5155
if err != nil {
5256
t.Fatalf("expected no error, got %v", err)
5357
}
@@ -61,7 +65,7 @@ func TestFragmentedResourcesFile(t *testing.T) {
6165
}
6266

6367
func TestMonolithicCompositeServiceFile(t *testing.T) {
64-
rv, err := docval.ValidateAndParseFile("testdata/docs/googleapis.com/v0.1.2/services/bigquery-v2.yaml", "testdata/schema-definitions/service-resources.schema.json")
68+
rv, err := validator.ValidateAndParseFile("testdata/docs/googleapis.com/v0.1.2/services/bigquery-v2.yaml", "service-resources.schema.json")
6569
if err != nil {
6670
t.Fatalf("expected no error, got %v", err)
6771
}
@@ -71,7 +75,7 @@ func TestMonolithicCompositeServiceFile(t *testing.T) {
7175
}
7276

7377
func TestSplitCompositeServiceFile(t *testing.T) {
74-
rv, err := docval.ValidateAndParseFile("testdata/docs/googleapis.com/v0.1.2/services-split/compute/compute-disks-v1.yaml", "testdata/schema-definitions/service-resources.schema.json")
78+
rv, err := validator.ValidateAndParseFile("testdata/docs/googleapis.com/v0.1.2/services-split/compute/compute-disks-v1.yaml", "service-resources.schema.json")
7579
if err != nil {
7680
t.Fatalf("expected no error, got %v", err)
7781
}
@@ -81,7 +85,7 @@ func TestSplitCompositeServiceFile(t *testing.T) {
8185
}
8286

8387
func TestLocalTemplatedCompositeServiceFile(t *testing.T) {
84-
rv, err := docval.ValidateAndParseFile("testdata/docs/local_openssl/v0.1.0/services/keys.yaml", "testdata/schema-definitions/local-templated.service-resources.schema.json")
88+
rv, err := validator.ValidateAndParseFile("testdata/docs/local_openssl/v0.1.0/services/keys.yaml", "local-templated.service-resources.schema.json")
8589
if err != nil {
8690
t.Fatalf("expected no error, got %v", err)
8791
}

public/discovery/shallow_discovery_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestDiscoveryAWS(t *testing.T) {
135135
t.Fatal("Expected 'aws' provider to be available")
136136
}
137137
awsProviderPath := "testdata/registry/basic/src/aws/v0.1.0/provider.yaml"
138-
analysisCfg := discovery.NewAnalyzerCfg("openapi", "testdata/registry/basic/src", awsProviderPath, schemaLocalPath, false)
138+
analysisCfg := discovery.NewAnalyzerCfg("openapi", "testdata/registry/basic/src", awsProviderPath, schemaLocalPath, true)
139139
analysisCfg.SetIsProviderServicesMustExpand(true) // not always the case
140140
rtCtx := dto.RuntimeCtx{CLISchemaDir: "testdata/schema-definitions"}
141141
staticAnalyzer, analyzerErr := discovery.NewStaticAnalyzer(

public/discovery/static_analyzer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func newGenericStaticAnalyzer(
126126
discoveryAdapter: discoveryAdapter,
127127
registryAPI: registryAPI,
128128
schemaDir: schemaDir,
129+
validator: docval.NewFileValidator(schemaDir),
129130
}
130131
}
131132

@@ -945,6 +946,7 @@ type genericStaticAnalyzer struct {
945946
discoveryStore IDiscoveryStore
946947
registryAPI anysdk.RegistryAPI
947948
schemaDir string
949+
validator docval.FileValidator
948950
}
949951

950952
// For each operation store in each resource:
@@ -976,7 +978,7 @@ func (osa *genericStaticAnalyzer) Analyze() error {
976978
// --- DOCVAL ANALYSIS ---
977979
schemaDir := osa.schemaDir
978980
if !osa.cfg.IsSkipSchemaValidation() {
979-
result, err := docval.ValidateAndParseFile(osa.cfg.GetDocRoot(), path.Join(schemaDir, "provider.schema.json"))
981+
result, err := osa.validator.ValidateAndParseFile(osa.cfg.GetDocRoot(), path.Join(schemaDir, "provider.schema.json"))
980982
if err != nil {
981983
osa.errors = append(osa.errors, fmt.Errorf("docval error in provider file: %v", err))
982984
}
@@ -997,7 +999,7 @@ func (osa *genericStaticAnalyzer) Analyze() error {
997999
schemaPath = path.Join(schemaDir, "local-templated-service-resource.schema.json")
9981000
}
9991001
if svcPath != "" {
1000-
result, err := docval.ValidateAndParseFile(svcPath, schemaPath)
1002+
result, err := osa.validator.ValidateAndParseFile(svcPath, schemaPath)
10011003
if err != nil {
10021004
osa.errors = append(osa.errors, fmt.Errorf("docval error in service file %s: %v", svcPath, err))
10031005
}

0 commit comments

Comments
 (0)