Skip to content

Commit

Permalink
validator: change option param to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
agungdwiprasetyo committed Apr 10, 2023
1 parent ce53ad1 commit c622e7c
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 139 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# mocks all interfaces
mocks:
@rm -rf mocks/cmd;
@mockery --all --keeptree
@if [ -f mocks/candiutils/HTTPRequestOption.go ]; then rm mocks/candiutils/HTTPRequestOption.go; fi;
@if [ -f mocks/candiutils/httpClientDo.go ]; then rm mocks/candiutils/httpClientDo.go; fi;
Expand All @@ -17,6 +16,9 @@ mocks:
@if [ -f mocks/codebase/app/rest_server/OptionFunc.go ]; then rm mocks/codebase/app/rest_server/OptionFunc.go; fi;
@if [ -f mocks/codebase/app/task_queue_worker/OptionFunc.go ]; then rm mocks/codebase/app/task_queue_worker/OptionFunc.go; fi;
@if [ -f mocks/codebase/factory/dependency/Option.go ]; then rm mocks/codebase/factory/dependency/Option.go; fi;
@if [ -f mocks/validator/JSONSchemaValidatorOptionFunc.go ]; then rm mocks/validator/JSONSchemaValidatorOptionFunc.go; fi;
@if [ -f mocks/validator/StructValidatorOptionFunc.go ]; then rm mocks/validator/StructValidatorOptionFunc.go; fi;
@rm -rf mocks/cmd;

# unit test & calculate code coverage
test:
Expand Down
2 changes: 1 addition & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package candi

const (
// Version of this library
Version = "v1.14.3"
Version = "v1.14.4"
)
39 changes: 39 additions & 0 deletions mocks/validator/JSONSchemaValidator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions mocks/validator/JSONSchemaValidatorOptionFunc.go

This file was deleted.

39 changes: 39 additions & 0 deletions mocks/validator/StructValidator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions mocks/validator/StructValidatorOptionFunc.go

This file was deleted.

21 changes: 13 additions & 8 deletions validator/json_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,39 @@ import (
"github.com/golangid/gojsonschema"
)

// JSONSchemaValidator abstraction
type JSONSchemaValidator interface {
ValidateDocument(schemaID string, documentSource interface{}) error
}

// JSONSchemaValidatorOptionFunc type
type JSONSchemaValidatorOptionFunc func(*JSONSchemaValidator)
type JSONSchemaValidatorOptionFunc func(*jsonSchemaValidator)

// SetSchemaStorageJSONSchemaValidatorOption option func
func SetSchemaStorageJSONSchemaValidatorOption(s Storage) JSONSchemaValidatorOptionFunc {
return func(v *JSONSchemaValidator) {
return func(v *jsonSchemaValidator) {
v.schemaStorage = s
}
}

// AddHideErrorListTypeJSONSchemaValidatorOption option func
func AddHideErrorListTypeJSONSchemaValidatorOption(descType ...string) JSONSchemaValidatorOptionFunc {
return func(v *JSONSchemaValidator) {
return func(v *jsonSchemaValidator) {
for _, e := range descType {
v.notShowErrorListType[e] = struct{}{}
}
}
}

// JSONSchemaValidator validator
type JSONSchemaValidator struct {
// jsonSchemaValidator validator
type jsonSchemaValidator struct {
schemaStorage Storage
notShowErrorListType map[string]struct{}
}

// NewJSONSchemaValidator constructor
func NewJSONSchemaValidator(opts ...JSONSchemaValidatorOptionFunc) *JSONSchemaValidator {
v := &JSONSchemaValidator{
func NewJSONSchemaValidator(opts ...JSONSchemaValidatorOptionFunc) JSONSchemaValidator {
v := &jsonSchemaValidator{
schemaStorage: NewInMemStorage(os.Getenv(candihelper.WORKDIR) + "api/jsonschema"),
notShowErrorListType: map[string]struct{}{
"condition_else": {}, "condition_then": {},
Expand All @@ -52,7 +57,7 @@ func NewJSONSchemaValidator(opts ...JSONSchemaValidatorOptionFunc) *JSONSchemaVa
}

// ValidateDocument based on schema id
func (v *JSONSchemaValidator) ValidateDocument(schemaID string, documentSource interface{}) error {
func (v *jsonSchemaValidator) ValidateDocument(schemaID string, documentSource interface{}) error {

s, err := v.schemaStorage.Get(schemaID)
if err != nil {
Expand Down
Loading

0 comments on commit c622e7c

Please sign in to comment.