add errors for context in schemavalidato.go

This commit is contained in:
AshwiniK-protean
2025-04-24 10:37:47 +05:30
parent d5467e8bf0
commit a939d7d8ae
3 changed files with 69 additions and 161 deletions

View File

@@ -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)
}

View File

@@ -19,8 +19,25 @@ import (
// Payload represents the structure of the data payload with context information. // Payload represents the structure of the data payload with context information.
type payload struct { type payload struct {
Context struct { Context struct {
Domain string `json:"domain"` Domain string `json:"domain"`
Version string `json:"version"` 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"` } `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)) 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. // Extract domain, version, and endpoint from the payload and uri.
cxtDomain := payloadData.Context.Domain cxtDomain := payloadData.Context.Domain
version := payloadData.Context.Version version := payloadData.Context.Version
@@ -111,6 +132,52 @@ func (v *schemaValidator) Validate(ctx context.Context, url *url.URL, data []byt
return nil 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 // 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. // from the specified directory and storing them in a cache indexed by their schema filenames.
func (v *schemaValidator) initialise() error { func (v *schemaValidator) initialise() error {

View File

@@ -1,158 +1,16 @@
package plugin package plugin
import ( import (
<<<<<<< HEAD
"context"
"fmt"
=======
"archive/zip" "archive/zip"
"context" "context"
"fmt" "fmt"
"io" "io"
"io/fs" "io/fs"
"net/http" "net/http"
>>>>>>> fdec61e90d57d3d82345d023c1a0d33d5a90583b
"os" "os"
"path/filepath" "path/filepath"
"plugin" "plugin"
"strings" "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" "time"
"github.com/beckn/beckn-onix/pkg/log" "github.com/beckn/beckn-onix/pkg/log"
@@ -528,5 +386,4 @@ func unzip(src, dest string) error {
} }
return nil return nil
>>>>>>> fdec61e90d57d3d82345d023c1a0d33d5a90583b
} }