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 85dca54 commit 2b36d51
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions pkg/Filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,17 @@ import (
)

func FilterValidationResults(result ValidationResult, conf *Config) ValidationResult {
//var vr []ValidationResult
//for _, result := range validationResults {
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 {
excludeArrayNullError(schemaError) ||
excludeRawExtensionError(schemaError) || excludeCPUMemoryNumberError(schemaError) {
continue
}
filteredErrors = append(filteredErrors, schemaError)
Expand Down Expand Up @@ -80,3 +67,23 @@ func removeIgnoredKeys(result ValidationResult, conf *Config) ValidationResult {
}
return result
}

func excludeCPUMemoryNumberError(schemaError *openapi3.SchemaError) bool {
penultimateValue := len(schemaError.JSONPointer()) - 2
requestOrLimit := len(schemaError.JSONPointer()) > 1 && (schemaError.JSONPointer()[penultimateValue] == "requests" || schemaError.JSONPointer()[penultimateValue] == "limits")
if requestOrLimit {
switch v := schemaError.Value.(type) {
case string:
return v == "number, integer"
}
}
return false
}

func excludeRawExtensionError(schemaError *openapi3.SchemaError) bool {
return Contains(schemaError.Schema.Description, []string{"RawExtension*"})
}

func excludeArrayNullError(schemaError *openapi3.SchemaError) bool {
return strings.TrimSpace(schemaError.Reason) == "Value is not nullable" && schemaError.Schema.Type == "array"
}

0 comments on commit 2b36d51

Please sign in to comment.