xmidt-org / wrp-go

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

FR: Add spec validators for the WRP messages #84

Closed denopink closed 2 years ago

denopink commented 2 years ago

Overview

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

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 * 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) Clients can leverage our prebuilt validators to validate their messages. One set of these validators are our `spec validators`: ```go 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.
codecov[bot] commented 2 years ago

Codecov Report

Merging #84 (edeb68e) into main (be83fc7) will increase coverage by 0.82%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main      #84      +/-   ##
==========================================
+ Coverage   49.67%   50.49%   +0.82%     
==========================================
  Files          20       21       +1     
  Lines        3259     3313      +54     
==========================================
+ Hits         1619     1673      +54     
  Misses       1469     1469              
  Partials      171      171              
Impacted Files Coverage Δ
utf8.go 88.23% <ø> (-0.66%) :arrow_down:
spec_validator.go 100.00% <100.00%> (ø)
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 be83fc7...edeb68e. Read the comment docs.