update schemas and create a validateHandler function

This commit is contained in:
AshwiniK-protean
2025-03-02 18:44:16 +05:30
parent 4b940d9e51
commit 4559ac3e5f
28 changed files with 3053 additions and 3552 deletions

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
@@ -25,7 +24,6 @@ type tekuriValidatorProvider struct {
// Validate validates the given data against the 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 {
// start := time.Now()
var jsonData interface{} var jsonData interface{}
if err := json.Unmarshal(data, &jsonData); err != nil { if err := json.Unmarshal(data, &jsonData); err != nil {
return err return err
@@ -35,11 +33,55 @@ func (v *tekuriValidator) Validate(ctx context.Context, data []byte) error {
fmt.Printf("Validation error: %v\n", err) fmt.Printf("Validation error: %v\n", err)
} }
// fmt.Printf("validate executed in %s\n", time.Since(start))
return 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)
validatorCache := make(map[string]plugins.Validator)
err := filepath.Walk(schemaDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
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)
}
if compiledSchema == nil {
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)
}
vp.schemaCache[dir][info.Name()] = compiledSchema
validatorCache[info.Name()] = &tekuriValidator{schema: compiledSchema}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to read schema directory: %v", err)
}
return validatorCache, nil
}
var _ plugins.ValidatorProvider = (*tekuriValidatorProvider)(nil)
var providerInstance = &tekuriValidatorProvider{}
// GetProvider returns the ValidatorProvider instance.
func GetProvider() plugins.ValidatorProvider {
return providerInstance
}
///////////////////////
//(Approach 2)(all json files for each schema) //(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. // Initialize reads all .json files from the given schema directory, validates them using JSON Schema, and prints the result.
@@ -82,74 +124,6 @@ func (v *tekuriValidator) Validate(ctx context.Context, data []byte) error {
// // fmt.Printf("initialize executed in %s\n", time.Since(start)) // // fmt.Printf("initialize executed in %s\n", time.Since(start))
// return validatorCache, nil // return validatorCache, nil
// }
// (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)
validatorCache := make(map[string]plugins.Validator)
baseCustomID := "https://core/v1.1.0/"
// Walk through the directory and compile all .json files
err := filepath.Walk(schemaDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(info.Name()) == ".json" {
filePath := filepath.Join(schemaDir, info.Name())
// Construct the CustomID using baseCustomID and the file name
customID := baseCustomID + info.Name()
compiler := jsonschema.NewCompiler()
resource, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open JSON schema file %s: %v", info.Name(), err)
}
defer func() {
if err := resource.Close(); err != nil {
log.Printf("Error closing resource: %v", err)
}
}()
// defer resource.Close()
if err := compiler.AddResource(customID, resource); err != nil {
return fmt.Errorf("failed to add resource for JSON schema file %s and custom id is %s: %v", info.Name(), customID, err)
}
// compiledSchema, err := compiler.Compile(path)
compiledSchema, err := compiler.Compile(customID)
if err != nil {
return fmt.Errorf("failed to compile JSON schema from file %s: %v", info.Name(), err)
}
if compiledSchema == nil {
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)
}
vp.schemaCache[dir][info.Name()] = compiledSchema
validatorCache[info.Name()] = &tekuriValidator{schema: compiledSchema}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to read schema directory: %v", err)
}
return validatorCache, nil
}
var _ plugins.ValidatorProvider = (*tekuriValidatorProvider)(nil)
var providerInstance = &tekuriValidatorProvider{}
// GetProvider returns the ValidatorProvider instance.
func GetProvider() plugins.ValidatorProvider {
return providerInstance
}
////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////
// (Approach 1) // (Approach 1)

View File

