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

Add spec validators for the WRP messages (BAD BRANCH) #82

Closed

Conversation

denopink
Copy link
Contributor

@denopink denopink commented Jun 3, 2022

Overview

related to #25, #78, xmidt-org/scytale#88, xmidt-org/talaria#153 and builds on top of #80.

Note The diff looks big because we're waiting for #80 , we'll rebase when it's merged.

tl;dr

This pr introduces our wrp spec validators built with our validation framework introduced in #80 which only validates the opinionated portions of the spec. Clients can leverage these prebuilt validators to validate their messages.

Explanation

Clients can leverage our prebuilt validators to validate their messages. One set of these validators are our spec validators:

msgv, err := NewTypeValidator(
	// Validates found msg types
	map[MessageType]Validator{
		// Validates opinionated portions of the spec
		SimpleEventMessageType: SpecValidators,
		// Only validates Source and nothing else
		SimpleRequestResponseMessageType: SourceValidator,
	},
	// Validates unfound msg types
	AlwaysInvalid)
if err != nil {
	return
}

foundErrSuccess1 := msgv.Validate(Message{
	Type:        SimpleEventMessageType,
	Source:      "MAC:11:22:33:44:55:66",
	Destination: "MAC:11:22:33:44:55:61",
}) // Found success
foundErrSuccess2 := msgv.Validate(Message{
	Type:        SimpleRequestResponseMessageType,
	Source:      "MAC:11:22:33:44:55:66",
	Destination: "invalid:a-BB-44-55",
}) // Found success
foundErrFailure := msgv.Validate(Message{
	Type:        Invalid0MessageType,
	Source:      "invalid:a-BB-44-55",
	Destination: "invalid:a-BB-44-55",
}) // Found error
unfoundErrFailure := msgv.Validate(Message{Type: CreateMessageType}) // Unfound error
fmt.Println(foundErrSuccess1 == nil, foundErrSuccess2 == nil, foundErrFailure == nil, unfoundErrFailure == nil)
// Output: true true false false
Type of Change(s)
  • Non-breaking Enhancement
  • All new and existing tests passed.

denopink and others added 30 commits May 24, 2022 09:22
## Overview

related to xmidt-org#25, xmidt-org#78, xmidt-org/scytale#88, xmidt-org/talaria#153 s.t. we want a validation mechanism that is configurable by the application & verifies the spec.

### tl;rd
This pr introduces the initial validation framework, where applications supply validators (satisfying the `Validator interface`) to `NewMsgTypeValidator` and then used to verify the spec.

<details>
<summary> Explanation</summary>

Apps supply validators satisfying:
```go
// Validator is a WRP validator that allows access to the Validate function.
type Validator interface {
	Validate(m Message) error
}
```

and listing which validators are used on known and unknown msg types (where unknown msg types are handled by `defaultValidator`):
```go
var alwaysValidMsg ValidatorFunc = func(msg Message) error { return nil }
msgv, err := NewMsgTypeValidator(
	// Validates known msg types
	m: map[MessageType]Validators{SimpleEventMessageType: {alwaysValidMsg}},
	// Validates unknown msg types
	defaultValidator: alwaysValidMsg)
err = msgv.Validate(Message{Type: SimpleEventMessageType}) // Known msg type
err == nil // True
err = msgv.Validate(Message{Type: CreateMessageType}) // Unknown msg type, uses defaultValidator
err == nil // True
```

if a default validator is not provided, all unknown msg type will **fail** by default
```go
var alwaysValidMsg ValidatorFunc = func(msg Message) error { return nil }
msgv, err := NewMsgTypeValidator( // Omitted defaultValidator
	m: map[MessageType]Validators{SimpleEventMessageType: {alwaysInvalidMsg()}})
err = msgv.Validate(Message{Type: CreateMessageType})
err != nil // True
```
</details>

<details>
<summary>Type of Change(s)</summary>

- Non-breaking Enhancement
- All new and existing tests passed.

</details>

<details>
<summary>Module Unit Testing: [PASSING]</summary>

</details>

<details>
<summary>PR Affecting Unit Testing: validator_test.go [PASSING]</summary>

