decode_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package jsonschema
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "strings"
  6. "testing"
  7. "github.com/google/go-cmp/cmp"
  8. )
  9. const testSchemaBasic = `
  10. {
  11. "properties": {
  12. "tupleClosedEmpty": { "prefixItems": [] },
  13. "tupleClosedMissing": { "prefixItems": [{}] },
  14. "tupleClosedNull": { "prefixItems": [{}], "items": null },
  15. "tupleClosedFalse": { "prefixItems": [{}], "items": false },
  16. "tupleOpenTrue": { "prefixItems": [{}], "items": true },
  17. "tupleOpenEmpty": { "prefixItems": [{}], "items": {} },
  18. "tupleOpenTyped": { "prefixItems": [{}], "items": {"type": "boolean"} },
  19. "tupleOpenMax": { "prefixItems": [{}], "items": true, "maxItems": 3},
  20. "array": { "items": {"type": "number"} },
  21. "null": { "type": "null" },
  22. "string": { "type": "string" },
  23. "boolean": { "type": "boolean" }
  24. }
  25. }
  26. `
  27. func TestSchemaUnmarshal(t *testing.T) {
  28. var got *Schema
  29. if err := json.Unmarshal([]byte(testSchemaBasic), &got); err != nil {
  30. t.Fatalf("Unmarshal: %v", err)
  31. }
  32. want := &Schema{
  33. Properties: []*Schema{
  34. {Name: "tupleClosedEmpty", PrefixItems: []*Schema{}, Items: nil},
  35. {Name: "tupleClosedMissing", PrefixItems: []*Schema{{}}, Items: nil},
  36. {Name: "tupleClosedNull", PrefixItems: []*Schema{{}}, Items: nil},
  37. {Name: "tupleClosedFalse", PrefixItems: []*Schema{{}}, Items: nil},
  38. {Name: "tupleOpenTrue", PrefixItems: []*Schema{{}}, Items: &Schema{}},
  39. {Name: "tupleOpenEmpty", PrefixItems: []*Schema{{}}, Items: &Schema{}},
  40. {Name: "tupleOpenTyped", PrefixItems: []*Schema{{}}, Items: &Schema{Type: "boolean"}},
  41. {Name: "tupleOpenMax", PrefixItems: []*Schema{{}}, Items: &Schema{}, MaxItems: 3},
  42. {Name: "array", Items: &Schema{Type: "number"}},
  43. {Name: "null", Type: "null"},
  44. {Name: "string", Type: "string"},
  45. {Name: "boolean", Type: "boolean"},
  46. },
  47. }
  48. if diff := cmp.Diff(want, got); diff != "" {
  49. t.Errorf("(-want, +got)\n%s", diff)
  50. }
  51. }
  52. func TestEffectiveType(t *testing.T) {
  53. const schema = `
  54. {"properties": {
  55. "o": {"type": "object"},
  56. "a": {"type": "array"},
  57. "n": {"type": "number"},
  58. "s": {"type": "string"},
  59. "z": {"type": "null"},
  60. "b": {"type": "boolean"},
  61. "t0": {"prefixItems": [{}], "items": {"type": "number"}},
  62. "t1": {"items": {"type": "number"}, "maxItems": 3},
  63. "v": {"maxItems": 3}
  64. }}
  65. `
  66. var s *Schema
  67. if err := json.Unmarshal([]byte(schema), &s); err != nil {
  68. t.Fatalf("json.Unmarshal: %v", err)
  69. }
  70. var got []string
  71. for _, p := range s.Properties {
  72. got = append(got, p.EffectiveType())
  73. }
  74. want := strings.Fields(`
  75. object
  76. array
  77. number
  78. string
  79. null
  80. boolean
  81. array
  82. array
  83. value
  84. `)
  85. if !reflect.DeepEqual(want, got) {
  86. t.Errorf("\ngot:\n\t%v\nwant:\n\t%v", got, want)
  87. }
  88. }