diff --git a/plugins/config.yaml b/plugins/config.yaml index 0feb580..9ba873a 100644 --- a/plugins/config.yaml +++ b/plugins/config.yaml @@ -2,6 +2,5 @@ plugins: validation_plugin: id: tekuriValidator config: - #schema_dir: plugins/schemas/ #approach 1 - schema_dir: plugins/schemas/ #approach 2 - plugin_path: plugins/implementations/ # Path to the directory containing the .so files \ No newline at end of file + schema_dir: plugins/schemas/ + plugin_path: plugins/implementations/ \ No newline at end of file diff --git a/plugins/implementations/plugin_impl.go b/plugins/implementations/plugin_impl.go index 9b584e7..752322e 100644 --- a/plugins/implementations/plugin_impl.go +++ b/plugins/implementations/plugin_impl.go @@ -6,48 +6,56 @@ import ( "fmt" "os" "path/filepath" + "strings" "beckn-onix/plugins" "github.com/santhosh-tekuri/jsonschema/v6" ) -// tekuriValidator implements the Validator interface using the santhosh-tekuri/jsonschema package. -type tekuriValidator struct { +// TekuriValidator implements the Validator interface using the santhosh-tekuri/jsonschema package. +type TekuriValidator struct { schema *jsonschema.Schema } -type tekuriValidatorProvider struct { - schemaCache map[string]map[string]*jsonschema.Schema - //schemaCache map[string]*jsonschema.Schema +// TekuriValidatorProvider is responsible for managing and providing access to the JSON schema validators. +type TekuriValidatorProvider struct { + SchemaCache map[string]*jsonschema.Schema } // Validate validates the given data against the schema. -func (v *tekuriValidator) Validate(ctx context.Context, data []byte) error { +func (v *TekuriValidator) Validate(ctx context.Context, data []byte) error { var jsonData interface{} if err := json.Unmarshal(data, &jsonData); err != nil { return err } err := v.schema.Validate(jsonData) if err != nil { + // TODO: Integrate with the logging module once it is ready fmt.Printf("Validation error: %v\n", err) } return err } -// (Approach 2)(all json files for each schema from sub directories) -func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plugins.Validator, error) { - vp.schemaCache = make(map[string]map[string]*jsonschema.Schema) +// Initialize initializes 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 (vp *TekuriValidatorProvider) Initialize(schemaDir string) (map[string]plugins.Validator, error) { + // Initialize the SchemaCache map to store the compiled schemas using a unique key (domain/version/schema). + vp.SchemaCache = make(map[string]*jsonschema.Schema) + // Initialize the validatorCache map to store the Validator instances associated with each schema. validatorCache := make(map[string]plugins.Validator) + compiler := jsonschema.NewCompiler() + // Walk through the schema directory and process each file. err := filepath.Walk(schemaDir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } + + // Only process files (ignore directories) and ensure the file has a ".json" extension. if !info.IsDir() && filepath.Ext(info.Name()) == ".json" { - filePath := filepath.Join(schemaDir, info.Name()) - compiler := jsonschema.NewCompiler() compiledSchema, err := compiler.Compile(path) if err != nil { return fmt.Errorf("failed to compile JSON schema from file %s: %v", info.Name(), err) @@ -56,12 +64,31 @@ func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plug return fmt.Errorf("compiled schema is nil for file %s", info.Name()) } - dir := filepath.Base(filepath.Dir(filePath)) - if vp.schemaCache[dir] == nil { - vp.schemaCache[dir] = make(map[string]*jsonschema.Schema) + // 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", info.Name(), err) } - vp.schemaCache[dir][info.Name()] = compiledSchema - validatorCache[info.Name()] = &tekuriValidator{schema: compiledSchema} + // 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. + domain := parts[0] + version := parts[1] + schemaFileName := parts[2] + + // Construct a unique key combining domain, version, and schema name (e.g., ondc_trv10/v2.0.0/schema.json). + uniqueKey := fmt.Sprintf("%s/%s/%s", domain, version, schemaFileName) + // Store the compiled schema in the SchemaCache using the unique key. + vp.SchemaCache[uniqueKey] = compiledSchema + // Store the corresponding validator in the validatorCache using the same unique key. + validatorCache[uniqueKey] = &TekuriValidator{schema: compiledSchema} + } return nil }) @@ -72,110 +99,11 @@ func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plug return validatorCache, nil } -var _ plugins.ValidatorProvider = (*tekuriValidatorProvider)(nil) +var _ plugins.ValidatorProvider = (*TekuriValidatorProvider)(nil) -var providerInstance = &tekuriValidatorProvider{} +var providerInstance = &TekuriValidatorProvider{} // GetProvider returns the ValidatorProvider instance. func GetProvider() plugins.ValidatorProvider { return providerInstance } - -/////////////////////// -//(Approach 2)(all json files for each schema) - -// Initialize reads all .json files from the given schema directory, validates them using JSON Schema, and prints the result. -// func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plugins.Validator, error) { -// //start := time.Now() -// // Initialize the cache -// vp.schemaCache = make(map[string]map[string]*jsonschema.Schema) -// validatorCache := make(map[string]plugins.Validator) - -// // Read the directory -// files, err := ioutil.ReadDir(schemaDir) -// if err != nil { -// return nil, fmt.Errorf("failed to read schema directory: %v", err) -// } - -// for _, file := range files { -// if filepath.Ext(file.Name()) == ".json" { -// // Read the JSON file -// filePath := filepath.Join(schemaDir, file.Name()) -// fmt.Println("file path : ", filePath) -// compiler := jsonschema.NewCompiler() -// compiledSchema, err := compiler.Compile(filePath) -// if err != nil { -// return nil, fmt.Errorf("failed to compile JSON schema from file %s: %v", file.Name(), err) -// } -// if compiledSchema == nil { -// return nil, fmt.Errorf("compiled schema is nil for file %s", file.Name()) -// } -// // Extract directory and filename to use in the nested map -// dir := filepath.Base(filepath.Dir(filePath)) -// if vp.schemaCache[dir] == nil { -// vp.schemaCache[dir] = make(map[string]*jsonschema.Schema) -// } -// // Store the compiled schema in the nested cache -// vp.schemaCache[dir][file.Name()] = compiledSchema - -// validatorCache[file.Name()] = &tekuriValidator{schema: compiledSchema} -// } -// } -// // fmt.Printf("initialize executed in %s\n", time.Since(start)) - -// return validatorCache, nil - -////////////////////////////////////////////////////////// -// (Approach 1) -// func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plugins.Validator, error) { -// // start := time.Now() -// vp.schemaCache = make(map[string]*jsonschema.Schema) -// validatorCache := make(map[string]plugins.Validator) - -// files, err := ioutil.ReadDir(schemaDir) -// if err != nil { -// return nil, fmt.Errorf("failed to read schema directory: %w", err) -// } -// for _, file := range files { -// if filepath.Ext(file.Name()) == ".json" { -// filePath := filepath.Join(schemaDir, file.Name()) -// // Read the file content -// content, err := ioutil.ReadFile(filePath) -// if err != nil { -// return nil, fmt.Errorf("failed to read file %s: %v", filePath, err) -// } - -// var schemaDoc map[string]interface{} -// if err := json.Unmarshal(content, &schemaDoc); err != nil { -// return nil, fmt.Errorf("failed to unmarshal JSON schema from file %s: %v", filePath, err) -// } - -// if defs, exists := schemaDoc["$defs"]; exists { -// defsMap := defs.(map[string]interface{}) - -// for name, defSchema := range defsMap { -// _, err := json.Marshal(defSchema) -// if err != nil { -// return nil, fmt.Errorf("failed to marshal schema definition %s: %v", name, err) -// } - -// compiler := jsonschema.NewCompiler() -// if err := compiler.AddResource(name, filepath.Dir(filePath)); err != nil { -// return nil, fmt.Errorf("failed to add resource for schema definition %s: %v", name, err) -// } - -// compiledSchema, err := compiler.Compile(filePath) -// if err != nil { -// return nil, fmt.Errorf("failed to compile schema definition: %v", err) -// } - -// schemaKey := fmt.Sprintf("%s.%s", file.Name(), name) -// vp.schemaCache[schemaKey] = compiledSchema -// validatorCache[schemaKey] = &tekuriValidator{schema: compiledSchema} -// } -// } -// } -// } -// // fmt.Printf("Initialize executed in %s\n", time.Since(start)) -// return validatorCache, nil -// } diff --git a/plugins/implementations/plugin_impl_test.go b/plugins/implementations/plugin_impl_test.go index 0be16d3..d924691 100644 --- a/plugins/implementations/plugin_impl_test.go +++ b/plugins/implementations/plugin_impl_test.go @@ -20,7 +20,7 @@ type Context struct{} type Message struct{} func TestInitializeValidDirectory(t *testing.T) { - provider := &tekuriValidatorProvider{} + provider := &TekuriValidatorProvider{} schemaDir := "../schemas/" _, err := provider.Initialize(schemaDir) if err != nil { @@ -29,7 +29,7 @@ func TestInitializeValidDirectory(t *testing.T) { } func TestInitializeInValidDirectory(t *testing.T) { - provider := &tekuriValidatorProvider{} + provider := &TekuriValidatorProvider{} schemaDir := "../schema/ondc_trv10_2.0.0/" _, err := provider.Initialize(schemaDir) if err != nil { @@ -42,7 +42,7 @@ func TestInvalidCompileFile(t *testing.T) { if _, err := os.Stat(schemaDir); os.IsNotExist(err) { t.Fatalf("Schema directory does not exist: %v", schemaDir) } - provider := &tekuriValidatorProvider{} + provider := &TekuriValidatorProvider{} compiledSchema, err := provider.Initialize(schemaDir) if err != nil { t.Fatalf("failed to compile JSON schema : %v", err) @@ -58,7 +58,7 @@ func TestInvalidCompileSchema(t *testing.T) { if _, err := os.Stat(schemaDir); os.IsNotExist(err) { t.Fatalf("Schema directory does not exist: %v", schemaDir) } - provider := &tekuriValidatorProvider{} + provider := &TekuriValidatorProvider{} compiledSchema, _ := provider.Initialize(schemaDir) fmt.Println(compiledSchema) if compiledSchema == nil { @@ -72,15 +72,15 @@ func TestValidateData(t *testing.T) { if _, err := os.Stat(schemaDir); os.IsNotExist(err) { t.Fatalf("Schema directory does not exist: %v", schemaDir) } - provider := &tekuriValidatorProvider{} + provider := &TekuriValidatorProvider{} validators, err := provider.Initialize(schemaDir) if err != nil { t.Fatalf("Failed to initialize schema provider: %v", err) } - var validator *tekuriValidator + var validator *TekuriValidator for _, v := range validators { var ok bool - validator, ok = v.(*tekuriValidator) + validator, ok = v.(*TekuriValidator) if ok { break } @@ -89,7 +89,7 @@ func TestValidateData(t *testing.T) { t.Fatalf("No validators found in the map") } - payloadFilePath := "../test/payload.json" + payloadFilePath := "../testData/payload.json" payloadData, err := ioutil.ReadFile(payloadFilePath) if err != nil { t.Fatalf("Failed to read payload data: %v", err) @@ -114,15 +114,15 @@ func TestInValidateData(t *testing.T) { t.Fatalf("Schema directory does not exist: %v", schemaDir) } - provider := &tekuriValidatorProvider{} + provider := &TekuriValidatorProvider{} validators, err := provider.Initialize(schemaDir) if err != nil { t.Fatalf("Failed to initialize schema provider: %v", err) } - var validator *tekuriValidator + var validator *TekuriValidator for _, v := range validators { var ok bool - validator, ok = v.(*tekuriValidator) + validator, ok = v.(*TekuriValidator) if ok { break } @@ -144,15 +144,15 @@ func TestInValidateUnmarshalData(t *testing.T) { t.Fatalf("Schema directory does not exist: %v", schemaDir) } - provider := &tekuriValidatorProvider{} + provider := &TekuriValidatorProvider{} validators, err := provider.Initialize(schemaDir) if err != nil { t.Fatalf("Failed to initialize schema provider: %v", err) } - var validator *tekuriValidator + var validator *TekuriValidator for _, v := range validators { var ok bool - validator, ok = v.(*tekuriValidator) + validator, ok = v.(*TekuriValidator) if ok { break } diff --git a/plugins/implementations/tekuriValidator.so b/plugins/implementations/tekuriValidator.so index 6db56d2..bb02825 100644 Binary files a/plugins/implementations/tekuriValidator.so and b/plugins/implementations/tekuriValidator.so differ diff --git a/plugins/schemas.zip b/plugins/schemas.zip new file mode 100644 index 0000000..0c9583d Binary files /dev/null and b/plugins/schemas.zip differ diff --git a/plugins/testData/cancel.json b/plugins/testData/cancel.json new file mode 100644 index 0000000..51c66e3 --- /dev/null +++ b/plugins/testData/cancel.json @@ -0,0 +1,31 @@ +{ + "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/plugins/testData/confirm.json b/plugins/testData/confirm.json new file mode 100644 index 0000000..b5a0e21 --- /dev/null +++ b/plugins/testData/confirm.json @@ -0,0 +1,153 @@ +{ + "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/plugins/test/payload.json b/plugins/testData/search.json similarity index 99% rename from plugins/test/payload.json rename to plugins/testData/search.json index a911a8d..282f81f 100644 --- a/plugins/test/payload.json +++ b/plugins/testData/search.json @@ -1,4 +1,3 @@ - { "context": { "action": "search", @@ -79,7 +78,3 @@ } } } - - - - diff --git a/plugins/testData/select.json b/plugins/testData/select.json new file mode 100644 index 0000000..a391e6e --- /dev/null +++ b/plugins/testData/select.json @@ -0,0 +1,352 @@ +{ + "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/test.go b/test.go index cc732bc..de64fdc 100644 --- a/test.go +++ b/test.go @@ -2,6 +2,7 @@ package main import ( "beckn-onix/plugins" + "encoding/json" "fmt" "io/ioutil" "log" @@ -40,11 +41,35 @@ func validateHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, "Failed to parse request URL", http.StatusBadRequest) return } - // Ensure endpoint trimming to avoid leading slashes - endpoint := strings.Trim(u.Path, "/") - schemaFileName := fmt.Sprintf("%s.json", endpoint) + payloadData, err := ioutil.ReadAll(r.Body) + if err != nil { + http.Error(w, "Failed to read payload data", http.StatusInternalServerError) + return + } + + // Initialize an instance of Payload struct + var payload Payload + err1 := json.Unmarshal(payloadData, &payload) + if err1 != nil { + http.Error(w, "Failed to parse JSON payload", http.StatusBadRequest) + return + } + + // Extract the domain, version, and endpoint from the payload and URL + domain := payload.Context.Domain + version := payload.Context.Version + version = fmt.Sprintf("v%s", version) + + endpoint := strings.Trim(u.Path, "/") fmt.Println("Handling request for endpoint:", endpoint) + domain = strings.ToLower(domain) + domain = strings.ReplaceAll(domain, ":", "_") + + schemaFileName := fmt.Sprintf("%s/%s/%s.json", domain, version, endpoint) + fmt.Println("printing schema file name : ", schemaFileName) + + // schemaFileName := fmt.Sprintf("%s.json", endpoint) pluginsConfig, err := plugins.LoadPluginsConfig("plugins/config.yaml") if err != nil { @@ -64,12 +89,6 @@ func validateHandler(w http.ResponseWriter, r *http.Request) { return } - payloadData, err := ioutil.ReadAll(r.Body) - if err != nil { - http.Error(w, "Failed to read payload data", http.StatusInternalServerError) - return - } - ctx := r.Context() err = validator.Validate(ctx, payloadData) if err != nil {