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

Commits on May 24, 2022

  1. Add VSCode to .gitignore

    denopink committed May 24, 2022
    Configuration menu
    Copy the full SHA
    dfeee98 View commit details
    Browse the repository at this point in the history

Commits on May 25, 2022

  1. Initial WRP Validation Framework

    ## 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 committed May 25, 2022
    Configuration menu
    Copy the full SHA
    7f8bad0 View commit details
    Browse the repository at this point in the history
  2. formatting

    denopink committed May 25, 2022
    Configuration menu
    Copy the full SHA
    9319c12 View commit details
    Browse the repository at this point in the history
  3. formatting

    denopink committed May 25, 2022
    Configuration menu
    Copy the full SHA
    209ffdb View commit details
    Browse the repository at this point in the history
  4. convert alwaysInvalidMsg to literal func

    simplifies the usage of alwaysInvalidMsg
    denopink committed May 25, 2022
    Configuration menu
    Copy the full SHA
    ddd0acf View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    a2a4093 View commit details
    Browse the repository at this point in the history
  6. Decouple error from api

    Return `ErrInvalidMsgTypeValidator` and `ErrInvalidMsgType` without additional details. We can add those error details downstream later
    denopink committed May 25, 2022
    Configuration menu
    Copy the full SHA
    f5a2e90 View commit details
    Browse the repository at this point in the history

Commits on May 26, 2022

  1. Updates based on PR review

    Thx @kristinapathak for the feedback! 🍻
    denopink committed May 26, 2022
    Configuration menu
    Copy the full SHA
    eb5bf9b View commit details
    Browse the repository at this point in the history
  2. removed duplicated test

    already covered by "Not found success"
    denopink committed May 26, 2022
    Configuration menu
    Copy the full SHA
    afa08cb View commit details
    Browse the repository at this point in the history
  3. Fix type test name

    denopink committed May 26, 2022
    Configuration menu
    Copy the full SHA
    8cb0fa7 View commit details
    Browse the repository at this point in the history
  4. Fix type test name

    denopink committed May 26, 2022
    Configuration menu
    Copy the full SHA
    10afaa2 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    1923ec7 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    98c4b4a View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    3197568 View commit details
    Browse the repository at this point in the history
  8. Merge branch 'denopink/FR-WRPValidationFramework' of https://github.c…

    …om/denopink/wrp-go into denopink/FR-WRPValidationFramework
    denopink committed May 26, 2022
    Configuration menu
    Copy the full SHA
    1ee6c2e View commit details
    Browse the repository at this point in the history

Commits on Jun 1, 2022

  1. Squash: examples, docs, multierr updates

    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
    denopink committed Jun 1, 2022
    Configuration menu
    Copy the full SHA
    793d6f4 View commit details
    Browse the repository at this point in the history

Commits on Jun 3, 2022

  1. formatting

    denopink committed Jun 3, 2022
    Configuration menu
    Copy the full SHA
    22ca097 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    5ff49e3 View commit details
    Browse the repository at this point in the history

Commits on Jun 7, 2022

  1. Updates based on PR feedback

    * 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`
    denopink committed Jun 7, 2022
    Configuration menu
    Copy the full SHA
    1f1aa67 View commit details
    Browse the repository at this point in the history

Commits on Jun 8, 2022

  1. Update based on PR review

    * [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 committed Jun 8, 2022
    Configuration menu
    Copy the full SHA
    df7e2ff View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    ffd531f View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    13a9a57 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    3ac1d02 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    e553e7e View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    8e6882b View commit details
    Browse the repository at this point in the history
  7. Update based on PR feedback

    * rename `defaultValidators` to `defaultValidator`
    * replace `defaultValidators` variadic to `defaultValidator Validator` in `NewTypeValidator` func
    denopink committed Jun 8, 2022
    Configuration menu
    Copy the full SHA
    fc099bf View commit details
    Browse the repository at this point in the history