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

FR: Initial WRP Validation Framework #80

Merged
merged 26 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dfeee98
Add VSCode to .gitignore
denopink May 24, 2022
7f8bad0
Initial WRP Validation Framework
denopink May 25, 2022
9319c12
formatting
denopink May 25, 2022
209ffdb
formatting
denopink May 25, 2022
ddd0acf
convert alwaysInvalidMsg to literal func
denopink May 25, 2022
a2a4093
Formating and exported alwaysInvalidMsg
denopink May 25, 2022
f5a2e90
Decouple error from api
denopink May 25, 2022
eb5bf9b
Updates based on PR review
denopink May 26, 2022
afa08cb
removed duplicated test
denopink May 26, 2022
8cb0fa7
Fix type test name
denopink May 26, 2022
10afaa2
Fix type test name
denopink May 26, 2022
1923ec7
Add missing test/edge cases for Validators
denopink May 26, 2022
98c4b4a
Updated defaultValidator to work as a variadic for multi-default supp…
denopink May 26, 2022
3197568
rename test var `name` to `description`
denopink May 26, 2022
1ee6c2e
Merge branch 'denopink/FR-WRPValidationFramework' of https://github.c…
denopink May 26, 2022
793d6f4
Squash: examples, docs, multierr updates
denopink May 26, 2022
22ca097
formatting
denopink Jun 3, 2022
5ff49e3
Add missing test for `AlwaysValid` func
denopink Jun 3, 2022
1f1aa67
Updates based on PR feedback
denopink Jun 7, 2022
df7e2ff
Update based on PR review
denopink Jun 8, 2022
ffd531f
Move exported fields to the top
denopink Jun 8, 2022
13a9a57
update tests to include correct mac length
denopink Jun 8, 2022
3ac1d02
`testAlwaysValid` & `testAlwaysInvalid` ensure a non-existing message…
denopink Jun 8, 2022
e553e7e
`testTypeValidatorFactory` swap `assert.Zero/NotZero`
denopink Jun 8, 2022
8e6882b
Add missing scheme to source fields & add missing `assert.ErrorIs` to…
denopink Jun 8, 2022
fc099bf
Update based on PR feedback
denopink Jun 8, 2022
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
49 changes: 9 additions & 40 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
)

var (
ErrInvalidTypeValidator = errors.New("invalid WRP message type validator")
ErrInvalidTypeValidator = errors.New("invalid TypeValidator")
ErrInvalidValidator = errors.New("invalid WRP message type validator")
ErrInvalidMsgType = errors.New("invalid WRP message type")
)

Expand All @@ -48,7 +49,9 @@ type Validators []Validator
func (vs Validators) Validate(m Message) error {
denopink marked this conversation as resolved.
Show resolved Hide resolved
var err error
for _, v := range vs {
err = multierr.Append(err, v.Validate(m))
if v != nil {
err = multierr.Append(err, v.Validate(m))
}
}

return err
Expand All @@ -67,7 +70,7 @@ func (vf ValidatorFunc) Validate(m Message) error {
// or using the defaultValidators if message type is unfound.
type TypeValidator struct {
m map[MessageType]Validator
defaultValidators Validators
defaultValidators Validator
denopink marked this conversation as resolved.
Show resolved Hide resolved
}

// Validate validates messages based on message type or using the defaultValidators
Expand All @@ -84,49 +87,15 @@ func (m TypeValidator) Validate(msg Message) error {
// NewTypeValidator is a TypeValidator factory.
func NewTypeValidator(m map[MessageType]Validator, defaultValidators ...Validator) (TypeValidator, error) {
denopink marked this conversation as resolved.
Show resolved Hide resolved
if m == nil {
return TypeValidator{}, ErrInvalidTypeValidator
}

for _, v := range m {
if err := validateValidator(v); err != nil {
return TypeValidator{}, err
}
return TypeValidator{}, ErrInvalidValidator
}

if len(defaultValidators) == 0 {
if defaultValidators == nil {
defaultValidators = Validators{AlwaysInvalid}
}

for _, v := range defaultValidators {
if err := validateValidator(v); err != nil {
return TypeValidator{}, err
}
}

return TypeValidator{
m: m,
defaultValidators: defaultValidators,
defaultValidators: Validators(defaultValidators),
}, nil
}

// validateValidator validates a given Validator.
func validateValidator(v Validator) error {
switch vs := v.(type) {
case Validators:
if vs == nil || len(vs) == 0 {
return ErrInvalidTypeValidator
}

for _, v := range vs {
if v == nil {
return ErrInvalidTypeValidator
}
}
case Validator, ValidatorFunc:
// catch nil Validator
default:
return ErrInvalidTypeValidator
}

return nil
}
Loading