Schema validator plugin

This commit is contained in:
tanyamadaan
2025-03-19 10:52:42 +05:30
parent d86d23d43d
commit ef816b6e5f
24 changed files with 735 additions and 543 deletions

10
.gitignore vendored
View File

@@ -129,3 +129,13 @@ dist
.yarn/build-state.yml .yarn/build-state.yml
.yarn/install-state.gz .yarn/install-state.gz
.pnp.* .pnp.*
# Ignore compiled shared object files
*.so
# Ignore coverage output files
coverage.out
coverage.html
# Ignore the schema directory used for testing
/schemas/

2
go.mod
View File

@@ -1,4 +1,4 @@
module beckn-onix module github.com/beckn/beckn-onix
go 1.23.4 go 1.23.4

View File

@@ -0,0 +1,38 @@
package definition
import (
"context"
"fmt"
"net/url"
"strings"
)
// SchemaValError represents a single schema validation failure.
type SchemaValError struct {
Path string
Message string
}
// SchemaValidationErr represents a collection of schema validation failures.
type SchemaValidationErr struct {
Errors []SchemaValError
}
// Validator interface for schema validation.
type SchemaValidator interface {
Validate(ctx context.Context, url *url.URL, payload []byte) error
}
// ValidatorProvider interface for creating validators.
type SchemaValidatorProvider interface {
New(ctx context.Context, config map[string]string) (SchemaValidator, func() error, error)
}
// Error implements the error interface for SchemaValidationErr.
func (e *SchemaValidationErr) Error() string {
var errorMessages []string
for _, err := range e.Errors {
errorMessages = append(errorMessages, fmt.Sprintf("%s: %s", err.Path, err.Message))
}
return strings.Join(errorMessages, "; ")
}

View File

@@ -0,0 +1,33 @@
package main
import (
"context"
"errors"
definition "github.com/beckn/beckn-onix/pkg/plugin/definition"
validator "github.com/beckn/beckn-onix/pkg/plugin/implementation/schemaValidator"
)
// ValidatorProvider provides instance of Validator.
type schemaValidatorProvider struct{}
// New initializes a new Verifier instance.
func (vp schemaValidatorProvider) New(ctx context.Context, config map[string]string) (definition.SchemaValidator, func() error, error) {
if ctx == nil {
return nil, nil, errors.New("context cannot be nil")
}
// Extract schema_dir from the config map
schemaDir, ok := config["schema_dir"]
if !ok || schemaDir == "" {
return nil, nil, errors.New("config must contain 'schema_dir'")
}
// Create a new Validator instance with the provided configuration
return validator.New(ctx, &validator.Config{
SchemaDir: schemaDir, // Pass the schemaDir to the validator.Config
})
}
// Provider is the exported symbol that the plugin manager will look for.
var Provider definition.SchemaValidatorProvider = &schemaValidatorProvider{}

View File

@@ -0,0 +1,148 @@
package main
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
// setupTestSchema creates a temporary directory and writes a sample schema file.
func setupTestSchema(t *testing.T) string {
t.Helper()
// Create a temporary directory for the schema
schemaDir, err := os.MkdirTemp("", "schemas")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
// Create the directory structure for the schema file
schemaFilePath := filepath.Join(schemaDir, "example", "1.0", "test_schema.json")
if err := os.MkdirAll(filepath.Dir(schemaFilePath), 0755); err != nil {
t.Fatalf("Failed to create schema directory structure: %v", err)
}
// Define a sample schema
schemaContent := `{
"type": "object",
"properties": {
"context": {
"type": "object",
"properties": {
"domain": {"type": "string"},
"version": {"type": "string"}
},
"required": ["domain", "version"]
}
},
"required": ["context"]
}`
// Write the schema to the file
if err := os.WriteFile(schemaFilePath, []byte(schemaContent), 0644); err != nil {
t.Fatalf("Failed to write schema file: %v", err)
}
return schemaDir
}
// TestValidatorProviderSuccess tests successful ValidatorProvider implementation.
func TestValidatorProviderSuccess(t *testing.T) {
schemaDir := setupTestSchema(t)
defer os.RemoveAll(schemaDir)
// Define test cases.
tests := []struct {
name string
ctx context.Context
config map[string]string
expectedError string
}{
{
name: "Valid schema directory",
ctx: context.Background(), // Valid context
config: map[string]string{"schema_dir": schemaDir},
expectedError: "",
},
}
// Test using table-driven tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vp := schemaValidatorProvider{}
validator, close, err := vp.New(tt.ctx, tt.config)
// Ensure no error occurred
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
// Ensure the validator is not nil
if validator == nil {
t.Error("expected a non-nil validator, got nil")
}
// Ensure the close function is not nil
if close == nil {
t.Error("expected a non-nil close function, got nil")
}
// Test the close function
if err := close(); err != nil {
t.Errorf("close function returned an error: %v", err)
}
})
}
}
// TestValidatorProviderSuccess tests cases where ValidatorProvider creation should fail.
func TestValidatorProviderFailure(t *testing.T) {
schemaDir := setupTestSchema(t)
defer os.RemoveAll(schemaDir)
// Define test cases.
tests := []struct {
name string
ctx context.Context
config map[string]string
expectedError string
}{
{
name: "Invalid schema directory",
ctx: context.Background(), // Valid context
config: map[string]string{"schema_dir": "/invalid/dir"},
expectedError: "failed to initialise validator: schema directory does not exist: /invalid/dir",
},
{
name: "Nil context",
ctx: nil, // Nil context
config: map[string]string{"schema_dir": schemaDir},
expectedError: "context cannot be nil",
},
}
// Test using table-driven tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vp := schemaValidatorProvider{}
_, _, err := vp.New(tt.ctx, tt.config)
// Check for expected error
if tt.expectedError != "" {
if err == nil || !strings.Contains(err.Error(), tt.expectedError) {
t.Errorf("expected error %q, got %v", tt.expectedError, err)
}
return
}
// Ensure no error occurred
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
})
}
}

View File

