@@ -46,6 +46,23 @@ func init() {
46
46
)
47
47
}
48
48
49
+ // propertiesToMap converts properties.Map data structures to map[string]interface.
50
+ func propertiesToMap (propertiesInput * properties.Map ) map [string ]interface {} {
51
+ mapOutput := make (map [string ]interface {})
52
+ keys := propertiesInput .FirstLevelKeys ()
53
+ for _ , key := range keys {
54
+ subTree := propertiesInput .SubTree (key )
55
+ if subTree .Size () > 0 {
56
+ // This key contains a map, recurse it.
57
+ mapOutput [key ] = propertiesToMap (subTree )
58
+ } else {
59
+ // This key contains a string, no more recursion is possible.
60
+ mapOutput [key ] = propertiesInput .Get (key )
61
+ }
62
+ }
63
+ return mapOutput
64
+ }
65
+
49
66
func TestCompile (t * testing.T ) {
50
67
require .Panics (t , func () {
51
68
Compile ("valid-schema-with-references.json" , []string {"nonexistent.json" }, testdata .Asset )
@@ -82,87 +99,87 @@ func TestCompile(t *testing.T) {
82
99
func TestValidate (t * testing.T ) {
83
100
schemaObject := Compile ("valid-schema.json" , []string {}, testdata .Asset )
84
101
propertiesMap := properties .NewFromHashmap (validMap )
85
- validationResult := Validate (propertiesMap , schemaObject )
102
+ validationResult := Validate (propertiesToMap ( propertiesMap ) , schemaObject )
86
103
require .Nil (t , validationResult .Result )
87
104
88
- validationResult = Validate (propertiesMap , validSchemaWithReferences )
105
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
89
106
require .Nil (t , validationResult .Result )
90
107
91
108
propertiesMap .Set ("property1" , "a" )
92
- validationResult = Validate (propertiesMap , schemaObject )
109
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , schemaObject )
93
110
require .Equal (t , "#/property1" , validationResult .Result .InstancePtr )
94
111
require .Equal (t , "#/properties/property1/minLength" , validationResult .Result .SchemaPtr )
95
112
}
96
113
97
114
func TestRequiredPropertyMissing (t * testing.T ) {
98
115
propertiesMap := properties .NewFromHashmap (validMap )
99
- validationResult := Validate (propertiesMap , validSchemaWithReferences )
116
+ validationResult := Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
100
117
require .False (t , RequiredPropertyMissing ("property1" , validationResult ))
101
118
102
119
propertiesMap .Remove ("property1" )
103
- validationResult = Validate (propertiesMap , validSchemaWithReferences )
120
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
104
121
require .True (t , RequiredPropertyMissing ("property1" , validationResult ))
105
122
}
106
123
107
124
func TestPropertyPatternMismatch (t * testing.T ) {
108
125
propertiesMap := properties .NewFromHashmap (validMap )
109
- validationResult := Validate (propertiesMap , validSchemaWithReferences )
126
+ validationResult := Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
110
127
require .False (t , PropertyPatternMismatch ("property2" , validationResult ))
111
128
112
129
propertiesMap .Set ("property2" , "fOo" )
113
- validationResult = Validate (propertiesMap , validSchemaWithReferences )
130
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
114
131
require .True (t , PropertyPatternMismatch ("property2" , validationResult ))
115
132
116
133
require .False (t , PropertyPatternMismatch ("property1" , validationResult ))
117
134
}
118
135
119
136
func TestPropertyLessThanMinLength (t * testing.T ) {
120
137
propertiesMap := properties .NewFromHashmap (validMap )
121
- validationResult := Validate (propertiesMap , validSchemaWithReferences )
138
+ validationResult := Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
122
139
require .False (t , PropertyLessThanMinLength ("property1" , validationResult ))
123
140
124
141
propertiesMap .Set ("property1" , "a" )
125
- validationResult = Validate (propertiesMap , validSchemaWithReferences )
142
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
126
143
require .True (t , PropertyLessThanMinLength ("property1" , validationResult ))
127
144
}
128
145
129
146
func TestPropertyGreaterThanMaxLength (t * testing.T ) {
130
147
propertiesMap := properties .NewFromHashmap (validMap )
131
- validationResult := Validate (propertiesMap , validSchemaWithReferences )
148
+ validationResult := Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
132
149
require .False (t , PropertyGreaterThanMaxLength ("property1" , validationResult ))
133
150
134
151
propertiesMap .Set ("property1" , "12345" )
135
- validationResult = Validate (propertiesMap , validSchemaWithReferences )
152
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
136
153
require .True (t , PropertyGreaterThanMaxLength ("property1" , validationResult ))
137
154
}
138
155
139
156
func TestPropertyEnumMismatch (t * testing.T ) {
140
157
propertiesMap := properties .NewFromHashmap (validMap )
141
- validationResult := Validate (propertiesMap , validSchemaWithReferences )
158
+ validationResult := Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
142
159
require .False (t , PropertyEnumMismatch ("property3" , validationResult ))
143
160
144
161
propertiesMap .Set ("property3" , "invalid" )
145
- validationResult = Validate (propertiesMap , validSchemaWithReferences )
162
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
146
163
require .True (t , PropertyEnumMismatch ("property3" , validationResult ))
147
164
}
148
165
149
166
func TestMisspelledOptionalPropertyFound (t * testing.T ) {
150
167
propertiesMap := properties .NewFromHashmap (validMap )
151
- validationResult := Validate (propertiesMap , validSchemaWithReferences )
168
+ validationResult := Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
152
169
require .False (t , MisspelledOptionalPropertyFound (validationResult ))
153
170
154
171
propertiesMap .Set ("porperties" , "foo" )
155
- validationResult = Validate (propertiesMap , validSchemaWithReferences )
172
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
156
173
require .True (t , MisspelledOptionalPropertyFound (validationResult ))
157
174
}
158
175
159
176
func TestValidationErrorMatch (t * testing.T ) {
160
177
propertiesMap := properties .NewFromHashmap (validMap )
161
- validationResult := Validate (propertiesMap , validSchemaWithReferences )
178
+ validationResult := Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
162
179
require .False (t , ValidationErrorMatch ("" , "" , "" , "" , validationResult ))
163
180
164
181
propertiesMap .Set ("property2" , "fOo" )
165
- validationResult = Validate (propertiesMap , validSchemaWithReferences )
182
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
166
183
require .False (t , ValidationErrorMatch ("nomatch" , "nomatch" , "nomatch" , "nomatch" , validationResult ))
167
184
require .False (t , ValidationErrorMatch ("^#/property2$" , "nomatch" , "nomatch" , "nomatch" , validationResult ))
168
185
require .False (t , ValidationErrorMatch ("^#/property2$" , "/pattern$" , "nomatch" , "nomatch" , validationResult ))
@@ -171,12 +188,12 @@ func TestValidationErrorMatch(t *testing.T) {
171
188
require .True (t , ValidationErrorMatch ("" , "" , "" , "" , validationResult ))
172
189
173
190
propertiesMap .Set ("property3" , "bAz" )
174
- validationResult = Validate (propertiesMap , validSchemaWithReferences )
191
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
175
192
require .True (t , ValidationErrorMatch ("^#/property3$" , "/pattern$" , "" , "" , validationResult ), "Match pointer below logic inversion keyword" )
176
193
177
194
propertiesMap = properties .NewFromHashmap (validMap )
178
195
propertiesMap .Remove ("property1" )
179
- validationResult = Validate (propertiesMap , validSchemaWithReferences )
196
+ validationResult = Validate (propertiesToMap ( propertiesMap ) , validSchemaWithReferences )
180
197
require .False (t , ValidationErrorMatch ("nomatch" , "nomatch" , "nomatch" , "nomatch" , validationResult ))
181
198
require .True (t , ValidationErrorMatch ("" , "" , "" , "^#/property1$" , validationResult ))
182
199
}
0 commit comments