From a939d7d8ae4f2f1b68bc442e24e2ab282b5a1e56 Mon Sep 17 00:00:00 2001 From: AshwiniK-protean Date: Thu, 24 Apr 2025 10:37:47 +0530 Subject: [PATCH] add errors for context in schemavalidato.go --- pkg/plugin/definition/validator.go | 16 -- .../schemavalidator/schemavalidator.go | 71 ++++++++- pkg/plugin/manager.go | 143 ------------------ 3 files changed, 69 insertions(+), 161 deletions(-) delete mode 100644 pkg/plugin/definition/validator.go diff --git a/pkg/plugin/definition/validator.go b/pkg/plugin/definition/validator.go deleted file mode 100644 index dc69ca5..0000000 --- a/pkg/plugin/definition/validator.go +++ /dev/null @@ -1,16 +0,0 @@ -package definition - -import ( - "context" - "net/url" -) - -// SchemaValidator interface for schema validation. -type SchemaValidator interface { - Validate(ctx context.Context, url *url.URL, payload []byte) error -} - -// SchemaValidatorProvider interface for creating validators. -type SchemaValidatorProvider interface { - New(ctx context.Context, config map[string]string) (SchemaValidator, func() error, error) -} diff --git a/pkg/plugin/implementation/schemavalidator/schemavalidator.go b/pkg/plugin/implementation/schemavalidator/schemavalidator.go index 5a04a4c..46fe79d 100644 --- a/pkg/plugin/implementation/schemavalidator/schemavalidator.go +++ b/pkg/plugin/implementation/schemavalidator/schemavalidator.go @@ -19,8 +19,25 @@ import ( // Payload represents the structure of the data payload with context information. type payload struct { Context struct { - Domain string `json:"domain"` - Version string `json:"version"` + Domain string `json:"domain"` + Version string `json:"version"` + Action string `json:"action"` + BapID string `json:"bap_id"` + BapURI string `json:"bap_uri"` + BppID string `json:"bpp_id"` + BppURI string `json:"bpp_uri"` + MessageID string `json:"message_id"` + TransactionID string `json:"transaction_id"` + Timestamp string `json:"timestamp"` + TTL string `json:"ttl"` + Location struct { + Country struct { + Code string `json:"code"` + } `json:"country"` + City struct { + Code string `json:"code"` + } `json:"city"` + } `json:"location"` } `json:"context"` } @@ -61,6 +78,10 @@ func (v *schemaValidator) Validate(ctx context.Context, url *url.URL, data []byt return model.NewBadReqErr(fmt.Errorf("failed to parse JSON payload: %v", err)) } + if err := validateContext(payloadData); err != nil { + return err + } + // Extract domain, version, and endpoint from the payload and uri. cxtDomain := payloadData.Context.Domain version := payloadData.Context.Version @@ -111,6 +132,52 @@ func (v *schemaValidator) Validate(ctx context.Context, url *url.URL, data []byt return nil } +func validateContext(p payload) error { + // Validate the payload context fields FIRST + if p.Context.Domain == "" { + return fmt.Errorf("missing field Domain in context") + } + if p.Context.Version == "" { + return fmt.Errorf("missing field Version in context") + } + if p.Context.Action == "" { + return fmt.Errorf("missing field action in context") + } + if p.Context.BapID == "" { + return fmt.Errorf("missing field bpp_id in context") + } + if p.Context.BapURI == "" { + return fmt.Errorf("missing field bpp_uri in context") + } + if p.Context.BppID == "" { + return fmt.Errorf("missing field bpp_id in context") + } + + if p.Context.BppURI == "" { + return fmt.Errorf("missing field bpp_uri in context") + } + if p.Context.MessageID == "" { + return fmt.Errorf("missing field message_id in context") + } + if p.Context.TransactionID == "" { + return fmt.Errorf("missing field transaction_id in context") + } + if p.Context.Timestamp == "" { + return fmt.Errorf("missing field timestamp in context") + } + if p.Context.TTL == "" { + return fmt.Errorf("missing field ttl in context") + } + if p.Context.Location.Country.Code == "" { + return fmt.Errorf("missing Location.Country.Code in context") + } + if p.Context.Location.City.Code == "" { + return fmt.Errorf("missing field Location.City.Code in context") + } + return nil + +} + // Initialise initialises the validator provider by compiling all the JSON schema files // from the specified directory and storing them in a cache indexed by their schema filenames. func (v *schemaValidator) initialise() error { diff --git a/pkg/plugin/manager.go b/pkg/plugin/manager.go index c776b51..1fa6655 100644 --- a/pkg/plugin/manager.go +++ b/pkg/plugin/manager.go @@ -1,158 +1,16 @@ package plugin import ( -<<<<<<< HEAD - "context" - "fmt" -======= "archive/zip" "context" "fmt" "io" "io/fs" "net/http" ->>>>>>> fdec61e90d57d3d82345d023c1a0d33d5a90583b "os" "path/filepath" "plugin" "strings" -<<<<<<< HEAD - - "github.com/beckn/beckn-onix/pkg/plugin/definition" - - "gopkg.in/yaml.v2" -) - -// Config represents the plugin manager configuration. -type Config struct { - Root string `yaml:"root"` - SchemaValidator PluginConfig `yaml:"schema_validator"` -} - -// PluginConfig represents configuration details for a plugin. -type PluginConfig struct { - ID string `yaml:"id"` - Config map[string]string `yaml:"config"` -} - -// // ValidationPluginConfig represents configuration details for a plugin. -// type ValidationPluginConfig struct { -// ID string `yaml:"id"` -// Schema SchemaDetails `yaml:"config"` -// PluginPath string `yaml:"plugin_path"` -// } - -// SchemaDetails contains information about the plugin schema directory. -type SchemaDetails struct { - SchemaDir string `yaml:"schema_dir"` -} - -// // Config represents the configuration for the application, including plugin configurations. -// type Config struct { -// Plugins struct { -// ValidationPlugin ValidationPluginConfig `yaml:"validation_plugin"` -// } `yaml:"plugins"` -// } - -// Manager handles dynamic plugin loading and management. -type Manager struct { - vp definition.SchemaValidatorProvider - cfg *Config -} - -// NewManager initializes a new Manager with the given configuration file. -func NewManager(ctx context.Context, cfg *Config) (*Manager, error) { - if cfg == nil { - return nil, fmt.Errorf("configuration cannot be nil") - } - - // Load schema validator plugin - vp, err := provider[definition.SchemaValidatorProvider](cfg.Root, cfg.SchemaValidator.ID) - if err != nil { - return nil, fmt.Errorf("failed to load validator plugin: %w", err) - } - if vp == nil { - return nil, fmt.Errorf("validator provider is nil") - } - - // // Initialize validator - // validatorMap, defErr := vp.New(ctx, map[string]string{ - // "schema_dir": cfg.Plugins.ValidationPlugin.Schema.SchemaDir, - // }) - // if defErr != nil { - // return nil, fmt.Errorf("failed to initialize validator: %v", defErr) - // } - - // // Initialize the validators map - // validators := make(map[string]definition.Validator) - // for key, validator := range validatorMap { - // validators[key] = validator - // } - - return &Manager{vp: vp, cfg: cfg}, nil -} - -// provider loads a plugin dynamically and retrieves its provider instance. -func provider[T any](path string, id string) (T, error) { - var zero T - if len(strings.TrimSpace(id)) == 0 { - return zero, nil - } - - p, err := plugin.Open(pluginPath(path, id)) - if err != nil { - return zero, fmt.Errorf("failed to open plugin %s: %w", id, err) - } - - symbol, err := p.Lookup("Provider") - if err != nil { - return zero, fmt.Errorf("failed to find Provider symbol in plugin %s: %w", id, err) - } - - // Ensure the symbol is of the correct type - prov, ok := symbol.(*T) - if !ok { - return zero, fmt.Errorf("failed to cast Provider for %s", id) - } - - return *prov, nil -} - -// pluginPath constructs the path to the plugin pkg object file. -func pluginPath(path, id string) string { - return filepath.Join(path, id+".so") -} - -// Validators retrieves the validation plugin instances. -func (m *Manager) SchemaValidator(ctx context.Context) (definition.SchemaValidator, func() error, error) { - if m.vp == nil { - return nil, nil, fmt.Errorf("schema validator plugin provider not loaded") - - } - schemaValidator, close, err := m.vp.New(ctx, m.cfg.SchemaValidator.Config) - if err != nil { - - return nil, nil, fmt.Errorf("failed to initialize schema validator: %v", err) - } - return schemaValidator, close, nil -} - -// LoadConfig loads the configuration from a YAML file. -func LoadConfig(path string) (*Config, error) { - file, err := os.Open(path) - if err != nil { - return nil, fmt.Errorf("failed to open config file: %w", err) - } - defer file.Close() - - var cfg Config - decoder := yaml.NewDecoder(file) - if err := decoder.Decode(&cfg); err != nil { - return nil, fmt.Errorf("failed to decode config file: %w", err) - } - - return &cfg, nil -======= "time" "github.com/beckn/beckn-onix/pkg/log" @@ -528,5 +386,4 @@ func unzip(src, dest string) error { } return nil ->>>>>>> fdec61e90d57d3d82345d023c1a0d33d5a90583b }