@@ -0,0 +1,201 @@
package schemaValidator
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strings"
definition "github.com/beckn/beckn-onix/pkg/plugin/definition"
"github.com/santhosh-tekuri/jsonschema/v6"
)
// Payload represents the structure of the data payload with context information.
type payload struct {
Context struct {
Domain string `json:"domain"`
Version string `json:"version"`
} `json:"context"`
}
// Validator implements the Validator interface.
type SchemaValidator struct {
config *Config
schemaCache map[string]*jsonschema.Schema
}
type Config struct {
SchemaDir string
}
// New creates a new ValidatorProvider instance.
func New(ctx context.Context, config *Config) (*SchemaValidator, func() error, error) {
// Check if config is nil
if config == nil {
return nil, nil, fmt.Errorf("config cannot be nil")
}
v := &SchemaValidator{
config: config,
schemaCache: make(map[string]*jsonschema.Schema),
}
// Call Initialise function to load schemas and get validators
if err := v.Initialise(); err != nil {
return nil, nil, fmt.Errorf("failed to initialise validator: %v", err)
}
return v, v.Close, nil
}
// Validate validates the given data against the schema.
func (v *SchemaValidator) Validate(ctx context.Context, url *url.URL, data []byte) error {
var payloadData payload
err := json.Unmarshal(data, &payloadData)
if err != nil {
return fmt.Errorf("failed to parse JSON payload: %v", err)
}
// Extract domain, version, and endpoint from the payload and uri.
cxt_domain := payloadData.Context.Domain
version := payloadData.Context.Version
version = fmt.Sprintf("v%s", version)
endpoint := path.Base(url.String())
// ToDo Add debug log here
fmt.Println("Handling request for endpoint:", endpoint)
domain := strings.ToLower(cxt_domain)
domain = strings.ReplaceAll(domain, ":", "_")
// Construct the schema file name.
schemaFileName := fmt.Sprintf("%s_%s_%s", domain, version, endpoint)
// Retrieve the schema from the cache.
schema, exists := v.schemaCache[schemaFileName]
if !exists {
return fmt.Errorf("schema not found for domain: %s", schemaFileName)
}
var jsonData any
if err := json.Unmarshal(data, &jsonData); err != nil {
return fmt.Errorf("failed to parse JSON data: %v", err)
}
err = schema.Validate(jsonData)
if err != nil {
// Handle schema validation errors
if validationErr, ok := err.(*jsonschema.ValidationError); ok {
// Convert validation errors into an array of SchemaValError
var schemaErrors []definition.SchemaValError
for _, cause := range validationErr.Causes {
// Extract the path and message from the validation error
path := strings.Join(cause.InstanceLocation, ".") // JSON path to the invalid field
message := cause.Error() // Validation error message
// Append the error to the schemaErrors array
schemaErrors = append(schemaErrors, definition.SchemaValError{
Path: path,
Message: message,
})
}
// Return the array of schema validation errors
return &definition.SchemaValidationErr{Errors: schemaErrors}
}
// Return a generic error for non-validation errors
return fmt.Errorf("validation failed: %v", err)
}
// Return nil if validation succeeds
return nil
}
// ValidatorProvider provides instances of Validator.
type ValidatorProvider struct{}
// 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 {
schemaDir := v.config.SchemaDir
// Check if the directory exists and is accessible.
info, err := os.Stat(schemaDir)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("schema directory does not exist: %s", schemaDir)
}
return fmt.Errorf("failed to access schema directory: %v", err)
}
if !info.IsDir() {
return fmt.Errorf("provided schema path is not a directory: %s", schemaDir)
}
compiler := jsonschema.NewCompiler()
// Helper function to process directories recursively.
var processDir func(dir string) error
processDir = func(dir string) error {
entries, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("failed to read directory: %v", err)
}
for _, entry := range entries {
path := filepath.Join(dir, entry.Name())
if entry.IsDir() {
// Recursively process subdirectories.
if err := processDir(path); err != nil {
return err
}
} else if filepath.Ext(entry.Name()) == ".json" {
// Process JSON files.
compiledSchema, err := compiler.Compile(path)
if err != nil {
return fmt.Errorf("failed to compile JSON schema from file %s: %v", entry.Name(), err)
}
// Use relative path from schemaDir to avoid absolute paths and make schema keys domain/version specific.
relativePath, err := filepath.Rel(schemaDir, path)
if err != nil {
return fmt.Errorf("failed to get relative path for file %s: %v", entry.Name(), err)
}
// Split the relative path to get domain, version, and schema.
parts := strings.Split(relativePath, string(os.PathSeparator))
// Ensure that the file path has at least 3 parts: domain, version, and schema file.
if len(parts) < 3 {
return fmt.Errorf("invalid schema file structure, expected domain/version/schema.json but got: %s", relativePath)
}
// Extract domain, version, and schema filename from the parts.
// Validate that the extracted parts are non-empty.
domain := strings.TrimSpace(parts[0])
version := strings.TrimSpace(parts[1])
schemaFileName := strings.TrimSpace(parts[2])
schemaFileName = strings.TrimSuffix(schemaFileName, ".json")
if domain == "" || version == "" || schemaFileName == "" {
return fmt.Errorf("invalid schema file structure, one or more components are empty. Relative path: %s", relativePath)
}
// Construct a unique key combining domain, version, and schema name (e.g., ondc_trv10_v2.0.0_schema).
uniqueKey := fmt.Sprintf("%s_%s_%s", domain, version, schemaFileName)
// Store the compiled schema in the SchemaCache using the unique key.
v.schemaCache[uniqueKey] = compiledSchema
}
}
return nil
}
// Start processing from the root schema directory.
if err := processDir(schemaDir); err != nil {
return fmt.Errorf("failed to read schema directory: %v", err)
}
return nil
}
// Close releases resources (mock implementation returning nil).
func (v *SchemaValidator) Close() error {
return nil
}

View File

