Response Error struct added

This commit is contained in:
tanyamadaan
2025-03-19 14:23:44 +05:30
parent 84501b92f4
commit d422743dcc
14 changed files with 35 additions and 928 deletions

6
go.mod
View File

@@ -1,16 +1,16 @@
module github.com/beckn/beckn-onix module github.com/beckn/beckn-onix
go 1.23.4 go 1.23.4
toolchain go1.23.7 toolchain go1.23.7
require ( require (
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
gopkg.in/yaml.v2 v2.4.0
golang.org/x/crypto v0.36.0 golang.org/x/crypto v0.36.0
gopkg.in/yaml.v2 v2.4.0
) )
require ( require (
golang.org/x/sys v0.31.0 // indirect golang.org/x/sys v0.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 golang.org/x/text v0.23.0 // indirect
golang.org/x/text v0.14.0 // indirect
) )

View File

@@ -2,37 +2,15 @@ package definition
import ( import (
"context" "context"
"fmt"
"net/url" "net/url"
"strings"
) )
// SchemaValError represents a single schema validation failure. // SchemaValidator interface for schema validation.
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 { type SchemaValidator interface {
Validate(ctx context.Context, url *url.URL, payload []byte) error Validate(ctx context.Context, url *url.URL, payload []byte) error
} }
// ValidatorProvider interface for creating validators. // SchemaValidatorProvider interface for creating validators.
type SchemaValidatorProvider interface { type SchemaValidatorProvider interface {
New(ctx context.Context, config map[string]string) (SchemaValidator, func() error, error) 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

@@ -15,8 +15,8 @@ import (
// Config represents the plugin manager configuration. // Config represents the plugin manager configuration.
type Config struct { type Config struct {
Root string `yaml:"root"` Root string `yaml:"root"`
Validator PluginConfig `yaml:"schema_validator"` SchemaValidator PluginConfig `yaml:"schema_validator"`
} }
// PluginConfig represents configuration details for a plugin. // PluginConfig represents configuration details for a plugin.
@@ -46,8 +46,7 @@ type SchemaDetails struct {
// Manager handles dynamic plugin loading and management. // Manager handles dynamic plugin loading and management.
type Manager struct { type Manager struct {
vp definition.SchemaValidatorProvider vp definition.SchemaValidatorProvider
// validators definition.Validator
cfg *Config cfg *Config
} }
@@ -57,8 +56,8 @@ func NewManager(ctx context.Context, cfg *Config) (*Manager, error) {
return nil, fmt.Errorf("configuration cannot be nil") return nil, fmt.Errorf("configuration cannot be nil")
} }
// Load validator plugin // Load schema validator plugin
vp, err := provider[definition.SchemaValidatorProvider](cfg.Root, cfg.Validator.ID) vp, err := provider[definition.SchemaValidatorProvider](cfg.Root, cfg.SchemaValidator.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)
} }
@@ -115,17 +114,17 @@ func pluginPath(path, id string) string {
} }
// Validators retrieves the validation plugin instances. // Validators retrieves the validation plugin instances.
func (m *Manager) Validator(ctx context.Context) (definition.SchemaValidator, func() error, error) { func (m *Manager) SchemaValidator(ctx context.Context) (definition.SchemaValidator, func() error, error) {
if m.vp == nil { if m.vp == nil {
return nil, nil, fmt.Errorf("validator plugin provider not loaded") return nil, nil, fmt.Errorf("schema validator plugin provider not loaded")
} }
validator, close, err := m.vp.New(ctx, m.cfg.Validator.Config) schemaValidator, close, err := m.vp.New(ctx, m.cfg.SchemaValidator.Config)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("failed to initialize validator: %v", err) return nil, nil, fmt.Errorf("failed to initialize schema validator: %v", err)
} }
return validator, close, nil return schemaValidator, close, nil
} }
// LoadConfig loads the configuration from a YAML file. // LoadConfig loads the configuration from a YAML file.

View File

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

View File

@@ -1,31 +0,0 @@
{
"context": {
"action": "cancel",
"bap_id": "example-bap.com",
"bap_uri": "https://example-bap.com/prod/trv10",
"bpp_id": "api.beckn.juspay.in/dobpp/beckn/7f7896dd-787e-4a0b-8675-e9e6fe93bb8f",
"bpp_uri": "https://example-bpp.com/prod/seller",
"domain": "ONDC:TRV10",
"location": {
"city": {
"code": "std:080"
},
"country": {
"code": "IND"
}
},
"message_id": "be6a495a-e941-4fbf-9d59-f1e6166cccc8",
"timestamp": "2023-03-23T05:15:08Z",
"transaction_id": "870782be-6757-43f1-945c-8eeaf9536259",
"ttl": "PT30S",
"version": "2.0.0"
},
"message": {
"cancellation_reason_id": "7",
"descriptor": {
"code": "SOFT_CANCEL",
"name": "Ride Cancellation"
},
"order_id": "O1"
}
}