@@ -21,7 +21,7 @@ type Message struct{}
func TestInitializeValidDirectory(t *testing.T) { func TestInitializeValidDirectory(t *testing.T) {
provider := &tekuriValidatorProvider{} provider := &tekuriValidatorProvider{}
schemaDir := "../schema_valid/ondc_trv10_2.0.0/" schemaDir := "../schemas/"
_, err := provider.Initialize(schemaDir) _, err := provider.Initialize(schemaDir)
if err != nil { if err != nil {
t.Fatalf("expected no error, got %v", err) t.Fatalf("expected no error, got %v", err)
@@ -68,7 +68,7 @@ func TestInvalidCompileSchema(t *testing.T) {
} }
func TestValidateData(t *testing.T) { func TestValidateData(t *testing.T) {
schemaDir := "../schema_valid/ondc_trv10_2.0.0/" schemaDir := "../schemas/"
if _, err := os.Stat(schemaDir); os.IsNotExist(err) { if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
t.Fatalf("Schema directory does not exist: %v", schemaDir) t.Fatalf("Schema directory does not exist: %v", schemaDir)
} }

View File

@@ -80,8 +80,6 @@ func NewValidatorProvider(pluginsConfig PluginConfig) (*PluginManager, map[strin
log.Fatalf("Failed to initialize validator provider: %v", err) log.Fatalf("Failed to initialize validator provider: %v", err)
} }
fmt.Println("printing validators in new validator provider : ", validator)
var memStatsAfter runtime.MemStats var memStatsAfter runtime.MemStats
runtime.ReadMemStats(&memStatsAfter) runtime.ReadMemStats(&memStatsAfter)
fmt.Printf("Memory allocated during plugin boot-up: %v MiB", (memStatsAfter.Alloc-memStatsBefore.Alloc)/1024/1024) fmt.Printf("Memory allocated during plugin boot-up: %v MiB", (memStatsAfter.Alloc-memStatsBefore.Alloc)/1024/1024)

View File

@@ -22,25 +22,8 @@
] ]
} }
] ]
},
"message": {
"type": "object",
"additionalProperties": false,
"properties": {
"order_id": {
"$ref": "definitions.json#/$defs/Order"
},
"cancellation_reason_id": {
"$ref": "definitions.json#/$defs/Option"
},
"descriptor": {
"$ref": "definitions.json#/$defs/Descriptor"
}
},
"required": [
"order_id"
]
} }
}, },
"required": [ "required": [
"message", "message",

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,7 @@
"$ref": "../../core/v1.1.0/cancel.json#" "$ref": "../../core/v1.1.0/cancel.json#"
}, },
{ {
"$ref": "./init.json#/allOf/2" "$ref": "init.json#/allOf/2"
}, },
{ {
"properties": { "properties": {

View File

@@ -7,15 +7,15 @@
"$ref": "../../core/v1.1.0/confirm.json#" "$ref": "../../core/v1.1.0/confirm.json#"
}, },
{ {
"$ref": "./init.json#/allOf/2" "$ref": "./init.json#/allOf/1"
}, },
{ {
"$ref": "./init.json#/allOf/3" "$ref": "./init.json#/allOf/2"
}, },
{ {
"allOf": [ "allOf": [
{ {
"$ref": "./on_select.json#/allOf/8/allOf/0" "$ref": "./on_select.json#/allOf/5"
}, },
{ {
"properties": { "properties": {
@@ -77,7 +77,7 @@
{ {
"allOf": [ "allOf": [
{ {
"$ref": "./init.json#/allOf/6" "$ref": "./init.json#/allOf/7"
}, },
{ {
"properties": { "properties": {

View File

@@ -2,395 +2,549 @@
"$schema": "https://json-schema.org/draft/2020-12/schema", "$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "init", "$id": "init",
"type": "object", "type": "object",
"allOf": [ "allOf": [
{ "$ref": "../../core/v1.1.0/init.json#" },
{ {
"allOf": [ "$ref": "../../core/v1.1.0/init.json#"
{ "$ref": "./search.json#/properties/context/allOf/0" },
{ "required": ["bpp_id", "bpp_uri"] }
]
}, },
{ {
"properties": { "properties": {
"message": { "context": {
"properties": {
"order": {
"type": "object", "type": "object",
"properties": { "allOf": [
"provider": { {
"type": "object", "$ref": "./search.json#/properties/context/allOf/0"
"properties": {
"id": { "type": "string" }
},
"required": ["id"]
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string" }
}, },
"required": ["id"] {
}
} "required": [
}, "bpp_id",
"required": ["provider", "items"] "bpp_uri"
]
}
]
} }
}
} }
}
},
{ "$ref": "./confirm.json#/allOf/4" },
{ "$ref": "./on_select.json#" },
{
"properties": {
"message": {
"properties": {
"order": {
"required": ["fulfillments"]
}
}
}
}
}, },
{ {
"properties": { "properties": {
"message": { "message": {
"type": "object", "properties": {
"properties": { "order": {
"order": {
"type": "object",
"properties": {
"payments": {
"type": "array",
"items": {
"type": "object",
"properties": {
"params": {
"type": "object", "type": "object",
"properties": { "properties": {
"amount": { "provider": {
"type": "string", "type": "object",
"pattern": "^\\d+(\\.\\d{1,2})?$" "properties": {
} "id": {
} "type": "string"
},
"type": {
"type": "string",
"enum": ["PRE-ORDER", "ON-FULFILLMENT", "POST-FULFILLMENT"]
},
"status": {
"type": "string",
"enum": ["PAID", "NOT-PAID"]
},
"collected_by": {
"type": "string",
"enum": ["BAP", "BPP"]
},
"tags": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"uniqueItems": true,
"items": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"type": "string",
"enum": ["SETTLEMENT_TERMS", "BUYER_FINDER_FEES"]
}
}
}
},
"required": ["descriptor"]
}
}
},
"required": ["type", "status", "collected_by", "tags"],
"allOf": [
{
"if": {
"allOf": [
{ "properties": { "collected_by": { "const": "BAP" } } },
{ "properties": { "type": { "const": "PRE-ORDER" } } }
]
},
"then": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": { "code": { "const": "SETTLEMENT_TERMS" } }
} }
}
}, },
"then": { "required": [
"properties": { "id"
"list": { ]
"allOf": [ },
{ "items": {
"contains": { "type": "array",
"type": "object", "items": {
"properties": { "type": "object",
"descriptor": { "properties": {
"type": "object", "id": {
"properties": { "code": { "const": "STATIC_TERMS" } } "type": "string"
},
"value": {
"type": "string",
"format": "uri"
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": { "code": { "const": "SETTLEMENT_BASIS" } }
},
"value": {
"type": "string",
"enum": ["DELIVERY"]
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": { "code": { "const": "SETTLEMENT_WINDOW" } }
},
"value": {
"type": "string"
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": { "code": { "const": "DELAY_INTEREST" } }
},
"value": {
"type": "string",
"pattern": "^\\d+(\\.\\d{1,2})?$"
}
}
}
} }
] },
} "required": [
} "id"
]
} }
}
} }
}
}
},
{
"if": {
"allOf": [
{ "properties": { "collected_by": { "const": "BPP" } } },
{ "properties": { "type": { "const": "PRE-ORDER" } } }
]
}, },
"then": { "required": [
"properties": { "provider",
"tags": { "items"
"items": { ]
"if": { }
"properties": {
"descriptor": {
"properties": { "code": { "const": "SETTLEMENT_TERMS" } }
}
}
},
"then": {
"properties": {
"list": {
"allOf": [
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": { "code": { "const": "STATIC_TERMS" } }
},
"value": {
"type": "string",
"format": "uri"
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": { "code": { "const": "SETTLEMENT_BASIS" } }
},
"value": {
"type": "string",
"enum": ["DELIVERY"]
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": { "code": { "const": "SETTLEMENT_WINDOW" } }
},
"value": {
"type": "string"
}
}
}
}
]
}
}
}
}
}
}
}
},
{
"if": {
"allOf": [
{ "properties": { "collected_by": { "const": "BPP" } } },
{ "properties": { "type": { "const": "ON-FULFILLMENT" } } }
]
},
"then": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": { "code": { "const": "SETTLEMENT_TERMS" } }
}
}
},
"then": {
"properties": {
"list": {
"allOf": [
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": { "code": { "const": "STATIC_TERMS" } }
},
"value": {
"type": "string",
"format": "uri"
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": { "code": { "const": "SETTLEMENT_BASIS" } }
},
"value": {
"type": "string",
"enum": ["DELIVERY"]
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": { "code": { "const": "SETTLEMENT_WINDOW" } }
},
"value": {
"type": "string"
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": { "code": { "const": "DELAY_INTEREST" } }
},
"value": {
"type": "string",
"pattern": "^\\d+(\\.\\d{1,2})?$"
}
}
}
}
]
}
}
}
}
}
}
}
}
]
}
} }
}
} }
}
} }
}
}, },
{ {
"properties": { "$ref": "./confirm.json#/allOf/4"
"message": { },
"properties": { {
"order": { "$ref": "./on_select.json#"
"properties": { },
"billing": { {
"required": ["name"] "properties": {
"message": {
"properties": {
"order": {
"required": [
"fulfillments"
]
}
}
}
}
},
{
"properties": {
"message": {
"type": "object",
"properties": {
"order": {
"type": "object",
"properties": {
"payments": {
"type": "array",
"items": {
"type": "object",
"properties": {
"params": {
"type": "object",
"properties": {
"amount": {
"type": "string",
"pattern": "^\\d+(\\.\\d{1,2})?$"
}
}
},
"type": {
"type": "string",
"enum": [
"PRE-ORDER",
"ON-FULFILLMENT",
"POST-FULFILLMENT"
]
},
"status": {
"type": "string",
"enum": [
"PAID",
"NOT-PAID"
]
},
"collected_by": {
"type": "string",
"enum": [
"BAP",
"BPP"
]
},
"tags": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"uniqueItems": true,
"items": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"type": "string",
"enum": [
"SETTLEMENT_TERMS",
"BUYER_FINDER_FEES"
]
}
}
}
},
"required": [
"descriptor"
]
}
}
},
"required": [
"type",
"status",
"collected_by",
"tags"
],
"allOf": [
{
"if": {
"allOf": [
{
"properties": {
"collected_by": {
"const": "BAP"
}
}
},
{
"properties": {
"type": {
"const": "PRE-ORDER"
}
}
}
]
},
"then": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": {
"code": {
"const": "SETTLEMENT_TERMS"
}
}
}
}
},
"then": {
"properties": {
"list": {
"allOf": [
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "STATIC_TERMS"
}
}
},
"value": {
"type": "string",
"format": "uri"
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "SETTLEMENT_BASIS"
}
}
},
"value": {
"type": "string",
"enum": [
"DELIVERY"
]
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "SETTLEMENT_WINDOW"
}
}
},
"value": {
"type": "string"
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "DELAY_INTEREST"
}
}
},
"value": {
"type": "string",
"pattern": "^\\d+(\\.\\d{1,2})?$"
}
}
}
}
]
}
}
}
}
}
}
}
},
{
"if": {
"allOf": [
{
"properties": {
"collected_by": {
"const": "BPP"
}
}
},
{
"properties": {
"type": {
"const": "PRE-ORDER"
}
}
}
]
},
"then": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": {
"code": {
"const": "SETTLEMENT_TERMS"
}
}
}
}
},
"then": {
"properties": {
"list": {
"allOf": [
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "STATIC_TERMS"
}
}
},
"value": {
"type": "string",
"format": "uri"
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "SETTLEMENT_BASIS"
}
}
},
"value": {
"type": "string",
"enum": [
"DELIVERY"
]
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "SETTLEMENT_WINDOW"
}
}
},
"value": {
"type": "string"
}
}
}
}
]
}
}
}
}
}
}
}
},
{
"if": {
"allOf": [
{
"properties": {
"collected_by": {
"const": "BPP"
}
}
},
{
"properties": {
"type": {
"const": "ON-FULFILLMENT"
}
}
}
]
},
"then": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": {
"code": {
"const": "SETTLEMENT_TERMS"
}
}
}
}
},
"then": {
"properties": {
"list": {
"allOf": [
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "STATIC_TERMS"
}
}
},
"value": {
"type": "string",
"format": "uri"
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "SETTLEMENT_BASIS"
}
}
},
"value": {
"type": "string",
"enum": [
"DELIVERY"
]
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "SETTLEMENT_WINDOW"
}
}
},
"value": {
"type": "string"
}
}
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": {
"const": "DELAY_INTEREST"
}
}
},
"value": {
"type": "string",
"pattern": "^\\d+(\\.\\d{1,2})?$"
}
}
}
}
]
}
}
}
}
}
}
}
}
]
}
}
}
}
}
}
}
},
{
"properties": {
"message": {
"properties": {
"order": {
"properties": {
"billing": {
"required": [
"name"
]
}
},
"required": [
"billing"
]
}
} }
},
"required": ["billing"]
} }
}
} }
}
} }
] ]
} }