@@ -1,17 +1,97 @@
package validator package schemaValidator
import ( import (
"beckn-onix/shared/plugin/definition"
"context" "context"
"fmt"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
"github.com/beckn/beckn-onix/pkg/plugin/definition"
) )
func TestValidator_Validate(t *testing.T) { // setupTestSchema creates a temporary directory and writes a sample schema file.
func setupTestSchema(t *testing.T) string {
t.Helper()
// Create a temporary directory for the schema
schemaDir, err := os.MkdirTemp("", "schemas")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
// Create the directory structure for the schema file
schemaFilePath := filepath.Join(schemaDir, "example", "v1.0", "endpoint.json")
if err := os.MkdirAll(filepath.Dir(schemaFilePath), 0755); err != nil {
t.Fatalf("Failed to create schema directory structure: %v", err)
}
// Define a sample schema
schemaContent := `{
"type": "object",
"properties": {
"context": {
"type": "object",
"properties": {
"domain": {"type": "string"},
"version": {"type": "string"},
"action": {"type": "string"}
},
"required": ["domain", "version", "action"]
}
},
"required": ["context"]
}`
// Write the schema to the file
if err := os.WriteFile(schemaFilePath, []byte(schemaContent), 0644); err != nil {
t.Fatalf("Failed to write schema file: %v", err)
}
return schemaDir
}
func TestValidator_Validate_Success(t *testing.T) {
tests := []struct {
name string
url string
payload string
wantValid bool
}{
{
name: "Valid payload",
url: "http://example.com/endpoint",
payload: `{"context": {"domain": "example", "version": "1.0", "action": "endpoint"}}`,
wantValid: true,
},
}
// Setup a temporary schema directory for testing
schemaDir := setupTestSchema(t)
defer os.RemoveAll(schemaDir)
config := &Config{SchemaDir: schemaDir}
v, _, err := New(context.Background(), config)
if err != nil {
t.Fatalf("Failed to create validator: %v", err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, _ := url.Parse(tt.url)
valid, err := v.Validate(context.Background(), u, []byte(tt.payload))
if err != (definition.SchemaValError{}) {
t.Errorf("Unexpected error: %v", err)
}
if valid != tt.wantValid {
t.Errorf("Error: Validate() returned valid = %v, expected valid = %v", valid, tt.wantValid)
}
})
}
}
func TestValidator_Validate_Failure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
url string url string
@@ -19,13 +99,6 @@ func TestValidator_Validate(t *testing.T) {
wantValid bool wantValid bool
wantErr string wantErr string
}{ }{
{
name: "Valid payload",
url: "http://example.com/endpoint",
payload: `{"context": {"domain": "example", "version": "1.0"}}`,
wantValid: true,
wantErr: "",
},
{ {
name: "Invalid JSON payload", name: "Invalid JSON payload",
url: "http://example.com/endpoint", url: "http://example.com/endpoint",
@@ -36,54 +109,39 @@ func TestValidator_Validate(t *testing.T) {
{ {
name: "Schema validation failure", name: "Schema validation failure",
url: "http://example.com/endpoint", url: "http://example.com/endpoint",
payload: `{"context": {"domain": "invalid", "version": "1.0"}}`, payload: `{"context": {"domain": "example", "version": "1.0"}}`,
wantValid: false, wantValid: false,
wantErr: "Validation failed", wantErr: "Validation failed",
}, },
{
name: "Schema not found",
url: "http://example.com/unknown_endpoint",
payload: `{"context": {"domain": "example", "version": "1.0"}}`,
wantValid: false,
wantErr: "schema not found for domain",
},
} }
// Setup a temporary schema directory for testing // Setup a temporary schema directory for testing
schemaDir := filepath.Join(os.TempDir(), "schemas") schemaDir := setupTestSchema(t)
defer os.RemoveAll(schemaDir) defer os.RemoveAll(schemaDir)
os.MkdirAll(schemaDir, 0755)
// Create a sample schema file config := &Config{SchemaDir: schemaDir}
schemaContent := `{ v, _, err := New(context.Background(), config)
"type": "object", if err != nil {
"properties": {
"context": {
"type": "object",
"properties": {
"domain": {"type": "string"},
"version": {"type": "string"}
},
"required": ["domain", "version"]
}
},
"required": ["context"]
}`
schemaFile := filepath.Join(schemaDir, "example", "1.0", "endpoint.json")
os.MkdirAll(filepath.Dir(schemaFile), 0755)
os.WriteFile(schemaFile, []byte(schemaContent), 0644)
config := map[string]string{"schema_dir": schemaDir}
v, err := New(context.Background(), config)
if err != (definition.Error{}) {
t.Fatalf("Failed to create validator: %v", err) t.Fatalf("Failed to create validator: %v", err)
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
u, _ := url.Parse(tt.url) u, _ := url.Parse(tt.url)
valid, err := v["example_1.0_endpoint"].Validate(context.Background(), u, []byte(tt.payload)) valid, err := v.Validate(context.Background(), u, []byte(tt.payload))
if (err != (definition.Error{}) && !strings.Contains(err.Message, tt.wantErr)) || (err == (definition.Error{}) && tt.wantErr != "") { if (err != (definition.SchemaValError{}) && !strings.Contains(err.Message, tt.wantErr)) || (err == (definition.SchemaValError{}) && tt.wantErr != "") {
t.Errorf("Error: Validate() returned error = %v, expected error = %v", err, tt.wantErr) t.Errorf("Error: Validate() returned error = %v, expected error = %v", err, tt.wantErr)
return return
} }
if valid != tt.wantValid { if valid != tt.wantValid {
t.Errorf("Error: Validate() returned valid = %v, expected valid = %v", valid, tt.wantValid) t.Errorf("Validate() returned valid = %v, expected valid = %v", valid, tt.wantValid)
} else {
t.Logf("Test %s passed: valid = %v", tt.name, valid)
} }
}) })
} }
@@ -99,7 +157,7 @@ func TestValidator_Initialise(t *testing.T) {
name: "Schema directory does not exist", name: "Schema directory does not exist",
setupFunc: func(schemaDir string) error { setupFunc: func(schemaDir string) error {
// Do not create the schema directory // Do not create the schema directory
return fmt.Errorf("schema directory does not exist: %s", schemaDir) return nil
}, },
wantErr: "schema directory does not exist", wantErr: "schema directory does not exist",
@@ -117,7 +175,9 @@ func TestValidator_Initialise(t *testing.T) {
setupFunc: func(schemaDir string) error { setupFunc: func(schemaDir string) error {
// Create an invalid schema file structure // Create an invalid schema file structure
invalidSchemaFile := filepath.Join(schemaDir, "invalid_schema.json") invalidSchemaFile := filepath.Join(schemaDir, "invalid_schema.json")
os.MkdirAll(filepath.Dir(invalidSchemaFile), 0755) if err := os.MkdirAll(filepath.Dir(invalidSchemaFile), 0755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
return os.WriteFile(invalidSchemaFile, []byte(`{}`), 0644) return os.WriteFile(invalidSchemaFile, []byte(`{}`), 0644)
}, },
wantErr: "invalid schema file structure", wantErr: "invalid schema file structure",
@@ -127,7 +187,9 @@ func TestValidator_Initialise(t *testing.T) {
setupFunc: func(schemaDir string) error { setupFunc: func(schemaDir string) error {
// Create a schema file with invalid JSON // Create a schema file with invalid JSON
invalidSchemaFile := filepath.Join(schemaDir, "example", "1.0", "endpoint.json") invalidSchemaFile := filepath.Join(schemaDir, "example", "1.0", "endpoint.json")
os.MkdirAll(filepath.Dir(invalidSchemaFile), 0755) if err := os.MkdirAll(filepath.Dir(invalidSchemaFile), 0755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
return os.WriteFile(invalidSchemaFile, []byte(`{invalid json}`), 0644) return os.WriteFile(invalidSchemaFile, []byte(`{invalid json}`), 0644)
}, },
wantErr: "failed to compile JSON schema", wantErr: "failed to compile JSON schema",
@@ -137,7 +199,9 @@ func TestValidator_Initialise(t *testing.T) {
setupFunc: func(schemaDir string) error { setupFunc: func(schemaDir string) error {
// Create a schema file with empty domain, version, or schema name // Create a schema file with empty domain, version, or schema name
invalidSchemaFile := filepath.Join(schemaDir, "", "1.0", "endpoint.json") invalidSchemaFile := filepath.Join(schemaDir, "", "1.0", "endpoint.json")
os.MkdirAll(filepath.Dir(invalidSchemaFile), 0755) if err := os.MkdirAll(filepath.Dir(invalidSchemaFile), 0755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
return os.WriteFile(invalidSchemaFile, []byte(`{ return os.WriteFile(invalidSchemaFile, []byte(`{
"type": "object", "type": "object",
"properties": { "properties": {
@@ -153,32 +217,27 @@ func TestValidator_Initialise(t *testing.T) {
"required": ["context"] "required": ["context"]
}`), 0644) }`), 0644)
}, },
wantErr: "invalid schema file structure, one or more components are empty", wantErr: "failed to read schema directory: invalid schema file structure, expected domain/version/schema.json but got: 1.0/endpoint.json",
}, },
{ {
name: "Failed to read directory", name: "Failed to read directory",
setupFunc: func(schemaDir string) error { setupFunc: func(schemaDir string) error {
// Create a directory and remove read permissions // Create a directory and remove read permissions
os.MkdirAll(schemaDir, 0000) if err := os.MkdirAll(schemaDir, 0000); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
return nil return nil
}, },
wantErr: "failed to read directory", wantErr: "failed to read directory",
}, },
{
name: "Failed to access schema directory",
setupFunc: func(schemaDir string) error {
// Create a directory and remove access permissions
os.MkdirAll(schemaDir, 0000)
return nil
},
wantErr: "failed to access schema directory",
},
{ {
name: "Valid schema directory", name: "Valid schema directory",
setupFunc: func(schemaDir string) error { setupFunc: func(schemaDir string) error {
// Create a valid schema file // Create a valid schema file
validSchemaFile := filepath.Join(schemaDir, "example", "1.0", "endpoint.json") validSchemaFile := filepath.Join(schemaDir, "example", "1.0", "endpoint.json")
os.MkdirAll(filepath.Dir(validSchemaFile), 0755) if err := os.MkdirAll(filepath.Dir(validSchemaFile), 0755); err != nil {
t.Fatalf("Failed to create directory: %v", err)
}
return os.WriteFile(validSchemaFile, []byte(`{ return os.WriteFile(validSchemaFile, []byte(`{
"type": "object", "type": "object",
"properties": { "properties": {
@@ -209,13 +268,13 @@ func TestValidator_Initialise(t *testing.T) {
t.Fatalf("setupFunc() error = %v", err) t.Fatalf("setupFunc() error = %v", err)
} }
config := map[string]string{"schema_dir": schemaDir} config := &Config{SchemaDir: schemaDir}
v := &Validator{config: config} v := &Validator{config: config}
_, err := v.Initialise() err := v.Initialise()
if (err != (definition.Error{}) && !strings.Contains(err.Message, tt.wantErr)) || (err == (definition.Error{}) && tt.wantErr != "") { if (err != nil && !strings.Contains(err.Error(), tt.wantErr)) || (err == nil && tt.wantErr != "") {
t.Errorf("Error: Initialise() returned error = %v, expected error = %v", err, tt.wantErr) t.Errorf("Error: Initialise() returned error = %v, expected error = %v", err, tt.wantErr)
} else if err == (definition.Error{}) { } else if err == nil {
t.Logf("Test %s passed: validator initialized successfully", tt.name) t.Logf("Test %s passed: validator initialized successfully", tt.name)
} else { } else {
t.Logf("Test %s passed with expected error: %v", tt.name, err) t.Logf("Test %s passed with expected error: %v", tt.name, err)
@@ -224,68 +283,78 @@ func TestValidator_Initialise(t *testing.T) {
} }
} }
func TestValidator_New(t *testing.T) { func TestValidator_New_Success(t *testing.T) {
schemaDir := setupTestSchema(t)
defer os.RemoveAll(schemaDir)
config := &Config{SchemaDir: schemaDir}
_, _, err := New(context.Background(), config)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
func TestValidator_New_Failure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
config map[string]string config *Config
setupFunc func(schemaDir string) error setupFunc func(schemaDir string) error
wantErr string wantErr string
}{ }{
{
name: "Config is nil",
config: nil,
setupFunc: func(schemaDir string) error {
return nil
},
wantErr: "config cannot be nil",
},
{
name: "Config is empty",
config: &Config{},
setupFunc: func(schemaDir string) error {
return nil
},
wantErr: "config must contain 'schema_dir'",
},
{
name: "schema_dir is empty",
config: &Config{SchemaDir: ""},
setupFunc: func(schemaDir string) error {
return nil
},
wantErr: "config must contain 'schema_dir'",
},
{ {
name: "Failed to initialise validators", name: "Failed to initialise validators",
config: map[string]string{ config: &Config{
"schema_dir": "/invalid/path", SchemaDir: "/invalid/path",
}, },
setupFunc: func(schemaDir string) error { setupFunc: func(schemaDir string) error {
// Do not create the schema directory // Do not create the schema directory
return nil return nil
}, },
wantErr: "failed to initialise validators", wantErr: "failed to initialise validator",
},
{
name: "Valid initialisation",
config: map[string]string{
"schema_dir": "/valid/path",
},
setupFunc: func(schemaDir string) error {
// Create a valid schema directory and file
validSchemaFile := filepath.Join(schemaDir, "example", "1.0", "endpoint.json")
os.MkdirAll(filepath.Dir(validSchemaFile), 0755)
return os.WriteFile(validSchemaFile, []byte(`{
"type": "object",
"properties": {
"context": {
"type": "object",
"properties": {
"domain": {"type": "string"},
"version": {"type": "string"}
},
"required": ["domain", "version"]
}
},
"required": ["context"]
}`), 0644)
},
wantErr: "",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Setup a temporary schema directory for testing // Run the setup function if provided
schemaDir := tt.config["schema_dir"] if tt.setupFunc != nil {
defer os.RemoveAll(schemaDir) schemaDir := ""
if tt.config != nil {
// Run the setup function to prepare the test case schemaDir = tt.config.SchemaDir
}
if err := tt.setupFunc(schemaDir); err != nil { if err := tt.setupFunc(schemaDir); err != nil {
t.Fatalf("setupFunc() error = %v", err) t.Fatalf("Setup function failed: %v", err)
}
} }
_, err := New(context.Background(), tt.config) // Call the New function with the test config
if (err != (definition.Error{}) && !strings.Contains(err.Message, tt.wantErr)) || (err == (definition.Error{}) && tt.wantErr != "") { _, _, err := New(context.Background(), tt.config)
if (err != nil && !strings.Contains(err.Error(), tt.wantErr)) || (err == nil && tt.wantErr != "") {
t.Errorf("Error: New() returned error = %v, expected error = %v", err, tt.wantErr) t.Errorf("Error: New() returned error = %v, expected error = %v", err, tt.wantErr)
} else if err == (definition.Error{}) {
t.Logf("Test %s passed: validator initialized successfully", tt.name)
} else { } else {
t.Logf("Test %s passed with expected error: %v", tt.name, err) t.Logf("Test %s passed with expected error: %v", tt.name, err)
} }

View File

@@ -1,7 +1,6 @@
package plugin package plugin
import ( import (
"beckn-onix/shared/plugin/definition"
"context" "context"
"fmt" "fmt"
"os" "os"
@@ -9,32 +8,46 @@ import (
"plugin" "plugin"
"strings" "strings"
"github.com/beckn/beckn-onix/pkg/plugin/definition"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
// ValidationPluginConfig represents configuration details for a plugin. // Config represents the plugin manager configuration.
type ValidationPluginConfig struct { type Config struct {
ID string `yaml:"id"` Root string `yaml:"root"`
Schema SchemaDetails `yaml:"config"` Validator PluginConfig `yaml:"schema_validator"`
PluginPath string `yaml:"plugin_path"`
} }
// 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. // SchemaDetails contains information about the plugin schema directory.
type SchemaDetails struct { type SchemaDetails struct {
SchemaDir string `yaml:"schema_dir"` SchemaDir string `yaml:"schema_dir"`
} }
// Config represents the configuration for the application, including plugin configurations. // // Config represents the configuration for the application, including plugin configurations.
type Config struct { // type Config struct {
Plugins struct { // Plugins struct {
ValidationPlugin ValidationPluginConfig `yaml:"validation_plugin"` // ValidationPlugin ValidationPluginConfig `yaml:"validation_plugin"`
} `yaml:"plugins"` // } `yaml:"plugins"`
} // }
// Manager handles dynamic plugin loading and management. // Manager handles dynamic plugin loading and management.
type Manager struct { type Manager struct {
vp definition.ValidatorProvider vp definition.SchemaValidatorProvider
validators map[string]definition.Validator // validators definition.Validator
cfg *Config cfg *Config
} }
@@ -45,7 +58,7 @@ func NewManager(ctx context.Context, cfg *Config) (*Manager, error) {
} }
// Load validator plugin // Load validator plugin
vp, err := provider[definition.ValidatorProvider](cfg.Plugins.ValidationPlugin.PluginPath, cfg.Plugins.ValidationPlugin.ID) vp, err := provider[definition.SchemaValidatorProvider](cfg.Root, cfg.Validator.ID)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to load validator plugin: %w", err) return nil, fmt.Errorf("failed to load validator plugin: %w", err)
} }
@@ -53,21 +66,21 @@ func NewManager(ctx context.Context, cfg *Config) (*Manager, error) {
return nil, fmt.Errorf("validator provider is nil") return nil, fmt.Errorf("validator provider is nil")
} }
// Initialize validator // // Initialize validator
validatorMap, defErr := vp.New(ctx, map[string]string{ // validatorMap, defErr := vp.New(ctx, map[string]string{
"schema_dir": cfg.Plugins.ValidationPlugin.Schema.SchemaDir, // "schema_dir": cfg.Plugins.ValidationPlugin.Schema.SchemaDir,
}) // })
if defErr != (definition.Error{}) { // if defErr != nil {
return nil, fmt.Errorf("failed to initialize validator: %v", defErr) // return nil, fmt.Errorf("failed to initialize validator: %v", defErr)
} // }
// Initialize the validators map // // Initialize the validators map
validators := make(map[string]definition.Validator) // validators := make(map[string]definition.Validator)
for key, validator := range validatorMap { // for key, validator := range validatorMap {
validators[key] = validator // validators[key] = validator
} // }
return &Manager{vp: vp, validators: validators, cfg: cfg}, nil return &Manager{vp: vp, cfg: cfg}, nil
} }
// provider loads a plugin dynamically and retrieves its provider instance. // provider loads a plugin dynamically and retrieves its provider instance.
@@ -96,27 +109,23 @@ func provider[T any](path string, id string) (T, error) {
return *prov, nil return *prov, nil
} }
// pluginPath constructs the path to the plugin shared object file. // pluginPath constructs the path to the plugin pkg object file.
func pluginPath(path, id string) string { func pluginPath(path, id string) string {
return filepath.Join(path, id+".so") return filepath.Join(path, id+".so")
} }
// Validators retrieves the validation plugin instances. // Validators retrieves the validation plugin instances.
func (m *Manager) Validators(ctx context.Context) (map[string]definition.Validator, definition.Error) { func (m *Manager) Validator(ctx context.Context) (definition.SchemaValidator, func() error, error) {
if m.vp == nil { if m.vp == nil {
return nil, definition.Error{Message: "validator plugin provider not loaded"} return nil, nil, fmt.Errorf("validator plugin provider not loaded")
} }
configMap := map[string]string{ validator, close, err := m.vp.New(ctx, m.cfg.Validator.Config)
"schema_dir": m.cfg.Plugins.ValidationPlugin.Schema.SchemaDir, if err != nil {
}
_, err := m.vp.New(ctx, configMap)
if err != (definition.Error{}) {
return nil, definition.Error{Message: fmt.Sprintf("failed to initialize validator: %v", err)} return nil, nil, fmt.Errorf("failed to initialize validator: %v", err)
} }
return validator, close, nil
return m.validators, definition.Error{}
} }
// LoadConfig loads the configuration from a YAML file. // LoadConfig loads the configuration from a YAML file.

6
pkg/plugin/plugin.yaml Normal file
View File

@@ -0,0 +1,6 @@
root: pkg/plugin/implementation/
schema_validator:
id: validator
config:
schema_dir: schemas #Directory where the schema files are stored
plugin_path: pkg/plugin/implementations/

View File

@@ -1,22 +0,0 @@
package definition
import (
"context"
"net/url"
)
// Error struct for validation errors
type Error struct {
Path string
Message string
}
// Validator interface for schema validation
type Validator interface {
Validate(ctx context.Context, url *url.URL, payload []byte) (bool, Error)
}
// ValidatorProvider interface for creating validators
type ValidatorProvider interface {
New(ctx context.Context, config map[string]string) (map[string]Validator, Error)
}

View File

@@ -1,25 +0,0 @@
package main
import (
"context"
"beckn-onix/shared/plugin/definition"
"beckn-onix/shared/plugin/implementations/validator"
)
// ValidatorProvider provides instances of Validator.
type ValidatorProvider struct{}
// New initializes a new Verifier instance.
func (vp ValidatorProvider) New(ctx context.Context, config map[string]string) (map[string]definition.Validator, definition.Error) {
// Create a new Validator instance with the provided configuration
validators, err := validator.New(ctx, config)
if err != (definition.Error{}) {
return nil, definition.Error{Path: "", Message: err.Message}
}
return validators, definition.Error{}
}
// Provider is the exported symbol that the plugin manager will look for.
var Provider definition.ValidatorProvider = &ValidatorProvider{}

View File

@@ -1,107 +0,0 @@
package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"testing"
"beckn-onix/shared/plugin/definition"
)
// MockValidator is a mock implementation of the Validator interface for testing.
type MockValidator struct{}
func (m *MockValidator) Validate(ctx context.Context, u *url.URL, data []byte) (bool, definition.Error) {
return true, definition.Error{}
}
// Mock New function for testing
func MockNew(ctx context.Context, config map[string]string) (map[string]definition.Validator, error) {
// If the config has the error flag set to "true", return an error
if config["error"] == "true" {
return nil, errors.New("mock error")
}
// If schema_dir is set, print it out for debugging purposes
if schemaDir, ok := config["schema_dir"]; ok {
// You could add more logic to handle the schema_dir, for now we just print it
fmt.Println("Using schema directory:", schemaDir)
}
// Return a map of mock validators
return map[string]definition.Validator{
"validator1": &MockValidator{},
"validator2": &MockValidator{},
}, nil
}
// New method for ValidatorProvider, uses MockNew for creating mock validators
func New(ctx context.Context, config map[string]string) (map[string]definition.Validator, definition.Error) {
validators, err := MockNew(ctx, config)
if err != nil {
return nil, definition.Error{Message: err.Error()}
}
return validators, definition.Error{}
}
func TestValidatorProvider(t *testing.T) {
// Create a temporary directory for the schema
schemaDir, err := ioutil.TempDir("", "schemas")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(schemaDir)
// Create a temporary JSON schema file
schemaFile := fmt.Sprintf("%s/test_schema.json", schemaDir)
schemaContent := `{"type": "object", "properties": {"name": {"type": "string"}}}`
if err := ioutil.WriteFile(schemaFile, []byte(schemaContent), 0644); err != nil {
t.Fatalf("Failed to write schema file: %v", err)
}
// Define test cases
tests := []struct {
name string
config map[string]string
expectedError string
expectedCount int
}{
{
name: "Valid schema directory",
config: map[string]string{"schema_dir": schemaDir}, // Use schemaDir instead of tempDir
expectedError: "",
expectedCount: 2, // Expecting 2 mock validators
},
{
name: "Invalid schema directory",
config: map[string]string{"schema_dir": "/invalid/dir"},
expectedError: "failed to initialise validators: {/invalid/dir schema directory does not exist}",
expectedCount: 0,
},
}
// Test using table-driven tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vp := ValidatorProvider{}
validators, err := vp.New(context.Background(), tt.config)
// Check for expected error
if tt.expectedError != "" {
if err == (definition.Error{}) || err.Message != tt.expectedError {
t.Errorf("expected error %q, got %v", tt.expectedError, err)
}
return
}
// Check for expected number of validators
if len(validators) != tt.expectedCount {
t.Errorf("expected %d validators, got %d", tt.expectedCount, len(validators))
}
})
}
}

View File

@@ -1,166 +0,0 @@
package validator
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"beckn-onix/shared/plugin/definition"
"github.com/santhosh-tekuri/jsonschema/v6"
)
// Payload represents the structure of the data payload with context information.
type Payload struct {
Context struct {
Domain string `json:"domain"`
Version string `json:"version"`
} `json:"context"`
}
// Validator implements the Validator interface.
type Validator struct {
config map[string]string
schema *jsonschema.Schema
SchemaCache map[string]*jsonschema.Schema
}
// New creates a new ValidatorProvider instance.
func New(ctx context.Context, config map[string]string) (map[string]definition.Validator, definition.Error) {
v := &Validator{config: config}
// Call Initialise function to load schemas and get validators
validators, err := v.Initialise()
if err != (definition.Error{}) {
return nil, definition.Error{Message: fmt.Sprintf("failed to initialise validators: %v", err)}
}
return validators, definition.Error{}
}
// Validate validates the given data against the schema.
func (v *Validator) Validate(ctx context.Context, url *url.URL, payload []byte) (bool, definition.Error) {
var payloadData Payload
err := json.Unmarshal(payload, &payloadData)
if err != nil {
return false, definition.Error{Path: "", Message: fmt.Sprintf("failed to parse JSON payload: %v", err)}
}
// Extract domain, version, and endpoint from the payload and uri
domain := payloadData.Context.Domain
version := payloadData.Context.Version
version = fmt.Sprintf("v%s", version)
endpoint := path.Base(url.String())
fmt.Println("Handling request for endpoint:", endpoint)
domain = strings.ToLower(domain)
domain = strings.ReplaceAll(domain, ":", "_")
var jsonData interface{}
if err := json.Unmarshal(payload, &jsonData); err != nil {
return false, definition.Error{Path: "", Message: err.Error()}
}
err = v.schema.Validate(jsonData)
if err != nil {
// TODO: Integrate with the logging module once it is ready
return false, definition.Error{Path: "", Message: fmt.Sprintf("Validation failed: %v", err)}
}
return true, definition.Error{}
}
// ValidatorProvider provides instances of Validator.
type ValidatorProvider struct{}
// Initialise initialises the validator provider by compiling all the JSON schema files
// from the specified directory and storing them in a cache. It returns a map of validators
// indexed by their schema filenames.
func (v *Validator) Initialise() (map[string]definition.Validator, definition.Error) {
// Initialize SchemaCache as an empty Map if it's nil
if v.SchemaCache == nil {
v.SchemaCache = make(map[string]*jsonschema.Schema)
}
schemaDir := v.config["schema_dir"]
// Check if the directory exists and is accessible
info, err := os.Stat(schemaDir)
if err != nil {
if os.IsNotExist(err) {
return nil, definition.Error{Path: schemaDir, Message: "schema directory does not exist"}
}
return nil, definition.Error{Path: schemaDir, Message: fmt.Sprintf("failed to access schema directory: %v", err)}
}
if !info.IsDir() {
return nil, definition.Error{Path: schemaDir, Message: "provided schema path is not a directory"}
}
// Initialize the validatorCache map to store the Validator instances associated with each schema.
validatorCache := make(map[string]definition.Validator)
compiler := jsonschema.NewCompiler()
// Helper function to process directories recursively
var processDir func(dir string) definition.Error
processDir = func(dir string) definition.Error {
entries, err := os.ReadDir(dir)
if err != nil {
return definition.Error{Path: dir, Message: fmt.Sprintf("failed to read directory: %v", err)}
}
for _, entry := range entries {
path := filepath.Join(dir, entry.Name())
if entry.IsDir() {
// Recursively process subdirectories
if err := processDir(path); err != (definition.Error{}) {
return err
}
} else if filepath.Ext(entry.Name()) == ".json" {
// Process JSON files
compiledSchema, err := compiler.Compile(path)
if err != nil {
return definition.Error{Path: path, Message: fmt.Sprintf("failed to compile JSON schema from file %s: %v", entry.Name(), err)}
}
// Use relative path from schemaDir to avoid absolute paths and make schema keys domain/version specific.
relativePath, err := filepath.Rel(schemaDir, path)
if err != nil {
return definition.Error{Path: path, Message: fmt.Sprintf("failed to get relative path for file %s: %v", entry.Name(), err)}
}
// Split the relative path to get domain, version, and schema.
parts := strings.Split(relativePath, string(os.PathSeparator))
// Ensure that the file path has at least 3 parts: domain, version, and schema file.
if len(parts) < 3 {
return definition.Error{Path: relativePath, Message: "invalid schema file structure, expected domain/version/schema.json"}
}
// Extract domain, version, and schema filename from the parts.
// Validate that the extracted parts are non-empty
domain := strings.TrimSpace(parts[0])
version := strings.TrimSpace(parts[1])
schemaFileName := strings.TrimSpace(parts[2])
schemaFileName = strings.TrimSuffix(schemaFileName, ".json")
if domain == "" || version == "" || schemaFileName == "" {
return definition.Error{Path: relativePath, Message: "invalid schema file structure, one or more components are empty"}
}
// Construct a unique key combining domain, version, and schema name (e.g., ondc_trv10_v2.0.0_schema).
uniqueKey := fmt.Sprintf("%s_%s_%s", domain, version, schemaFileName)
// Store the compiled schema in the SchemaCache using the unique key.
v.SchemaCache[uniqueKey] = compiledSchema
// Store the corresponding validator in the validatorCache using the same unique key.
validatorCache[uniqueKey] = &Validator{schema: compiledSchema}
}
}
return definition.Error{}
}
// Start processing from the root schema directory
if err := processDir(schemaDir); err != (definition.Error{}) {
return nil, definition.Error{Path: schemaDir, Message: fmt.Sprintf("failed to read schema directory: %v", err)}
}
return validatorCache, definition.Error{}
}

View File

@@ -1,6 +0,0 @@
plugins:
validation_plugin:
id: validator
config:
schema_dir: #Directory where the schema files are stored
plugin_path: shared/plugin/implementations/

148
test.go
View File

@@ -1,77 +1,78 @@
package main package main
import ( import (
"beckn-onix/shared/plugin"
"beckn-onix/shared/plugin/definition"
"context" "context"
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"github.com/beckn/beckn-onix/pkg/plugin/definition"
"github.com/beckn/beckn-onix/pkg/plugin"
) )
var ( var (
manager *plugin.Manager manager *plugin.Manager
validators map[string]definition.Validator
) )
// Payload represents the structure of the payload with context information. // Payload represents the structure of the payload with context information.
type Payload struct { // type Payload struct {
Context struct { // Context struct {
Action string `json:"action"` // Action string `json:"action"`
BapID string `json:"bap_id"` // BapID string `json:"bap_id"`
BapURI string `json:"bap_uri"` // BapURI string `json:"bap_uri"`
BppID string `json:"bpp_id"` // BppID string `json:"bpp_id"`
BppURI string `json:"bpp_uri"` // BppURI string `json:"bpp_uri"`
Domain string `json:"domain"` // Domain string `json:"domain"`
Location struct { // Location struct {
City struct { // City struct {
Code string `json:"code"` // Code string `json:"code"`
} `json:"city"` // } `json:"city"`
Country struct { // Country struct {
Code string `json:"code"` // Code string `json:"code"`
} `json:"country"` // } `json:"country"`
} `json:"location"` // } `json:"location"`
MessageID string `json:"message_id"` // MessageID string `json:"message_id"`
Timestamp string `json:"timestamp"` // Timestamp string `json:"timestamp"`
TransactionID string `json:"transaction_id"` // TransactionID string `json:"transaction_id"`
TTL string `json:"ttl"` // TTL string `json:"ttl"`
Version string `json:"version"` // Version string `json:"version"`
} `json:"context"` // } `json:"context"`
Message struct { // Message struct {
CancellationReasonID string `json:"cancellation_reason_id"` // CancellationReasonID string `json:"cancellation_reason_id"`
Descriptor struct { // Descriptor struct {
Code string `json:"code"` // Code string `json:"code"`
Name string `json:"name"` // Name string `json:"name"`
} `json:"descriptor"` // } `json:"descriptor"`
OrderID string `json:"order_id"` // OrderID string `json:"order_id"`
} `json:"message"` // } `json:"message"`
} // }
func main() { func main() {
var err error var err error
// Load the configuration // Load the configuration.
config, err := plugin.LoadConfig("shared/plugin/plugin.yaml") config, err := plugin.LoadConfig("pkg/plugin/plugin.yaml")
if err != nil { if err != nil {
log.Fatalf("Failed to load plugins configuration: %v", err) log.Fatalf("Failed to load plugins configuration: %v", err)
} }
// Initialize the plugin manager // Initialize the plugin manager.
manager, err = plugin.NewManager(context.Background(), config) manager, err = plugin.NewManager(context.Background(), config)
if err != nil { if err != nil {
log.Fatalf("Failed to create PluginManager: %v", err) log.Fatalf("Failed to create PluginManager: %v", err)
} }
// Get the validators map // Get the validator.
validators, defErr := manager.Validators(context.Background()) validator, _, defErr := manager.Validator(context.Background())
if defErr != (definition.Error{}) { if defErr != nil {
log.Fatalf("Failed to get validators: %v", defErr) log.Fatalf("Failed to get validators: %v", defErr)
} }
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
validateHandler(w, r, validators) validateHandler(w, r, validator)
}) })
fmt.Println("Starting server on port 8084...") fmt.Println("Starting server on port 8084...")
err = http.ListenAndServe(":8084", nil) err = http.ListenAndServe(":8084", nil)
@@ -80,13 +81,13 @@ func main() {
} }
} }
func validateHandler(w http.ResponseWriter, r *http.Request, validators map[string]definition.Validator) { func validateHandler(w http.ResponseWriter, r *http.Request, validators definition.SchemaValidator) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return return
} }
// Extract endpoint from request URL // Extract endpoint from request URL.
requestURL := r.RequestURI requestURL := r.RequestURI
u, err := url.ParseRequestURI(requestURL) u, err := url.ParseRequestURI(requestURL)
if err != nil { if err != nil {
@@ -94,40 +95,43 @@ func validateHandler(w http.ResponseWriter, r *http.Request, validators map[stri
return return
} }
payloadData, err := ioutil.ReadAll(r.Body) payloadData, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
http.Error(w, "Failed to read payload data", http.StatusInternalServerError) http.Error(w, "Failed to read payload data", http.StatusInternalServerError)
return return
} }
var payload Payload
err = json.Unmarshal(payloadData, &payload)
if err != nil {
log.Printf("Failed to parse JSON payload: %v", err)
http.Error(w, fmt.Sprintf("Failed to parse JSON payload: %v", err), http.StatusBadRequest)
return
}
// Validate that the domain and version fields are not empty
if payload.Context.Domain == "" || payload.Context.Version == "" {
http.Error(w, "Invalid payload: domain and version are required fields", http.StatusBadRequest)
return
}
schemaFileName := "ondc_trv10_v2.0.0_cancel"
validator, exists := validators[schemaFileName]
if !exists {
http.Error(w, fmt.Sprintf("Validator not found for %s", schemaFileName), http.StatusNotFound)
return
}
ctx := context.Background() ctx := context.Background()
valid, validationErr := validator.Validate(ctx, u, payloadData) // validationErr := validators.Validate(ctx, u, payloadData)
if validationErr != (definition.Error{}) { // if validationErr != (definition.SchemaValError{}) {
http.Error(w, fmt.Sprintf("Document validation failed: %v", validationErr), http.StatusBadRequest) // http.Error(w, fmt.Sprintf("Document validation failed: %v", validationErr), http.StatusBadRequest)
} else if !valid { // } else if !valid {
http.Error(w, "Document validation failed", http.StatusBadRequest) // http.Error(w, "Document validation failed", http.StatusBadRequest)
// } else {
// w.WriteHeader(http.StatusOK)
// if _, err := w.Write([]byte("Document validation succeeded!")); err != nil {
// log.Fatalf("Failed to write response: %v", err)
// }
// }
validationErr := validators.Validate(ctx, u, payloadData)
if validationErr != nil {
// Check if the error is of type SchemaValidationErr
if schemaErr, ok := validationErr.(*definition.SchemaValidationErr); ok {
// Handle schema validation errors
var errorMessages []string
for _, err := range schemaErr.Errors {
errorMessages = append(errorMessages, fmt.Sprintf("Path: %s, Message: %s", err.Path, err.Message))
}
errorMessage := fmt.Sprintf("Schema validation failed: %s", strings.Join(errorMessages, "; "))
http.Error(w, errorMessage, http.StatusBadRequest)
} else {
// Handle other types of errors
http.Error(w, fmt.Sprintf("Schema validation failed: %v", validationErr), http.StatusBadRequest)
}
} else { } else {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte("Document validation succeeded!")) if _, err := w.Write([]byte("Schema validation succeeded!")); err != nil {
log.Fatalf("Failed to write response: %v", err)
}
} }
} }