View File

@@ -1,153 +0,0 @@
{
"context": {
"action": "confirm",
"bap_id": "example-bap.com",
"bap_uri": "https://example-bap.com/prod/trv10",
"bpp_id": "example-bpp.com",
"bpp_uri": "https://example-bpp.com/prod/seller",
"domain": "ONDC:TRV10",
"location": {
"city": {
"code": "std:080"
},
"country": {
"code": "IND"
}
},
"message_id": "6743e9e2-4fb5-487c-92b7-13ba8018f176",
"timestamp": "2023-12-10T04:34:48.031Z",
"transaction_id": "870782be-6757-43f1-945c-8eeaf9536259",
"ttl": "PT30S",
"version": "2.0.0"
},
"message": {
"order": {
"billing": {
"name": "Joe Adams"
},
"fulfillments": [
{
"customer": {
"contact": {
"phone": "9876556789"
},
"person": {
"name": "Joe Adams"
}
},
"id": "F1",
"stops": [
{
"location": {
"gps": "13.008935, 77.644408"
},
"type": "START"
},
{
"location": {
"gps": "12.971186, 77.586812"
},
"type": "END"
}
],
"vehicle": {
"category": "AUTO_RICKSHAW"
}
}
],
"items": [
{
"id": "I1"
}
],
"payments": [
{
"collected_by": "BPP",
"id": "PA1",
"params": {
"bank_account_number": "xxxxxxxxxxxxxx",
"bank_code": "XXXXXXXX",
"virtual_payment_address": "9988199772@okicic"
},
"status": "NOT-PAID",
"tags": [
{
"descriptor": {
"code": "BUYER_FINDER_FEES"
},
"display": false,
"list": [
{
"descriptor": {
"code": "BUYER_FINDER_FEES_PERCENTAGE"
},
"value": "1"
}
]
},
{
"descriptor": {
"code": "SETTLEMENT_TERMS"
},
"display": false,
"list": [
{
"descriptor": {
"code": "SETTLEMENT_WINDOW"
},
"value": "PT60M"
},
{
"descriptor": {
"code": "SETTLEMENT_BASIS"
},
"value": "DELIVERY"
},
{
"descriptor": {
"code": "SETTLEMENT_TYPE"
},
"value": "UPI"
},
{
"descriptor": {
"code": "MANDATORY_ARBITRATION"
},
"value": "true"
},
{
"descriptor": {
"code": "COURT_JURISDICTION"
},
"value": "New Delhi"
},
{
"descriptor": {
"code": "DELAY_INTEREST"
},
"value": "5"
},
{
"descriptor": {
"code": "STATIC_TERMS"
},
"value": "https://example-test-bap.com/static-terms.txt"
},
{
"descriptor": {
"code": "SETTLEMENT_AMOUNT"
},
"value": "1.46"
}
]
}
],
"type": "ON-FULFILLMENT"
}
],
"provider": {
"id": "P1"
}
}
}
}

View File

@@ -1,80 +0,0 @@
{
"context": {
"action": "search",
"bap_id": "example-bap.com",
"bap_uri": "https://example-bap.com/prod/trv10",
"domain": "ONDC:TRV10",
"location": {
"city": {
"code": "std:080"
},
"country": {
"code": "IND"
}
},
"message_id": "40963dc1-e402-4f4d-ae70-7c5864ca682c",
"timestamp": "2023-12-09T13:39:56.645Z",
"transaction_id": "870782be-6757-43f1-945c-8eeaf9536259",
"ttl": "PT30S",
"version": "2.0.0"
},
"message": {
"intent": {
"fulfillment": {
"stops": [
{
"location": {
"gps": "13.008935, 77.644408"
},
"type": "START"
},
{
"location": {
"gps": "12.971186, 77.586812"
},
"type": "END"
}
]
},
"payment": {
"collected_by": "BPP",
"tags": [
{
"descriptor": {
"code": "BUYER_FINDER_FEES"
},
"display": false,
"list": [
{
"descriptor": {
"code": "BUYER_FINDER_FEES_PERCENTAGE"
},
"value": "1"
}
]
},
{
"descriptor": {
"code": "SETTLEMENT_TERMS"
},
"display": false,
"list": [
{
"descriptor": {
"code": "DELAY_INTEREST"
},
"value": "5"
},
{
"descriptor": {
"code": "STATIC_TERMS"
},
"value": "https://example-test-bap.com/static-terms.txt"
}
]
}
]
}
}
}
}