View File

@@ -1,16 +1,13 @@
{ {
"$schema": "https://json-schema.org/draft/2020-12/schema", "$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://ondc.org/trv10/2.0.0/on_cancel", "$id": "on_cancel",
"type": "object", "type": "object",
"allOf": [ "allOf": [
{ {
"$ref": "../../core/v1.1.0/confirm.json#" "$ref": "../../core/v1.1.0/confirm.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/confirm#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
} }
] ]
} }

View File

@@ -1,16 +1,13 @@
{ {
"$schema": "https://json-schema.org/draft/2020-12/schema", "$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://ondc.org/trv10/2.0.0/on_confirm", "$id": "on_confirm",
"type": "object", "type": "object",
"allOf": [ "allOf": [
{ {
"$ref": "../../core/v1.1.0/confirm.json#" "$ref": "../../core/v1.1.0/confirm.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/confirm#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
} }
] ]
} }

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/on_init.json#" "$ref": "../../core/v1.1.0/on_init.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/on_init#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
}, },
{ {
"properties": { "properties": {
@@ -35,7 +32,7 @@
} }
}, },
{ {
"$ref": "./on_select.json#/allOf/5" "$ref": "./confirm.json#/allOf/5"
}, },
{ {
"$ref": "./on_select.json#/allOf/6" "$ref": "./on_select.json#/allOf/6"
@@ -69,7 +66,7 @@
} }
}, },
{ {
"$ref": "./confirm.json#/allOf/5/allOf/1" "$ref": "./confirm.json#/allOf/4/allOf/1"
}, },
{ {
"properties": { "properties": {
@@ -226,7 +223,7 @@
] ]
}, },
{ {
"$ref": "./confirm.json#/allOf/7" "$ref": "./confirm.json#/allOf/6"
}, },
{ {
"allOf": [ "allOf": [

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/confirm.json#" "$ref": "../../core/v1.1.0/confirm.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/confirm#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
} }
] ]
} }

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/on_search.json#" "$ref": "../../core/v1.1.0/on_search.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/on_search#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
}, },
{ {
"properties": { "properties": {

View File

@@ -7,604 +7,16 @@
"$ref": "../../core/v1.1.0/on_select.json#" "$ref": "../../core/v1.1.0/on_select.json#"
}, },
{ {
"$ref": "./init.json#/allOf/2" "$ref": "./init.json#/allOf/1"
}, },
{ {
"properties": { "$ref": "./on_init.json#/allOf/2"
"message": {
"type": "object",
"properties": {
"order": {
"type": "object",
"properties": {
"items": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"id": { "type": "string" },
"descriptor": {
"type": "object",
"properties": {
"name": { "type": "string" },
"code": { "type": "string", "enum": ["RIDE"] }
},
"required": ["code"]
},
"price": {
"type": "object",
"properties": { "value": { "type": "string" } },
"required": ["value"]
},
"fulfillment_ids": { "minItems": 1 },
"location_ids": { "minItems": 1 }
},
"required": ["id", "price", "descriptor"]
}
}
},
"required": ["items"]
}
}
}
}
}, },
{ {
"allOf": [ "$ref": "./confirm.json#/allOf/5"
{
"properties": {
"message": {
"properties": {
"order": {
"properties": {
"items": {
"items": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": {
"code": { "const": "FARE_POLICY" }
}
}
}
},
"then": {
"properties": {
"list": {
"type": "array",
"items": {
"type": "object",
"properties": {
"descriptor": {
"properties": {
"code": {
"type": "string",
"enum": [
"MIN_FARE",
"MIN_FARE_DISTANCE_KM",
"PER_KM_CHARGE",
"PICKUP_CHARGE",
"WAITING_CHARGE_PER_MIN",
"NIGHT_CHARGE_MULTIPLIER",
"NIGHT_SHIFT_START_TIME",
"NIGHT_SHIFT_END_TIME",
"EXTERNAL_REF"
]
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
},
{
"properties": {
"message": {
"properties": {
"order": {
"properties": {
"items": {
"items": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": {
"code": { "const": "FARE_POLICY" }
}
}
}
},
"then": {
"properties": {
"list": {
"allOf": [
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "const": "MIN_FARE" }
},
"required": ["code"]
},
"value": {
"type": "string",
"pattern": "^[0-9]+(\\.[0-9]+)?$"
}
},
"required": ["descriptor", "value"]
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "const": "MIN_FARE_DISTANCE_KM" }
},
"required": ["code"]
},
"value": {
"type": "string",
"pattern": "^[0-9]+(\\.[0-9]+)?$"
}
},
"required": ["descriptor", "value"]
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "const": "PER_KM_CHARGE" }
},
"required": ["code"]
},
"value": {
"type": "string",
"pattern": "^[0-9]+(\\.[0-9]+)?$"
}
},
"required": ["descriptor", "value"]
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "const": "PICKUP_CHARGE" }
},
"required": ["code"]
},
"value": {
"type": "string",
"pattern": "^[0-9]+(\\.[0-9]+)?$"
}
},
"required": ["descriptor", "value"]
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "const": "WAITING_CHARGE_PER_MIN" }
},
"required": ["code"]
},
"value": {
"type": "string",
"pattern": "^[0-9]+(\\.[0-9]+)?$"
}
},
"required": ["descriptor", "value"]
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "const": "NIGHT_CHARGE_MULTIPLIER" }
},
"required": ["code"]
},
"value": {
"type": "string",
"pattern": "^[0-9]+(\\.[0-9]+)?$"
}
},
"required": ["descriptor", "value"]
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "const": "NIGHT_SHIFT_START_TIME" }
},
"required": ["code"]
},
"value": {
"type": "string",
"pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$"
}
},
"required": ["descriptor", "value"]
}
}
]
}
}
}
}
}
}
}
}
}
}
}
}
}
},
{
"properties": {
"message": {
"properties": {
"order": {
"properties": {
"items": {
"items": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": {
"code": { "const": "FARE_POLICY" }
}
}
}
},
"then": {
"properties": {
"list": {
"type": "array",
"items": {
"allOf": [
{
"if": {
"properties": {
"descriptor": {
"properties": {
"code": {
"enum": [
"MIN_FARE",
"MIN_FARE_DISTANCE_KM",
"PER_KM_CHARGE",
"PICKUP_CHARGE",
"WAITING_CHARGE_PER_MIN",
"NIGHT_CHARGE_MULTIPLIER"
]
}
}
}
}
},
"then": {
"properties": {
"value": {
"type": "string",
"pattern": "^-?\\d+(\\.\\d+)?$"
}
},
"required": ["descriptor", "value"]
}
},
{
"if": {
"properties": {
"descriptor": {
"properties": {
"code": {
"enum": [
"NIGHT_SHIFT_START_TIME",
"NIGHT_SHIFT_END_TIME"
]
}
}
}
}
},
"then": {
"properties": {
"value": {
"type": "string",
"pattern": "^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$"
}
},
"required": ["descriptor", "value"]
}
},
{
"if": {
"properties": {
"descriptor": {
"properties": {
"code": { "const": "EXTERNAL_REF" }
}
}
}
},
"then": {
"properties": {
"value": {
"type": "string",
"pattern": "^https?://[^\\s/$.?#].[^\\s]*$"
}
},
"required": ["descriptor", "value"]
}
}
]
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
]
}, },
{ {
"allOf": [ "$ref": "./on_init.json#/allOf/5"
{
"properties": {
"message": {
"properties": {
"order": {
"properties": {
"items": {
"items": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": {
"code": { "const": "INFO" }
}
}
}
},
"then": {
"properties": {
"list": {
"type": "array",
"items": {
"type": "object",
"properties": {
"descriptor": {
"properties": {
"code": {
"type": "string",
"enum": [
"DISTANCE_TO_NEAREST_DRIVER_METER",
"ETA_TO_NEAREST_DRIVER_MIN"
]
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
},
{
"properties": {
"message": {
"properties": {
"order": {
"properties": {
"items": {
"items": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": {
"code": { "const": "INFO" }
}
}
}
},
"then": {
"properties": {
"list": {
"allOf": [
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "const": "ETA_TO_NEAREST_DRIVER_MIN" }
},
"required": ["code"]
},
"value": {
"type": "string",
"pattern": "^\\d+(\\.\\d+)?$"
}
},
"required": ["descriptor", "value"]
}
},
{
"contains": {
"type": "object",
"properties": {
"descriptor": {
"type": "object",
"properties": {
"code": { "const": "DISTANCE_TO_NEAREST_DRIVER_METER" }
},
"required": ["code"]
},
"value": {
"type": "string",
"pattern": "^\\d+(\\.\\d+)?$"
}
},
"required": ["descriptor", "value"]
}
}
]
}
}
}
}
}
}
}
}
}
}
}
}
}
},
{
"properties": {
"message": {
"properties": {
"order": {
"properties": {
"items": {
"type": "array",
"items": {
"properties": {
"tags": {
"items": {
"if": {
"properties": {
"descriptor": {
"properties": {
"code": { "const": "INFO" }
}
}
}
},
"then": {
"properties": {
"list": {
"type": "array",
"items": {
"allOf": [
{
"if": {
"properties": {
"descriptor": {
"properties": {
"code": {
"enum": [
"DISTANCE_TO_NEAREST_DRIVER_METER",
"ETA_TO_NEAREST_DRIVER_MIN"
]
}
}
}
}
},
"then": {
"properties": {
"value": {
"type": "string",
"pattern": "^-?\\d+(\\.\\d+)?$"
}
},
"required": ["descriptor", "value"]
}
}
]
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
]
}, },
{ {
"allOf": [ "allOf": [
@@ -784,6 +196,9 @@
} }
] ]
}, },
{
"$ref": "./on_init.json#/allOf/7"
},
{ {
"properties": { "properties": {
"message": { "message": {
@@ -807,6 +222,9 @@
} }
} }
} }
},
{
"$ref": "./on_init.json#/allOf/8"
} }
] ]
} }

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/confirm.json#" "$ref": "../../core/v1.1.0/confirm.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/confirm#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
} }
] ]
} }

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/confirm.json#" "$ref": "../../core/v1.1.0/confirm.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/confirm#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
} }
] ]
} }

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/confirm.json#" "$ref": "../../core/v1.1.0/confirm.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/confirm#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
} }
] ]
} }

