Skip to content

Commit

Permalink
Fixed Validation function
Browse files Browse the repository at this point in the history
  • Loading branch information
xBlaz3kx committed Jul 29, 2024
1 parent 5c95391 commit afd2e5f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions ocpp_v16/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ocpp_v16
import (
"errors"
"fmt"
"strings"

"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/samber/lo"
Expand Down Expand Up @@ -94,17 +95,19 @@ func (config *Config) SetVersion(version int) {
// Validate validates the configuration - check if all mandatory keys are present.
func (config *Config) Validate(mandatoryKeys []Key) error {
missingKey := ""
containsMandatoryKeys := lo.ContainsBy(mandatoryKeys, func(key Key) bool {

containsMandatoryKeys := true

for _, key := range mandatoryKeys {
containsKey := lo.ContainsBy(config.Keys, func(item core.ConfigurationKey) bool {
return item.Key == key.String()
})

if !containsKey {
missingKey = key.String()
missingKey = strings.Join([]string{missingKey, key.String()}, ", ")
containsMandatoryKeys = false
}

return containsKey
})
}

if !containsMandatoryKeys {
return fmt.Errorf("missing mandatory keys: %s", missingKey)
Expand Down
2 changes: 1 addition & 1 deletion ocpp_v16/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *OcppConfigTest) TestValidate() {
s.Assert().NoError(err)

// Missing mandatory key
s.config.Keys = s.config.Keys[:len(s.config.Keys)-1]
s.config.Keys = s.config.Keys[:len(s.config.Keys)-2]
err = s.config.Validate(MandatoryCoreKeys)
s.Assert().Error(err)
}
Expand Down

0 comments on commit afd2e5f

Please sign in to comment.