Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: populate context with validation errors when context is nil #3815

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
abhimanyubabbar marked this conversation as resolved.
Show resolved Hide resolved
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
Loading