View File

@@ -1,63 +0,0 @@
{
"context": {
"action": "search",
"bap_id": "bap.example.com",
"bap_uri": "https://example-bap.com/prod/trv10",
"domain": "example-domain",
"location": {
"city": {
"code": "PUN"
},
"country": {
"code": "IND"
}
},
"message_id": "123e4567-e89b-12d3-a456-426614174000",
"timestamp": "2025-03-06T12:00:00Z",
"transaction_id": "123e4567-e89b-12d3-a456-426614174001",
"ttl": "PT30M",
"version": "1.0.0",
"extra_field": "unexpected_value"
},
"message": {
"intent": {
"fulfillment": {
"stops": [
{
"location": {
"gps": "19.0760,72.8777",
"created_at": "unexpected_value"
},
"type": "START"
},
{
"location": {
"gps": "18.5204,73.8567"
},
"type": "END"
}
]
},
"payment": {
"collected_by": "BPP",
"tags": [
{
"descriptor": {
"code": "some-code"
},
"display": true,
"list": [
{
"descriptor": {
"code": "list-code"
},
"value": "list-value"
}
]
}
]
}
}
}
}

View File

@@ -1,65 +0,0 @@
{
"context": {
"action": "search",
"bap_id": "example-bap.com",
"bap_uri": "https://example-bap.com/prod/trv10",
"domain": "ONDC:TRV10",
"location": {
"city": {
"code": "std:080"
},
"country": {
"code": "IND"
}
},
"message_id": "40963dc1-e402-4f4d-ae70-7c5864ca682c",
"timestamp": "2023-12-09T13:39:56.645Z",
"transaction_id": "870782be-6757-43f1-945c-8eeaf9536259",
"ttl": "PT30S",
"version": "2.0.0"
},
"message": {
"intent": {
"payment": {
"collected_by": "BPP",
"tags": [
{
"descriptor": {
"code": "BUYER_FINDER_FEES"
},
"display": false,
"list": [
{
"descriptor": {
"code": "BUYER_FINDER_FEES_PERCENTAGE"
},
"value": "1"
}
]
},
{
"descriptor": {
"code": "SETTLEMENT_TERMS"
},
"display": false,
"list": [
{
"descriptor": {
"code": "DELAY_INTEREST"
},
"value": "5"
},
{
"descriptor": {
"code": "STATIC_TERMS"
},
"value": "https://example-test-bap.com/static-terms.txt"
}
]
}
]
}
}
}
}

View File

