-
Notifications
You must be signed in to change notification settings - Fork 7
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
denopink
wants to merge
74
commits into
xmidt-org:main
from
denopink:denopink/feature/SpecValidators
Closed
Add spec validators for the WRP messages (BAD BRANCH) #82
denopink
wants to merge
74
commits into
xmidt-org:main
from
denopink:denopink/feature/SpecValidators
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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"
…ort & added examples
…om/denopink/wrp-go into denopink/FR-WRPValidationFramework
* 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
…re/SpecValidators
…re/SpecValidators
* 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
… `testAlwaysInvalid`
…denopink/wrp-go into denopink/feature/SpecValidators
denopink
changed the title
Add spec validators for the WRP messages
Add spec validators for the WRP messages (BAD BRANCH)
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
:Type of Change(s)