From d422743dcca05445c6f2e0067cb23fe3f2768388 Mon Sep 17 00:00:00 2001 From: tanyamadaan Date: Wed, 19 Mar 2025 14:23:44 +0530 Subject: [PATCH] Response Error struct added --- go.mod | 8 +- pkg/plugin/definition/schemaValidator.go | 26 +- pkg/plugin/manager.go | 21 +- pkg/plugin/plugin.yaml | 2 +- pkg/plugin/testData/directory.json | 0 pkg/plugin/testData/payloads/cancel.json | 31 -- pkg/plugin/testData/payloads/confirm.json | 153 -------- pkg/plugin/testData/payloads/search.json | 80 ---- .../testData/payloads/search_extraField.json | 63 ---- .../payloads/search_missingField.json | 65 ---- pkg/plugin/testData/payloads/select.json | 352 ------------------ .../ondc_trv10/v2.0.0/search.json | 129 ------- pkg/response/response.go | 15 + test.go | 18 +- 14 files changed, 35 insertions(+), 928 deletions(-) delete mode 100644 pkg/plugin/testData/directory.json delete mode 100644 pkg/plugin/testData/payloads/cancel.json delete mode 100644 pkg/plugin/testData/payloads/confirm.json delete mode 100644 pkg/plugin/testData/payloads/search.json delete mode 100644 pkg/plugin/testData/payloads/search_extraField.json delete mode 100644 pkg/plugin/testData/payloads/search_missingField.json delete mode 100644 pkg/plugin/testData/payloads/select.json delete mode 100644 pkg/plugin/testData/schema_valid/ondc_trv10/v2.0.0/search.json diff --git a/go.mod b/go.mod index eefc09c..30fc452 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,16 @@ module github.com/beckn/beckn-onix go 1.23.4 + toolchain go1.23.7 require ( github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 - gopkg.in/yaml.v2 v2.4.0 golang.org/x/crypto v0.36.0 + gopkg.in/yaml.v2 v2.4.0 ) require ( golang.org/x/sys v0.31.0 // indirect - gopkg.in/yaml.v2 v2.4.0 - golang.org/x/text v0.14.0 // indirect -) \ No newline at end of file + golang.org/x/text v0.23.0 // indirect +) diff --git a/pkg/plugin/definition/schemaValidator.go b/pkg/plugin/definition/schemaValidator.go index 47aa18c..dc69ca5 100644 --- a/pkg/plugin/definition/schemaValidator.go +++ b/pkg/plugin/definition/schemaValidator.go @@ -2,37 +2,15 @@ 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. +// SchemaValidator interface for schema validation. type SchemaValidator interface { Validate(ctx context.Context, url *url.URL, payload []byte) error } -// ValidatorProvider interface for creating validators. +// SchemaValidatorProvider 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, "; ") -} diff --git a/pkg/plugin/manager.go b/pkg/plugin/manager.go index 9b0c612..2cc76bb 100644 --- a/pkg/plugin/manager.go +++ b/pkg/plugin/manager.go @@ -15,8 +15,8 @@ import ( // Config represents the plugin manager configuration. type Config struct { - Root string `yaml:"root"` - Validator PluginConfig `yaml:"schema_validator"` + Root string `yaml:"root"` + SchemaValidator PluginConfig `yaml:"schema_validator"` } // PluginConfig represents configuration details for a plugin. @@ -46,8 +46,7 @@ type SchemaDetails struct { // Manager handles dynamic plugin loading and management. type Manager struct { - vp definition.SchemaValidatorProvider - // validators definition.Validator + vp definition.SchemaValidatorProvider cfg *Config } @@ -57,8 +56,8 @@ func NewManager(ctx context.Context, cfg *Config) (*Manager, error) { return nil, fmt.Errorf("configuration cannot be nil") } - // Load validator plugin - vp, err := provider[definition.SchemaValidatorProvider](cfg.Root, cfg.Validator.ID) + // 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) } @@ -115,17 +114,17 @@ func pluginPath(path, id string) string { } // 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 { - 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 { - 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. diff --git a/pkg/plugin/plugin.yaml b/pkg/plugin/plugin.yaml index 86947d8..174211b 100644 --- a/pkg/plugin/plugin.yaml +++ b/pkg/plugin/plugin.yaml @@ -1,6 +1,6 @@ root: pkg/plugin/implementation/ schema_validator: - id: validator + id: schemaValidator config: schema_dir: schemas #Directory where the schema files are stored plugin_path: pkg/plugin/implementations/ diff --git a/pkg/plugin/testData/directory.json b/pkg/plugin/testData/directory.json deleted file mode 100644 index e69de29..0000000 diff --git a/pkg/plugin/testData/payloads/cancel.json b/pkg/plugin/testData/payloads/cancel.json deleted file mode 100644 index 51c66e3..0000000 --- a/pkg/plugin/testData/payloads/cancel.json +++ /dev/null @@ -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" - } -} \ No newline at end of file diff --git a/pkg/plugin/testData/payloads/confirm.json b/pkg/plugin/testData/payloads/confirm.json deleted file mode 100644 index b5a0e21..0000000 --- a/pkg/plugin/testData/payloads/confirm.json +++ /dev/null @@ -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" - } - } - } -} \ No newline at end of file diff --git a/pkg/plugin/testData/payloads/search.json b/pkg/plugin/testData/payloads/search.json deleted file mode 100644 index 282f81f..0000000 --- a/pkg/plugin/testData/payloads/search.json +++ /dev/null @@ -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" - } - ] - } - ] - } - } - } -} diff --git a/pkg/plugin/testData/payloads/search_extraField.json b/pkg/plugin/testData/payloads/search_extraField.json deleted file mode 100644 index ef8ad94..0000000 --- a/pkg/plugin/testData/payloads/search_extraField.json +++ /dev/null @@ -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" - } - ] - } - ] - } - } - } - } - \ No newline at end of file diff --git a/pkg/plugin/testData/payloads/search_missingField.json b/pkg/plugin/testData/payloads/search_missingField.json deleted file mode 100644 index 792e533..0000000 --- a/pkg/plugin/testData/payloads/search_missingField.json +++ /dev/null @@ -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" - } - ] - } - ] - } - } - } - } - \ No newline at end of file diff --git a/pkg/plugin/testData/payloads/select.json b/pkg/plugin/testData/payloads/select.json deleted file mode 100644 index a391e6e..0000000 --- a/pkg/plugin/testData/payloads/select.json +++ /dev/null @@ -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" - } - } - } - } - \ No newline at end of file diff --git a/pkg/plugin/testData/schema_valid/ondc_trv10/v2.0.0/search.json b/pkg/plugin/testData/schema_valid/ondc_trv10/v2.0.0/search.json deleted file mode 100644 index 82c1720..0000000 --- a/pkg/plugin/testData/schema_valid/ondc_trv10/v2.0.0/search.json +++ /dev/null @@ -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"] -} diff --git a/pkg/response/response.go b/pkg/response/response.go index 310d06f..7364495 100644 --- a/pkg/response/response.go +++ b/pkg/response/response.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "strings" ) type ErrorType string @@ -23,6 +24,20 @@ type Error struct { 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 { Ack struct { Status string `json:"status,omitempty"` diff --git a/test.go b/test.go index 5d9c813..4239aa4 100644 --- a/test.go +++ b/test.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "net/url" - "strings" "github.com/beckn/beckn-onix/pkg/plugin/definition" @@ -66,7 +65,7 @@ func main() { } // Get the validator. - validator, _, defErr := manager.Validator(context.Background()) + validator, _, defErr := manager.SchemaValidator(context.Background()) if defErr != nil { 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) 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) - } + // Handle other types of errors + http.Error(w, fmt.Sprintf("Schema validation failed: %v", validationErr), http.StatusBadRequest) } else { w.WriteHeader(http.StatusOK) if _, err := w.Write([]byte("Schema validation succeeded!")); err != nil {