View File

@@ -1,16 +1,13 @@
{ {
"$schema": "https://json-schema.org/draft/2020-12/schema", "$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://ondc.org/trv10/2.0.0/on_update", "$id": "on_update",
"type": "object", "type": "object",
"allOf": [ "allOf": [
{ {
"$ref": "../core/v1.1.0/confirm.json#" "$ref": "../../core/v1.1.0/confirm.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/confirm#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
} }
] ]
} }

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/rating.json#" "$ref": "../../core/v1.1.0/rating.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/rating#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
} }
] ]
} }

View File

@@ -2,7 +2,7 @@
"$schema": "https://json-schema.org/draft/2020-12/schema", "$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "search", "$id": "search",
"allOf": [ "allOf": [
{ "$ref": "../core/v1.1.0/search.json#" } { "$ref": "../../core/v1.1.0/search.json#" }
], ],
"type": "object", "type": "object",
"properties": { "properties": {
@@ -38,12 +38,10 @@
"required": ["city", "country"] "required": ["city", "country"]
}, },
"bap_id": { "bap_id": {
"type": "string", "type": "string"
"pattern": "^(http|https).*"
}, },
"bpp_id": { "bpp_id": {
"type": "string", "type": "string"
"pattern": "^(http|https).*"
}, },
"ttl": { "ttl": {
"type": "string", "type": "string",

View File

@@ -3,9 +3,14 @@
"$id": "select", "$id": "select",
"type": "object", "type": "object",
"allOf": [ "allOf": [
{ "$ref": "../core/v1.1.0/select.json#" }, {
{ "$ref": "https://beckn.org/schema/select#" }, "$ref": "../../core/v1.1.0/select.json#"
{ "$ref": "./init.json#/$defs/init_schema_1" }, },
{ "$ref": "../paths/init.json#/$defs/init_schema_2" } {
"$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
}
] ]
} }

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/status.json#" "$ref": "../../core/v1.1.0/status.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/status#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
}, },
{ {
"properties": { "properties": {

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/support.json#" "$ref": "../../core/v1.1.0/support.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/support#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
} }
] ]
} }

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/track.json#" "$ref": "../../core/v1.1.0/track.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/track#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
} }
] ]
} }

