Skip to content

Commit

Permalink
refactored the code
Browse files Browse the repository at this point in the history
  • Loading branch information
pghildiyal committed Jul 5, 2023
1 parent 7e4b332 commit 85dca54
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions pkg/Filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,36 @@ import (
func FilterValidationResults(result ValidationResult, conf *Config) ValidationResult {
//var vr []ValidationResult
//for _, result := range validationResults {
var errorsForLatest []*openapi3.SchemaError
var errorsForOriginal []*openapi3.SchemaError
for _, schemaError := range result.ErrorsForLatest {
if !conf.IgnoreNullErrors || strings.TrimSpace(schemaError.Reason) != "Value is not nullable" {
errorsForLatest = append(errorsForLatest, schemaError)
}
}
result.ErrorsForLatest = errorsForLatest
for _, schemaError := range result.ErrorsForOriginal {
if !conf.IgnoreNullErrors || strings.TrimSpace(schemaError.Reason) != "Value is not nullable" {
errorsForOriginal = append(errorsForOriginal, schemaError)
}
}
result.ErrorsForOriginal = errorsForOriginal
result.ErrorsForLatest = filterError(result.ErrorsForLatest, conf)
result.ErrorsForOriginal = filterError(result.ErrorsForOriginal, conf)
//vr = append(vr, result)
//}
return removeIgnoredKeys(result, conf)
}

func filterError(errors []*openapi3.SchemaError, conf *Config) []*openapi3.SchemaError {
var filteredErrors []*openapi3.SchemaError
for _, schemaError := range errors {
penultimateValue := len(schemaError.JSONPointer()) - 2
requestOrLimit := len(schemaError.JSONPointer()) > 2 && (schemaError.JSONPointer()[penultimateValue] == "requests" || schemaError.JSONPointer()[penultimateValue] == "limits")
requestOrLimitIsNumber := false
if requestOrLimit {
switch v := schemaError.Value.(type) {
case string:
requestOrLimitIsNumber = v == "number, integer"
}
}
if conf.IgnoreNullErrors ||
(strings.TrimSpace(schemaError.Reason) == "Value is not nullable" && schemaError.Schema.Type == "array") ||
Contains(schemaError.Schema.Description, []string{"RawExtension*"}) || requestOrLimitIsNumber {
continue
}
filteredErrors = append(filteredErrors, schemaError)
}
return filteredErrors
}

func removeIgnoredKeys(result ValidationResult, conf *Config) ValidationResult {
//var out []ValidationResult
//for _, result := range results {
if len(result.DeprecationForOriginal) > 0 {
var depErr []*SchemaError
for _, schemaError := range result.DeprecationForOriginal {
Expand All @@ -54,9 +62,7 @@ func removeIgnoredKeys(result ValidationResult, conf *Config) ValidationResult {
var valErr []*openapi3.SchemaError
for _, schemaError := range result.ErrorsForOriginal {
key := strings.Join(schemaError.JSONPointer(), "/")
penultimateValue := len(schemaError.JSONPointer()) - 2
requestOrLimit := len(schemaError.JSONPointer()) > 2 && (schemaError.JSONPointer()[penultimateValue] == "requests" || schemaError.JSONPointer()[penultimateValue] == "limits")
if !Contains(key, conf.IgnoreKeysFromValidation) && !Contains(schemaError.Schema.Description, []string{"RawExtension*"}) && !requestOrLimit {
if !Contains(key, conf.IgnoreKeysFromValidation) {
valErr = append(valErr, schemaError)
}
}
Expand All @@ -66,15 +72,11 @@ func removeIgnoredKeys(result ValidationResult, conf *Config) ValidationResult {
var valErr []*openapi3.SchemaError
for _, schemaError := range result.ErrorsForLatest {
key := strings.Join(schemaError.JSONPointer(), "/")
penultimateValue := len(schemaError.JSONPointer()) - 2
requestOrLimit := len(schemaError.JSONPointer()) > 2 && (schemaError.JSONPointer()[penultimateValue] == "requests" || schemaError.JSONPointer()[penultimateValue] == "limits")
if !Contains(key, conf.IgnoreKeysFromValidation) && !Contains(schemaError.Schema.Description, []string{"RawExtension*"}) && !requestOrLimit {
if !Contains(key, conf.IgnoreKeysFromValidation) {
valErr = append(valErr, schemaError)
}
}
result.ErrorsForLatest = valErr
}
//out = append(out, result)
//}
return result
}

0 comments on commit 85dca54

Please sign in to comment.