...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package gojsonschema
27
28 import (
29 "errors"
30 "fmt"
31 "strings"
32 )
33
34 type jsonSchemaType struct {
35 types []string
36 }
37
38
39
40 func (t *jsonSchemaType) IsTyped() bool {
41 return len(t.types) > 0
42 }
43
44 func (t *jsonSchemaType) Add(etype string) error {
45
46 if !isStringInSlice(JSON_TYPES, etype) {
47 return errors.New(formatErrorDescription(Locale.NotAValidType(), ErrorDetails{"given": "/" + etype + "/", "expected": JSON_TYPES}))
48 }
49
50 if t.Contains(etype) {
51 return errors.New(formatErrorDescription(Locale.Duplicated(), ErrorDetails{"type": etype}))
52 }
53
54 t.types = append(t.types, etype)
55
56 return nil
57 }
58
59 func (t *jsonSchemaType) Contains(etype string) bool {
60
61 for _, v := range t.types {
62 if v == etype {
63 return true
64 }
65 }
66
67 return false
68 }
69
70 func (t *jsonSchemaType) String() string {
71
72 if len(t.types) == 0 {
73 return STRING_UNDEFINED
74 }
75
76
77 if len(t.types) > 1 {
78 return fmt.Sprintf("[%s]", strings.Join(t.types, ","))
79 }
80
81
82 return t.types[0]
83 }
84
View as plain text