View File

@@ -7,10 +7,7 @@
"$ref": "../../core/v1.1.0/update.json#" "$ref": "../../core/v1.1.0/update.json#"
}, },
{ {
"$ref": "https://beckn.org/schema/update#" "$ref": "./init.json#/allOf/1"
},
{
"$ref": "./init.json#/allOf/2"
}, },
{ {
"properties": { "properties": {

View File

@@ -1,3 +1,4 @@
{ {
"context": { "context": {
"action": "search", "action": "search",
@@ -24,7 +25,7 @@
"stops": [ "stops": [
{ {
"location": { "location": {
"gps": "13.0089, 77.644408" "gps": "13.008935, 77.644408"
}, },
"type": "START" "type": "START"
}, },
@@ -49,7 +50,7 @@
"descriptor": { "descriptor": {
"code": "BUYER_FINDER_FEES_PERCENTAGE" "code": "BUYER_FINDER_FEES_PERCENTAGE"
}, },
"value": 1 "value": "1"
} }
] ]
}, },
@@ -63,13 +64,13 @@
"descriptor": { "descriptor": {
"code": "DELAY_INTEREST" "code": "DELAY_INTEREST"
}, },
"value": 5 "value": "5"
}, },
{ {
"descriptor": { "descriptor": {
"code": "STATIC_TERMS" "code": "STATIC_TERMS"
}, },
"value": "example-test-bap.com/static-terms.txt" "value": "https://example-test-bap.com/static-terms.txt"
} }
] ]
} }
@@ -78,3 +79,7 @@
} }
} }
} }

