Skip to content

Commit

Permalink
chore: populate context with validation errors when context is nil (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayachand committed Sep 4, 2023
1 parent f3d9d11 commit dcb6a15
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 5 deletions.
20 changes: 15 additions & 5 deletions processor/trackingplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ func reportViolations(validateEvent *transformer.TransformerResponse, trackingPl
validationErrors := validateEvent.ValidationErrors
output := validateEvent.Output

eventContext, castOk := output["context"].(map[string]interface{})
if castOk {
eventContext["trackingPlanId"] = trackingPlanId
eventContext["trackingPlanVersion"] = trackingPlanVersion
eventContext["violationErrors"] = validationErrors
eventContext, ok := output["context"]
if !ok || eventContext == nil {
context := make(map[string]interface{})
context["trackingPlanId"] = trackingPlanId
context["trackingPlanVersion"] = trackingPlanVersion
context["violationErrors"] = validationErrors
output["context"] = context
return
}
context, castOk := eventContext.(map[string]interface{})
if !castOk {
return
}
context["trackingPlanId"] = trackingPlanId
context["trackingPlanVersion"] = trackingPlanVersion
context["violationErrors"] = validationErrors
}

// enhanceWithViolation It enhances extra information of ValidationErrors in context for:
Expand Down
88 changes: 88 additions & 0 deletions processor/trackingplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,56 @@ func TestReportViolations(t *testing.T) {
}
})

t.Run("Not propagating validation errors when context is not map", func(t *testing.T) {
var (
trackingPlanId string
trackingPlanVersion int
)

response := transformer.Response{
Events: []transformer.TransformerResponse{
{
Metadata: transformer.Metadata{
MergedTpConfig: map[string]interface{}{
"propagateValidationErrors": "false",
},
},
Output: map[string]interface{}{
"context": "some context",
},
},
},
FailedEvents: []transformer.TransformerResponse{
{
Metadata: transformer.Metadata{
MergedTpConfig: map[string]interface{}{
"propagateValidationErrors": "true",
},
},
Output: map[string]interface{}{
"context": 1234,
},
ValidationErrors: []transformer.ValidationError{
{
Type: "Datatype-Mismatch",
Meta: map[string]string{
"schemaPath": "#/properties/properties/properties/price/type",
"instacePath": "/properties/price",
},
Message: "must be number",
},
},
},
},
}

enhanceWithViolation(response, trackingPlanId, trackingPlanVersion)
for _, event := range eventsFromTransformerResponse(&response) {
_, castOk := event.Output["context"].(map[string]interface{})
assert.False(t, castOk)
}
})

t.Run("Propagate validation errors", func(t *testing.T) {
response := transformer.Response{
Events: []transformer.TransformerResponse{
Expand All @@ -81,6 +131,26 @@ func TestReportViolations(t *testing.T) {
},
},
},
{
Metadata: transformer.Metadata{
MergedTpConfig: map[string]interface{}{
"propagateValidationErrors": "true",
},
},
Output: map[string]interface{}{
"context": nil,
},
ValidationErrors: []transformer.ValidationError{
{
Type: "Datatype-Mismatch",
Meta: map[string]string{
"schemaPath": "#/properties/properties/properties/price/type",
"instacePath": "/properties/price",
},
Message: "must be number",
},
},
},
},
FailedEvents: []transformer.TransformerResponse{
{
Expand All @@ -103,6 +173,24 @@ func TestReportViolations(t *testing.T) {
},
},
},
{
Metadata: transformer.Metadata{
MergedTpConfig: map[string]interface{}{
"propagateValidationErrors": "true",
},
},
Output: map[string]interface{}{},
ValidationErrors: []transformer.ValidationError{
{
Type: "Datatype-Mismatch",
Meta: map[string]string{
"schemaPath": "#/properties/properties/properties/price/type",
"instacePath": "/properties/price",
},
Message: "must be number",
},
},
},
},
}
trackingPlanId := "tp_2BFrdaslxH9A7B2hSDFKxw8wPN6knOb57"
Expand Down

0 comments on commit dcb6a15

Please sign in to comment.