xmidt-org / wrp-go

go implementation of the Web Routing Protocol
Apache License 2.0
4 stars 7 forks source link

FR: Initial WRP Validation Framework #80

Closed denopink closed 2 years ago

denopink commented 2 years ago

Overview

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

tl;dr

This pr introduces the initial validation framework, where clients supply validators (satisfying the Validator interface) to NewMsgTypeValidator and then used to verify the spec.

Explanation Clients 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 alwaysValid ValidatorFunc = func(msg Message) error { return nil } msgv, err := NewTypeValidator( // Validates found msg types map[MessageType]Validators{SimpleEventMessageType: {alwaysValid}}, // Validates unfound msg types AlwaysInvalid) err = msgv.Validate(Message{Type: SimpleEventMessageType}) // Found success err == nil // True err = msgv.Validate(Message{Type: CreateMessageType}) // Unfound error err == nil // False ``` if a default validator is not provided, all unknown msg type will **fail** by default ```go msgv, err := NewTypeValidator( // Omitted defaultValidator map[MessageType]Validators{SimpleEventMessageType: {alwaysValid}}) err = msgv.Validate(Message{Type: CreateMessageType}) // Unfound error err == nil // False ```
Type of Change(s) - Non-breaking Enhancement - All new and existing tests passed.
codecov[bot] commented 2 years ago

Codecov Report

Merging #80 (0218fb9) into main (840a19e) will increase coverage by 0.37%. The diff coverage is 100.00%.

:exclamation: Current head 0218fb9 differs from pull request most recent head fc099bf. Consider uploading reports for the commit fc099bf to get more accurate results

@@            Coverage Diff             @@
##             main      #80      +/-   ##
==========================================
+ Coverage   49.30%   49.67%   +0.37%     
==========================================
  Files          19       20       +1     
  Lines        3235     3259      +24     
==========================================
+ Hits         1595     1619      +24     
  Misses       1469     1469              
  Partials      171      171              
Impacted Files Coverage Δ
validator.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 840a19e...fc099bf. Read the comment docs.

denopink commented 2 years ago

@kristinapathak I added some testable examples just to try them out. it's pretty neat

image

I can take them out if it's too early for doc examples.

kristinapathak commented 2 years ago

@denopink, example looks good! 👍