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 validation mechanism for the WRP messages #25

Open
schmidtw opened this issue Aug 6, 2019 · 1 comment
Open

Add validation mechanism for the WRP messages #25

schmidtw opened this issue Aug 6, 2019 · 1 comment
Assignees
Labels
wrp validator issues related to wrp validator

Comments

@schmidtw
Copy link
Member

schmidtw commented Aug 6, 2019

Ideally the validation mechanism is configurable by the application & the validators verify the spec.

@joe94
Copy link
Member

joe94 commented Apr 13, 2020

Add validation functionality in wrp-go library for clients to use.

@denopink denopink self-assigned this Apr 25, 2022
denopink added a commit to denopink/wrp-go that referenced this issue May 25, 2022
## 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>
@denopink denopink added the wrp validator issues related to wrp validator label Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wrp validator issues related to wrp validator
Projects
None yet
Development

No branches or pull requests

3 participants