80
test.go
View File

@@ -2,10 +2,10 @@ package main
import ( import (
"beckn-onix/plugins" "beckn-onix/plugins"
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http"
"net/url" "net/url"
"strings" "strings"
) )
@@ -19,55 +19,63 @@ type Payload struct {
} }
func main() { func main() {
http.HandleFunc("/", validateHandler)
fmt.Println("Starting server on port 8084...")
err := http.ListenAndServe(":8084", nil)
if err != nil {
log.Fatalf("Server failed to start: %v", err)
}
}
func validateHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
// Extract endpoint from request URL
requestURL := r.RequestURI
u, err := url.Parse(requestURL)
if err != nil {
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)
fmt.Println("Handling request for endpoint:", endpoint)
pluginsConfig, err := plugins.LoadPluginsConfig("plugins/config.yaml") pluginsConfig, err := plugins.LoadPluginsConfig("plugins/config.yaml")
if err != nil { if err != nil {
log.Fatalf("Failed to load plugins configuration: %v", err) http.Error(w, "Failed to load plugins configuration", http.StatusInternalServerError)
return
} }
_, validators, err := plugins.NewValidatorProvider(pluginsConfig) _, validators, err := plugins.NewValidatorProvider(pluginsConfig)
if err != nil { if err != nil {
log.Fatalf("Failed to create PluginManager: %v", err) http.Error(w, "Failed to create PluginManager", http.StatusInternalServerError)
return
} }
for fileName, validator := range validators {
fmt.Printf("%s: %v\n", fileName, validator)
}
requestURL := "http://example.com/select"
// Extract endpoint from request URL
u, err := url.Parse(requestURL)
if err != nil {
log.Fatalf("Failed to parse request URL: %v", err)
}
schemaFileName := fmt.Sprintf("%s.json", strings.Trim(u.Path, "/"))
//approch 1 start
// endpoint := strings.Trim(u.Path, "/")
payloadData, err := ioutil.ReadFile("plugins/test/payload.json") //approach 2
if err != nil {
log.Fatalf("Failed to read payload data: %v", err)
}
// var payload Payload
// if err := json.Unmarshal(payloadData, &payload); err != nil {
// log.Fatalf("Failed to unmarshal payload: %v", err)
// }
// domain := strings.Replace(strings.ToLower(payload.Context.Domain), ":", "_", -1)
// schemaFileName := fmt.Sprintf("%s_%s.json.%s", domain,
// strings.ToLower(payload.Context.Version), endpoint)
//end
validator, exists := validators[schemaFileName] validator, exists := validators[schemaFileName]
if !exists { if !exists {
log.Fatalf("Validator not found for %s", schemaFileName) http.Error(w, fmt.Sprintf("Validator not found for %s", schemaFileName), http.StatusNotFound)
return
} }
ctx := context.Background()
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) err = validator.Validate(ctx, payloadData)
if err != nil { if err != nil {
fmt.Printf("Document validation failed: %v\n", err) http.Error(w, fmt.Sprintf("Document validation failed: %v", err), http.StatusBadRequest)
} else { } else {
fmt.Println("Document validation succeeded!") w.WriteHeader(http.StatusOK)
w.Write([]byte("Document validation succeeded!"))
} }
} }