Skip to content

Commit c0b7847

Browse files
committed
Add convenience function for "type" schema result parsing
The JSON schema validation result parsing infrastructure provides general purpose capabilities for identifying the instance and schema pointers related to a validation failure. However, when a specific type of schema keyword is used in multiple places, it is convenient to create a dedicated wrapper function to facilitate that use case. Although present in the schemas for the sake of completeness, there are no rules for checking schema validation results for the "type" keyword because the Arduino "properties" data format treats all values as strings. For this reason, there was no convenience function for this keyword. Due to using the JSON data format, which allows for any type, there will be type checking schema-based rules for the package index project type. So it now becomes worthwhile having a convenience parsing function for the "type" keyword.
1 parent fa2aa49 commit c0b7847

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

Diff for: internal/rule/schema/parsevalidationresult.go

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func PropertyDependenciesMissing(propertyName string, validationResult Validatio
5454
return ValidationErrorMatch("", "/dependencies/"+propertyName+"/[0-9]+$", "", "", validationResult)
5555
}
5656

57+
// PropertyTypeMismatch returns whether the given property has incorrect type.
58+
func PropertyTypeMismatch(propertyName string, validationResult ValidationResult) bool {
59+
return ValidationErrorMatch("^#/"+propertyName+"$", "/type$", "", "", validationResult)
60+
}
61+
5762
// PropertyFormatMismatch returns whether the given property has incorrect format.
5863
func PropertyFormatMismatch(propertyName string, validationResult ValidationResult) bool {
5964
return ValidationErrorMatch("^#/"+propertyName+"$", "/format$", "", "", validationResult)

Diff for: internal/rule/schema/schema_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,29 @@ func TestPropertyEnumMismatch(t *testing.T) {
153153
assert.True(t, PropertyEnumMismatch("property3", validationResult))
154154
}
155155

156+
func TestPropertyTypeMismatch(t *testing.T) {
157+
propertyName := "TestPropertyTypeMismatch"
158+
instanceTemplate := `
159+
{
160+
"%s": "foo"
161+
}
162+
`
163+
rawInstance := fmt.Sprintf(instanceTemplate, propertyName)
164+
var instance map[string]interface{}
165+
json.Unmarshal([]byte(rawInstance), &instance)
166+
167+
assert.False(t, PropertyTypeMismatch(propertyName, Validate(instance, validSchemaWithReferences)), "Property type is correct")
168+
169+
// Change property to incorrect type.
170+
pointerString := "/" + propertyName
171+
pointer, err := gojsonpointer.NewJsonPointer(pointerString)
172+
require.NoError(t, err)
173+
_, err = pointer.Set(instance, 1)
174+
require.NoError(t, err)
175+
176+
assert.True(t, PropertyTypeMismatch(propertyName, Validate(instance, validSchemaWithReferences)), "Property type is incorrect")
177+
}
178+
156179
func TestPropertyFormatMismatch(t *testing.T) {
157180
propertyName := "TestPropertyFormatMismatch"
158181
instanceTemplate := `

Diff for: internal/rule/schema/testdata/bindata.go

+12
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ var _referencedSchema2Json = []byte(`{
138138
]
139139
}
140140
},
141+
"TestPropertyTypeMismatch": {
142+
"object": {
143+
"type": "string"
144+
}
145+
},
141146
"TestPropertyFormatMismatch": {
142147
"object": {
143148
"format": "uri"
@@ -223,6 +228,13 @@ var _validSchemaWithReferencesJson = []byte(`{
223228
}
224229
]
225230
},
231+
"TestPropertyTypeMismatch": {
232+
"allOf": [
233+
{
234+
"$ref": "referenced-schema-2.json#/definitions/TestPropertyTypeMismatch/object"
235+
}
236+
]
237+
},
226238
"TestPropertyFormatMismatch": {
227239
"allOf": [
228240
{

Diff for: internal/rule/schema/testdata/input/referenced-schema-2.json

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
]
2727
}
2828
},
29+
"TestPropertyTypeMismatch": {
30+
"object": {
31+
"type": "string"
32+
}
33+
},
2934
"TestPropertyFormatMismatch": {
3035
"object": {
3136
"format": "uri"

Diff for: internal/rule/schema/testdata/input/valid-schema-with-references.json

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
}
3232
]
3333
},
34+
"TestPropertyTypeMismatch": {
35+
"allOf": [
36+
{
37+
"$ref": "referenced-schema-2.json#/definitions/TestPropertyTypeMismatch/object"
38+
}
39+
]
40+
},
3441
"TestPropertyFormatMismatch": {
3542
"allOf": [
3643
{

0 commit comments

Comments
 (0)