...

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

Documentation: github.com/qri-io/jsonschema

     1  package jsonschema
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"testing"
     7  )
     8  
     9  func TestSchemaDeref(t *testing.T) {
    10  	ctx := context.Background()
    11  	sch := []byte(`{
    12      "$defs": {
    13          "a": {"type": "integer"},
    14          "b": {"$ref": "#/$defs/a"},
    15          "c": {"$ref": "#/$defs/b"}
    16      },
    17      "$ref": "#/$defs/c"
    18    }`)
    19  
    20  	rs := &Schema{}
    21  	if err := rs.UnmarshalJSON(sch); err != nil {
    22  		t.Errorf("unexpected unmarshal error: %s", err.Error())
    23  		return
    24  	}
    25  
    26  	got, err := rs.ValidateBytes(ctx, []byte(`"a"`))
    27  	if err != nil {
    28  		t.Errorf("error validating bytes: %s", err.Error())
    29  		return
    30  	}
    31  
    32  	if got == nil {
    33  		t.Errorf("expected error, got nil")
    34  		return
    35  	}
    36  }
    37  
    38  func TestReferenceTraversal(t *testing.T) {
    39  	sch, err := ioutil.ReadFile("testdata/draft2019-09_schema.json")
    40  	if err != nil {
    41  		t.Errorf("error reading file: %s", err.Error())
    42  		return
    43  	}
    44  
    45  	rs := &Schema{}
    46  	if err := rs.UnmarshalJSON(sch); err != nil {
    47  		t.Errorf("unexpected unmarshal error: %s", err.Error())
    48  		return
    49  	}
    50  
    51  	elements := 0
    52  	expectElements := 14
    53  	refs := 0
    54  	expectRefs := 6
    55  	walkJSON(rs, func(elem JSONPather) error {
    56  		elements++
    57  		if sch, ok := elem.(*Schema); ok {
    58  			if sch.HasKeyword("$ref") {
    59  				refs++
    60  			}
    61  		}
    62  		return nil
    63  	})
    64  
    65  	if elements != expectElements {
    66  		t.Errorf("expected %d elements, got: %d", expectElements, elements)
    67  	}
    68  	if refs != expectRefs {
    69  		t.Errorf("expected %d references, got: %d", expectRefs, refs)
    70  	}
    71  
    72  	cases := []struct {
    73  		input    string
    74  		elements int
    75  		refs     int
    76  	}{
    77  		{`{ "not" : { "$ref":"#" }}`, 2, 0},
    78  	}
    79  
    80  	for i, c := range cases {
    81  		rs := &Schema{}
    82  		if err := rs.UnmarshalJSON([]byte(c.input)); err != nil {
    83  			t.Errorf("unexpected unmarshal error: %s", err.Error())
    84  			return
    85  		}
    86  
    87  		elements := 0
    88  		refs := 0
    89  		walkJSON(rs, func(elem JSONPather) error {
    90  			elements++
    91  			if sch, ok := elem.(*Schema); ok {
    92  				if sch.HasKeyword("$ref") {
    93  					refs++
    94  				}
    95  			}
    96  			return nil
    97  		})
    98  		if elements != c.elements {
    99  			t.Errorf("case %d: expected %d elements, got: %d", i, c.elements, elements)
   100  		}
   101  		if refs != c.refs {
   102  			t.Errorf("case %d: expected %d references, got: %d", i, c.refs, refs)
   103  		}
   104  	}
   105  
   106  }
   107  

View as plain text