```console
Running tool: /usr/local/bin/go test -timeout 30s -run ^(TestHelperValidators|TestMsgTypeValidator)$ github.com/xmidt-org/wrp-go/v3

=== RUN   TestHelperValidators
=== RUN   TestHelperValidators/alwaysInvalidMsg
--- PASS: TestHelperValidators (0.00s)
    --- PASS: TestHelperValidators/alwaysInvalidMsg (0.00s)
=== RUN   TestMsgTypeValidator
=== RUN   TestMsgTypeValidator/MsgTypeValidator_validate
=== RUN   TestMsgTypeValidator/MsgTypeValidator_validate/known_message_type
=== RUN   TestMsgTypeValidator/MsgTypeValidator_validate/unknown_message_type_with_provided_default_Validator
=== RUN   TestMsgTypeValidator/MsgTypeValidator_validate/known_message_type_with_a_failing_Validator
=== RUN   TestMsgTypeValidator/MsgTypeValidator_validate/unknown_message_type_without_provided_default_Validator
=== RUN   TestMsgTypeValidator/MsgTypeValidator_factory
=== RUN   TestMsgTypeValidator/MsgTypeValidator_factory/with_provided_default_Validator
=== RUN   TestMsgTypeValidator/MsgTypeValidator_factory/without_provided_default_Validator
=== RUN   TestMsgTypeValidator/MsgTypeValidator_factory/empty_list_of_message_type_Validators
=== RUN   TestMsgTypeValidator/MsgTypeValidator_factory/empty_value_'m'_map[MessageType]Validators
--- PASS: TestMsgTypeValidator (0.00s)
    --- PASS: TestMsgTypeValidator/MsgTypeValidator_validate (0.00s)
        --- PASS: TestMsgTypeValidator/MsgTypeValidator_validate/known_message_type (0.00s)
        --- PASS: TestMsgTypeValidator/MsgTypeValidator_validate/unknown_message_type_with_provided_default_Validator (0.00s)
        --- PASS: TestMsgTypeValidator/MsgTypeValidator_validate/known_message_type_with_a_failing_Validator (0.00s)
        --- PASS: TestMsgTypeValidator/MsgTypeValidator_validate/unknown_message_type_without_provided_default_Validator (0.00s)
    --- PASS: TestMsgTypeValidator/MsgTypeValidator_factory (0.00s)
        --- PASS: TestMsgTypeValidator/MsgTypeValidator_factory/with_provided_default_Validator (0.00s)
        --- PASS: TestMsgTypeValidator/MsgTypeValidator_factory/without_provided_default_Validator (0.00s)
        --- PASS: TestMsgTypeValidator/MsgTypeValidator_factory/empty_list_of_message_type_Validators (0.00s)
        --- PASS: TestMsgTypeValidator/MsgTypeValidator_factory/empty_value_'m'_map[MessageType]Validators (0.00s)
PASS
ok      github.com/xmidt-org/wrp-go/v3  0.303s

> Test run finished at 5/25/2022, 11:39:22 AM <
```

</details>
simplifies the usage of alwaysInvalidMsg
Return `ErrInvalidMsgTypeValidator` and `ErrInvalidMsgType` without additional details. We can add those error details downstream later
Thx @kristinapathak for the feedback! 🍻
already covered by "Not found success"
* exported alwaysValid
* decoupled data structures leveraging `Validators` to `Validator`
* added validateValidator
update examples

Doc/var update

Add test for `Nil default Validators` edge case

update examples output

doc update

Updates based on PR review

* exported alwaysValid
* decoupled data structures leveraging `Validators` to `Validator`
* added validateValidator

Add multierr to Validator
* Validates requirements described at https://xmidt.io/docs/wrp/basics/#overarching-guidelines
* Validates requirements described at https://xmidt.io/docs/wrp/basics/#locators
* validates message type values
* Does not cover validators for a specific message type (future prs)
* exported alwaysValid
* decoupled data structures leveraging `Validators` to `Validator`
* added validateValidator
* Validates requirements described at https://xmidt.io/docs/wrp/basics/#overarching-guidelines
* Validates requirements described at https://xmidt.io/docs/wrp/basics/#locators
* validates message type values
* Does not cover validators for a specific message type (future prs)
* Clean up errors
* Remove multierr with some wrapping and regular errors
* Wrap SpecValidators within a func
…xample

* update testUTF8Validator success case to contain a correct source
* removed unused error list
* removed nil struct field inits
* added missing test cases for testValidateLocator
* improved test descriptions for testValidateLocator
* added missing test cases for TestSpecValidators
* improved example
* converted remaining `Validators` to `Validator`
* remove func `validateValidator`
* move nil checks to `Validate` func
* remove Test struct
* add new unexported field to check if `TypeValidato` is valid
* add func `IsBad` to TypeValidato to say whether `TypeValidato` is valid
* update tests for testAlwaysInvalid and testAlwaysValid
* update tests names for `TestTypeValidator`
* add tests for `Validators`
* [misunderstanding] Remove unexported field `isbad` from `TypeValidator` and its references
* [misunderstanding] Remove `IsBad` func from `TypeValidator`
* Use `assert.Zero/NotZero` funcs to test `TypeValidator`'s state
@denopink denopink changed the title Add spec validators for the WRP messages Add spec validators for the WRP messages (BAD BRANCH) Jun 8, 2022
@denopink
Copy link
Contributor Author

denopink commented Jun 8, 2022

I did something silly and this branch is kinda messed up. Easier to create new branch and pr and cherry-pick the desired commits.

@denopink denopink closed this Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants