Skip to content

Commit a11044a

Browse files
committed
Add convenience function for "additionalProperties" 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. Previously the "additionalProperties" keyword was not used in any rules, which made it not worth a convenience function. The package index schema uses the "additionalProperties" keyword, so it now becomes worthwhile having a convenience parsing function for it.
1 parent c0b7847 commit a11044a

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ func PropertyFormatMismatch(propertyName string, validationResult ValidationResu
6464
return ValidationErrorMatch("^#/"+propertyName+"$", "/format$", "", "", validationResult)
6565
}
6666

67+
// ProhibitedAdditionalProperty returns whether the given property has prohibited additional subproperty(s).
68+
func ProhibitedAdditionalProperties(propertyName string, validationResult ValidationResult) bool {
69+
return ValidationErrorMatch("^#/?"+propertyName+"$", "/additionalProperties$", "", "", validationResult)
70+
}
71+
6772
// MisspelledOptionalPropertyFound returns whether a misspelled optional property was found.
6873
func MisspelledOptionalPropertyFound(validationResult ValidationResult) bool {
6974
return ValidationErrorMatch("#/", "/misspelledOptionalProperties/", "", "", validationResult)

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

+43
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"regexp"
22+
"strings"
2223
"testing"
2324

2425
"github.com/arduino/arduino-lint/internal/project/general"
@@ -199,6 +200,48 @@ func TestPropertyFormatMismatch(t *testing.T) {
199200
assert.True(t, PropertyFormatMismatch(propertyName, Validate(instance, validSchemaWithReferences)), "Property format is incorrect")
200201
}
201202

203+
func TestProhibitedAdditionalProperties(t *testing.T) {
204+
propertyName := "TestProhibitedAdditionalProperties"
205+
instanceTemplate := `
206+
{
207+
"%s": {
208+
"additionalPropertiesTrue": {
209+
"fooProperty": "bar"
210+
},
211+
"additionalPropertiesFalse": {
212+
"fooProperty": "bar"
213+
}
214+
}
215+
}
216+
`
217+
218+
testTables := []struct {
219+
objectPointerString string
220+
assertion assert.BoolAssertionFunc
221+
}{
222+
{"/TestProhibitedAdditionalProperties/additionalPropertiesTrue", assert.False},
223+
{"/TestProhibitedAdditionalProperties/additionalPropertiesFalse", assert.True},
224+
}
225+
226+
for _, testTable := range testTables {
227+
rawInstance := fmt.Sprintf(instanceTemplate, propertyName)
228+
var instance map[string]interface{}
229+
json.Unmarshal([]byte(rawInstance), &instance)
230+
231+
assert.False(t, ProhibitedAdditionalProperties(strings.TrimPrefix(testTable.objectPointerString, "/"), Validate(instance, validSchemaWithReferences)), fmt.Sprintf("No additional properties in %s", testTable.objectPointerString))
232+
233+
// Add additional property to object.
234+
pointer, err := gojsonpointer.NewJsonPointer(testTable.objectPointerString + "/fooAdditionalProperty")
235+
require.NoError(t, err)
236+
_, err = pointer.Set(instance, "bar")
237+
require.NoError(t, err)
238+
239+
t.Run(fmt.Sprintf("Additional property in the %s object", testTable.objectPointerString), func(t *testing.T) {
240+
testTable.assertion(t, ProhibitedAdditionalProperties(strings.TrimPrefix(testTable.objectPointerString, "/"), Validate(instance, validSchemaWithReferences)))
241+
})
242+
}
243+
}
244+
202245
func TestPropertyDependenciesMissing(t *testing.T) {
203246
propertiesMap := properties.NewFromHashmap(validMap)
204247
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)

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

+16
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,22 @@ var _validSchemaWithReferencesJson = []byte(`{
241241
"$ref": "referenced-schema-2.json#/definitions/TestPropertyFormatMismatch/object"
242242
}
243243
]
244+
},
245+
"TestProhibitedAdditionalProperties": {
246+
"properties": {
247+
"additionalPropertiesTrue": {
248+
"properties": {
249+
"fooProperty": {}
250+
},
251+
"additionalProperties": true
252+
},
253+
"additionalPropertiesFalse": {
254+
"properties": {
255+
"fooProperty": {}
256+
},
257+
"additionalProperties": false
258+
}
259+
}
244260
}
245261
},
246262
"allOf": [

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

+16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@
4444
"$ref": "referenced-schema-2.json#/definitions/TestPropertyFormatMismatch/object"
4545
}
4646
]
47+
},
48+
"TestProhibitedAdditionalProperties": {
49+
"properties": {
50+
"additionalPropertiesTrue": {
51+
"properties": {
52+
"fooProperty": {}
53+
},
54+
"additionalProperties": true
55+
},
56+
"additionalPropertiesFalse": {
57+
"properties": {
58+
"fooProperty": {}
59+
},
60+
"additionalProperties": false
61+
}
62+
}
4763
}
4864
},
4965
"allOf": [

0 commit comments

Comments
 (0)