Skip to content

Commit

Permalink
Merge pull request #128 from ripienaar/optional_validation
Browse files Browse the repository at this point in the history
Make validation optional, and null by default
  • Loading branch information
ripienaar authored Sep 24, 2020
2 parents 2a08573 + bd42851 commit 2c1412c
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 446 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## Overview

This is a library for managing and interacting with JetStream.
This is a library for managing and interacting with the JetStream API and various other NATS APIs and event sources.

This library provides API access to all the JetStream related abilities of the `nats` CLI utility.

**NOTE** For general access to JetStream no special libraries are needed, the standard language specific NATS client can be used. These are optional helpers.
This library provides API accesses to all the JetStream related abilities of the `nats` CLI utility. To support API access
it provides a rich set of functions related to validation, documenting and detecting event types using a set of JSON
Schema documents.

## Stability

Expand Down
23 changes: 0 additions & 23 deletions api/example_validate_event_test.go

This file was deleted.

8 changes: 6 additions & 2 deletions api/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ var schemaTypes = map[string]func() interface{}{
{{- range . }}
{{- if .ShouldAddValidator }}
// Validate performs a JSON Schema validation of the configuration
func (t {{ .St }}) Validate() (valid bool, errors []string) {
return ValidateStruct(t, t.SchemaType())
func (t {{ .St }}) Validate(v ...StructValidator) (valid bool, errors []string) {
if len(v) == 0 || v[0] == nil {
return true, nil
}
return v[0].ValidateStruct(t, t.SchemaType())
}
// SchemaType is the NATS schema type {{ .T }}
Expand Down
34 changes: 5 additions & 29 deletions api/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"strings"
"text/template"
"time"

"github.com/xeipuuv/gojsonschema"
)

// SchemasRepo is the repository holding NATS Schemas
Expand All @@ -30,6 +28,11 @@ type Event interface {
EventTemplate(kind string) (*template.Template, error)
}

// StructValidator is used to validate API structures
type StructValidator interface {
ValidateStruct(data interface{}, schemaType string) (ok bool, errs []string)
}

// RenderFormat indicates the format to render templates in
type RenderFormat string

Expand Down Expand Up @@ -155,33 +158,6 @@ func NewMessage(schemaType string) (interface{}, bool) {
return gf(), ok
}

// ValidateStruct validates data matches schemaType like io.nats.jetstream.advisory.v1.api_audit
func ValidateStruct(data interface{}, schemaType string) (ok bool, errs []string) {
// other more basic types can be validated directly against their schemaType
s, err := Schema(schemaType)
if err != nil {
return false, []string{"unknown schema type %s", schemaType}
}

ls := gojsonschema.NewBytesLoader(s)
ld := gojsonschema.NewGoLoader(data)
result, err := gojsonschema.Validate(ls, ld)
if err != nil {
return false, []string{fmt.Sprintf("validation failed: %s", err)}
}

if result.Valid() {
return true, nil
}

errors := make([]string, len(result.Errors()))
for i, verr := range result.Errors() {
errors[i] = verr.String()
}

return false, errors
}

// ParseMessage parses a typed message m and returns event as for example *api.ConsumerAckMetric, all unknown
// event schemas will be of type *UnknownMessage
func ParseMessage(m []byte) (schemaType string, msg interface{}, err error) {
Expand Down
Loading

0 comments on commit 2c1412c

Please sign in to comment.