@@ -1,352 +0,0 @@
{
"context": {
"action": "select",
"bap_id": "example-bap.com",
"bap_uri": "https://example-bap.com/prod/trv10",
"bpp_id": "example-bpp.com",
"bpp_uri": "https://example-bpp.com/prod/seller",
"domain": "ONDC:TRV10",
"location": {
"city": {
"code": "std:080"
},
"country": {
"code": "IND"
}
},
"message_id": "8926b747-0362-4fcc-b795-0994a6287700",
"timestamp": "2023-12-09T14:11:32.859Z",
"transaction_id": "870782be-6757-43f1-945c-8eeaf9536259",
"ttl": "PT30S",
"version": "2.0.0"
},
"message": {
"order": {
"cancellation_terms": [
{
"cancellation_fee": {
"percentage": "0"
},
"fulfillment_state": {
"descriptor": {
"code": "RIDE_ASSIGNED"
}
},
"reason_required": true
},
{
"cancellation_fee": {
"amount": {
"currency": "INR",
"value": "30"
}
},
"fulfillment_state": {
"descriptor": {
"code": "RIDE_ENROUTE_PICKUP"
}
},
"reason_required": true
},
{
"cancellation_fee": {
"amount": {
"currency": "INR",
"value": "50"
}
},
"fulfillment_state": {
"descriptor": {
"code": "RIDE_ARRIVED_PICKUP"
}
},
"reason_required": true
},
{
"cancellation_fee": {
"percentage": "100"
},
"fulfillment_state": {
"descriptor": {
"code": "RIDE_STARTED"
}
},
"reason_required": true
}
],
"fulfillments": [
{
"id": "F1",
"customer": {
"contact": {
"phone": "9876556789"
},
"person": {
"name": "Joe Adams"
}
},
"stops": [
{
"location": {
"gps": "13.008935, 77.644408"
},
"type": "START"
},
{
"location": {
"gps": "12.971186, 77.586812"
},
"type": "END"
}
],
"tags": [
{
"descriptor": {
"code": "ROUTE_INFO",
"name": "Route Information"
},
"display": true,
"list": [
{
"descriptor": {
"code": "ENCODED_POLYLINE",
"name": "Path"
},
"value": "_p~iF~ps|U_ulLnnqC_mqNvxq`@"
},
{
"descriptor": {
"code": "WAYPOINTS",
"name": "Waypoints"
},
"value": "[{\"gps\":\"12.909982, 77.611822\"},{\"gps\":\"12.909982,77.611822\"},{\"gps\":\"12.909982,77.611822\"},{\"gps\":\"12.909982, 77.611822\"}]"
}
]
}
],
"type": "DELIVERY",
"vehicle": {
"category": "AUTO_RICKSHAW"
}
}
],
"items": [
{
"descriptor": {
"code": "RIDE",
"name": "Auto Ride"
},
"fulfillment_ids": [
"F1"
],
"id": "I1",
"location_ids": [
"L1",
"L3"
],
"payment_ids": [
"PA1"
],
"price": {
"currency": "INR",
"maximum_value": "176",
"minimum_value": "136",
"value": "146"
},
"tags": [
{
"descriptor": {
"code": "FARE_POLICY",
"name": "Daytime Charges"
},
"display": true,
"list": [
{
"descriptor": {
"code": "MIN_FARE"
},
"value": "30"
},
{
"descriptor": {
"code": "MIN_FARE_DISTANCE_KM"
},
"value": "2"
},
{
"descriptor": {
"code": "PER_KM_CHARGE"
},
"value": "15"
},
{
"descriptor": {
"code": "PICKUP_CHARGE"
},
"value": "10"
},
{
"descriptor": {
"code": "WAITING_CHARGE_PER_MIN"
},
"value": "2"
},
{
"descriptor": {
"code": "NIGHT_CHARGE_MULTIPLIER"
},
"value": "1.5"
},
{
"descriptor": {
"code": "NIGHT_SHIFT_START_TIME"
},
"value": "22:00:00"
},
{
"descriptor": {
"code": "NIGHT_SHIFT_END_TIME"
},
"value": "05:00:00"
}
]
},
{
"descriptor": {
"code": "INFO",
"name": "General Information"
},
"display": true,
"list": [
{
"descriptor": {
"code": "DISTANCE_TO_NEAREST_DRIVER_METER"
},
"value": "661"
},
{
"descriptor": {
"code": "ETA_TO_NEAREST_DRIVER_MIN"
},
"value": "3"
}
]
}
]
}
],
"payments": [
{
"collected_by": "BPP",
"id": "PA1",
"params": {
"bank_account_number": "xxxxxxxxxxxxxx",
"bank_code": "XXXXXXXX",
"virtual_payment_address": "9988199772@okicic"
},
"status": "NOT-PAID",
"tags": [
{
"descriptor": {
"code": "BUYER_FINDER_FEES"
},
"display": false,
"list": [
{
"descriptor": {
"code": "BUYER_FINDER_FEES_PERCENTAGE"
},
"value": "1"
}
]
},
{
"descriptor": {
"code": "SETTLEMENT_TERMS"
},
"display": false,
"list": [
{
"descriptor": {
"code": "DELAY_INTEREST"
},
"value": "5"
},
{
"descriptor": {
"code": "SETTLEMENT_TYPE"
},
"value": "UPI"
},
{
"descriptor": {
"code": "SETTLEMENT_WINDOW"
},
"value": "PT2D"
},
{
"descriptor": {
"code": "SETTLEMENT_BASIS"
},
"value": "DELIVERY"
},
{
"descriptor": {
"code": "MANDATORY_ARBITRATION"
},
"value": "true"
},
{
"descriptor": {
"code": "COURT_JURISDICTION"
},
"value": "New Delhi"
},
{
"descriptor": {
"code": "STATIC_TERMS"
},
"value": "https://example-test-bpp.com/static-terms.txt"
},
{
"descriptor": {
"code": "SETTLEMENT_AMOUNT"
},
"value": "1.46"
}
]
}
],
"type": "ON-FULFILLMENT"
}
],
"provider": {
"id": "P1"
},
"quote": {
"breakup": [
{
"price": {
"currency": "INR",
"value": "30"
},
"title": "BASE_FARE"
},
{
"price": {
"currency": "INR",
"value": "116"
},
"title": "DISTANCE_FARE"
}
],
"price": {
"currency": "INR",
"value": "146"
},
"ttl": "PT30S"
}
}
}
}

