Skip to content

Commit

Permalink
api: Validate KEPs by field
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <foo@auggie.dev>
  • Loading branch information
justaugustus committed Feb 1, 2021
1 parent 7ccea0e commit 2d4148b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (prr *PRRApproval) Validate() error {

// TODO(api): Can we refactor the proposal `Milestone` to retrieve this?
type PRRMilestone struct {
Approver string `json:"approver" yaml:"approver" validate:"required"`
Approver string `json:"approver" yaml:"approver"`
}

type PRRHandler Parser
Expand Down
48 changes: 48 additions & 0 deletions api/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ limitations under the License.

package api

import (
"bufio"
"bytes"
"io"

"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)

type Proposals []*Proposal

func (p *Proposals) AddProposal(proposal *Proposal) {
Expand Down Expand Up @@ -56,6 +66,44 @@ type Proposal struct {
Contents string `json:"markdown" yaml:"-"`
}

func (p *Proposal) Validate() error {
v := validator.New()
if err := v.Struct(p); err != nil {
return errors.Wrap(err, "running validation")
}

return nil
}

type KEPHandler Parser

// TODO(api): Make this a generic parser for all `Document` types
func (k *KEPHandler) Parse(in io.Reader) (*Proposal, error) {
scanner := bufio.NewScanner(in)
var body bytes.Buffer
for scanner.Scan() {
line := scanner.Text() + "\n"
body.WriteString(line)
}

kep := &Proposal{}
if err := scanner.Err(); err != nil {
return kep, errors.Wrap(err, "reading file")
}

if err := yaml.Unmarshal(body.Bytes(), &kep); err != nil {
k.Errors = append(k.Errors, errors.Wrap(err, "error unmarshalling YAML"))
return kep, errors.Wrap(err, "unmarshalling YAML")
}

if valErr := kep.Validate(); valErr != nil {
k.Errors = append(k.Errors, errors.Wrap(valErr, "validating PRR"))
return kep, errors.Wrap(valErr, "validating PRR")
}

return kep, nil
}

type Milestone struct {
Alpha string `json:"alpha" yaml:"alpha"`
Beta string `json:"beta" yaml:"beta"`
Expand Down
7 changes: 4 additions & 3 deletions test/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/stretchr/testify/require"

"k8s.io/enhancements/api"
"k8s.io/enhancements/pkg/legacy/keps"
)

const (
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestValidation(t *testing.T) {
t.Fatal("must find more than 0 keps")
}

kepParser := &keps.Parser{}
kepHandler := &api.KEPHandler{}
prrHandler := &api.PRRHandler{}
prrsDir := filepath.Join("..", prrsDir)

Expand All @@ -88,7 +87,9 @@ func TestValidation(t *testing.T) {

defer kepFile.Close()

kep := kepParser.Parse(kepFile)
kep, kepParseErr := kepHandler.Parse(kepFile)
require.Nil(t, kepParseErr)

if kep.Error != nil {
t.Errorf("%v has an error: %v", filename, kep.Error)
}
Expand Down

0 comments on commit 2d4148b

Please sign in to comment.