changes in Initialse method as adding AddResource function

This commit is contained in:
AshwiniK-protean
2025-02-28 18:02:20 +05:30
parent 26c97c5774
commit 4b940d9e51
57 changed files with 2516 additions and 2993 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
@@ -84,10 +85,10 @@ func (v *tekuriValidator) Validate(ctx context.Context, data []byte) error {
// }
// (Approach 2)(all json files for each schema from sub directories)
func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plugins.Validator, error) {
vp.schemaCache = make(map[string]map[string]*jsonschema.Schema)
validatorCache := make(map[string]plugins.Validator)
baseCustomID := "https://core/v1.1.0/"
// Walk through the directory and compile all .json files
err := filepath.Walk(schemaDir, func(path string, info os.FileInfo, err error) error {
@@ -96,12 +97,28 @@ func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plug
}
if !info.IsDir() && filepath.Ext(info.Name()) == ".json" {
filePath := filepath.Join(schemaDir, info.Name())
fmt.Println("printing path : ", path)
fmt.Println("Compiling file path: ", filePath)
// Construct the CustomID using baseCustomID and the file name
customID := baseCustomID + info.Name()
compiler := jsonschema.NewCompiler()
compiledSchema, err := compiler.Compile(path)
resource, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open JSON schema file %s: %v", info.Name(), err)
}
defer func() {
if err := resource.Close(); err != nil {
log.Printf("Error closing resource: %v", err)
}
}()
// defer resource.Close()
if err := compiler.AddResource(customID, resource); err != nil {
return fmt.Errorf("failed to add resource for JSON schema file %s and custom id is %s: %v", info.Name(), customID, err)
}
// compiledSchema, err := compiler.Compile(path)
compiledSchema, err := compiler.Compile(customID)
if err != nil {
return fmt.Errorf("failed to compile JSON schema from file %s: %v", info.Name(), err)
}
@@ -109,8 +126,6 @@ func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plug
return fmt.Errorf("compiled schema is nil for file %s", info.Name())
}
fmt.Printf("Compiled schema for file %s: %+v\n", info.Name(), compiledSchema)
dir := filepath.Base(filepath.Dir(filePath))
if vp.schemaCache[dir] == nil {
vp.schemaCache[dir] = make(map[string]*jsonschema.Schema)
@@ -131,6 +146,7 @@ var _ plugins.ValidatorProvider = (*tekuriValidatorProvider)(nil)
var providerInstance = &tekuriValidatorProvider{}
// GetProvider returns the ValidatorProvider instance.
func GetProvider() plugins.ValidatorProvider {
return providerInstance
}

View File

@@ -21,7 +21,7 @@ type Message struct{}
func TestInitializeValidDirectory(t *testing.T) {
provider := &tekuriValidatorProvider{}
schemaDir := "../schema/ondc_trv10_2.0.0/"
schemaDir := "../schema_valid/ondc_trv10_2.0.0/"
_, err := provider.Initialize(schemaDir)
if err != nil {
t.Fatalf("expected no error, got %v", err)
@@ -30,7 +30,7 @@ func TestInitializeValidDirectory(t *testing.T) {
func TestInitializeInValidDirectory(t *testing.T) {
provider := &tekuriValidatorProvider{}
schemaDir := "../schemas/ondc_trv10_2.0.0/"
schemaDir := "../schema/ondc_trv10_2.0.0/"
_, err := provider.Initialize(schemaDir)
if err != nil {
t.Fatalf("failed to read schema directory: %v", err)
@@ -68,7 +68,7 @@ func TestInvalidCompileSchema(t *testing.T) {
}
func TestValidateData(t *testing.T) {
schemaDir := "../schema/ondc_trv10_2.0.0/"
schemaDir := "../schema_valid/ondc_trv10_2.0.0/"
if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
t.Fatalf("Schema directory does not exist: %v", schemaDir)
}
@@ -108,7 +108,7 @@ func TestValidateData(t *testing.T) {
}
func TestInValidateData(t *testing.T) {
schemaDir := "../schema/ondc_trv10_2.0.0/"
schemaDir := "../schema_valid/ondc_trv10_2.0.0/"
if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
t.Fatalf("Schema directory does not exist: %v", schemaDir)
@@ -138,7 +138,7 @@ func TestInValidateData(t *testing.T) {
}
func TestInValidateUnmarshalData(t *testing.T) {
schemaDir := "../schema/ondc_trv10_2.0.0/"
schemaDir := "../schema_valid/ondc_trv10_2.0.0/"
if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
t.Fatalf("Schema directory does not exist: %v", schemaDir)