...

Source file src/github.com/qri-io/jsonschema/keyword_test.go

Documentation: github.com/qri-io/jsonschema

     1  package jsonschema
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"testing"
     8  
     9  	jptr "github.com/qri-io/jsonpointer"
    10  )
    11  
    12  func TestErrorMessage(t *testing.T) {
    13  	ctx := context.Background()
    14  	cases := []struct {
    15  		schema, doc, message string
    16  	}{
    17  		{`{ "const" : "a value" }`, `"a different value"`, `must equal "a value"`},
    18  	}
    19  
    20  	for i, c := range cases {
    21  		rs := &Schema{}
    22  		if err := rs.UnmarshalJSON([]byte(c.schema)); err != nil {
    23  			t.Errorf("case %d schema is invalid: %s", i, err.Error())
    24  			continue
    25  		}
    26  
    27  		errs, err := rs.ValidateBytes(ctx, []byte(c.doc))
    28  		if err != nil {
    29  			t.Errorf("case %d error validating: %s", i, err)
    30  			continue
    31  		}
    32  
    33  		if len(errs) != 1 {
    34  			t.Errorf("case %d didn't return exactly 1 validation error. got: %d", i, len(errs))
    35  			continue
    36  		}
    37  
    38  		if errs[0].Message != c.message {
    39  			t.Errorf("case %d error mismatch. expected '%s', got: '%s'", i, c.message, errs[0].Message)
    40  		}
    41  	}
    42  }
    43  
    44  type IsFoo bool
    45  
    46  func newIsFoo() Keyword {
    47  	return new(IsFoo)
    48  }
    49  
    50  func (f *IsFoo) Validate(propPath string, data interface{}, errs *[]KeyError) {}
    51  
    52  func (f *IsFoo) Register(uri string, registry *SchemaRegistry) {}
    53  
    54  func (f *IsFoo) Resolve(pointer jptr.Pointer, uri string) *Schema {
    55  	return nil
    56  }
    57  
    58  func (f *IsFoo) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
    59  	if str, ok := data.(string); ok {
    60  		if str != "foo" {
    61  			currentState.AddError(data, fmt.Sprintf("should be foo. plz make '%s' == foo. plz", str))
    62  		}
    63  	}
    64  }
    65  
    66  func ExampleCustomValidator() {
    67  	// register a custom validator by supplying a function
    68  	// that creates new instances of your Validator.
    69  	RegisterKeyword("foo", newIsFoo)
    70  	ctx := context.Background()
    71  
    72  	schBytes := []byte(`{ "foo": true }`)
    73  
    74  	rs := new(Schema)
    75  	if err := json.Unmarshal(schBytes, rs); err != nil {
    76  		// Real programs handle errors.
    77  		panic(err)
    78  	}
    79  
    80  	errs, err := rs.ValidateBytes(ctx, []byte(`"bar"`))
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  
    85  	fmt.Println(errs[0].Error())
    86  	// Output: /: "bar" should be foo. plz make 'bar' == foo. plz
    87  }
    88  
    89  type FooKeyword uint8
    90  
    91  func (f *FooKeyword) Validate(propPath string, data interface{}, errs *[]KeyError) {}
    92  
    93  func (f *FooKeyword) Register(uri string, registry *SchemaRegistry) {}
    94  
    95  func (f *FooKeyword) Resolve(pointer jptr.Pointer, uri string) *Schema {
    96  	return nil
    97  }
    98  
    99  func (f *FooKeyword) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
   100  }
   101  
   102  func TestRegisterFooKeyword(t *testing.T) {
   103  	newFoo := func() Keyword {
   104  		return new(FooKeyword)
   105  	}
   106  
   107  	RegisterKeyword("foo", newFoo)
   108  
   109  	if !IsRegisteredKeyword("foo") {
   110  		t.Errorf("expected %s to be added as a default validator", "foo")
   111  	}
   112  }
   113  

View as plain text