View File

@@ -1,129 +0,0 @@
{
"$id": "http://example.com/schema/searchSchema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"context": {
"type": "object",
"properties": {
"action": { "type": "string" },
"bap_id": { "type": "string" },
"bap_uri": { "type": "string", "format": "uri" },
"domain": { "type": "string" },
"location": {
"type": "object",
"properties": {
"city": {
"type": "object",
"properties": {
"code": { "type": "string" }
},
"required": ["code"]
},
"country": {
"type": "object",
"properties": {
"code": { "type": "string" }
},
"required": ["code"]
}
},
"required": ["city", "country"]
},
"message_id": { "type": "string", "format": "uuid" },
"timestamp": { "type": "string", "format": "date-time" },
"transaction_id": { "type": "string", "format": "uuid" },
"ttl": { "type": "string" },
"version": { "type": "string" }
},
"required": [
"action",
"bap_id",
"bap_uri",
"domain",
"location",
"message_id",
"timestamp",
"transaction_id",
"ttl",
"version"
]
},
"message": {
"type": "object",
"properties": {
"intent": {
"type": "object",
"properties": {
"fulfillment": {
"type": "object",
"properties": {
"stops": {
"type": "array",
"items": {
"type": "object",
"properties": {
"location": {
"type": "object",
"properties": {
"gps": { "type": "string", "pattern": "^\\d{1,3}\\.\\d+,\\s?\\d{1,3}\\.\\d+$" }
},
"required": ["gps"]
},
"type": { "type": "string", "enum": ["START", "END"] }
},
"required": ["location", "type"]
}
}
},
"required": ["stops"]
},
"payment": {
"type": "object",
"properties": {
"collected_by": { "type": "string", "enum": ["BPP", "BAP"] },
"tags": {
"type": "array",
"items": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "type": "string" }
},
"required": ["code"]
},
"display": { "type": "boolean" },
"list": {
"type": "array",
"items": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "type": "string" }
},
"required": ["code"]
},
"value": { "type": "string" }
},
"required": ["descriptor", "value"]
}
}
},
"required": ["descriptor", "display", "list"]
}
}
},
"required": ["collected_by", "tags"]
}
},
"required": ["fulfillment", "payment"]
}
}
}
},
"required": ["context", "message"]
}

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings"
) )
type ErrorType string type ErrorType string
@@ -23,6 +24,20 @@ type Error struct {
Paths string `json:"paths,omitempty"` Paths string `json:"paths,omitempty"`
} }
// SchemaValidationErr represents a collection of schema validation failures.
type SchemaValidationErr struct {
Errors []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.Paths, err.Message))
}
return strings.Join(errorMessages, "; ")
}
type Message struct { type Message struct {
Ack struct { Ack struct {
Status string `json:"status,omitempty"` Status string `json:"status,omitempty"`

18
test.go
View File

@@ -7,7 +7,6 @@ import (
"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/definition"
@@ -66,7 +65,7 @@ func main() {
} }
// Get the validator. // Get the validator.
validator, _, defErr := manager.Validator(context.Background()) validator, _, defErr := manager.SchemaValidator(context.Background())
if defErr != nil { if defErr != nil {
log.Fatalf("Failed to get validators: %v", defErr) log.Fatalf("Failed to get validators: %v", defErr)
} }
@@ -115,19 +114,8 @@ func validateHandler(w http.ResponseWriter, r *http.Request, validators definiti
// } // }
validationErr := validators.Validate(ctx, u, payloadData) validationErr := validators.Validate(ctx, u, payloadData)
if validationErr != nil { if validationErr != nil {
// Check if the error is of type SchemaValidationErr // Handle other types of errors
if schemaErr, ok := validationErr.(*definition.SchemaValidationErr); ok { http.Error(w, fmt.Sprintf("Schema validation failed: %v", validationErr), http.StatusBadRequest)
// 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)
if _, err := w.Write([]byte("Schema validation succeeded!")); err != nil { if _, err := w.Write([]byte("Schema validation succeeded!")); err != nil {