Skip to content

Commit fa2aa49

Browse files
committed
Add convenience function for "format" 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 "format" keyword was only used for one thing in the library.properties schemas, which made it not worth a convenience function. The package index schema uses "format" keyword also, so it now becomes worthwhile having a convenience parsing function for it.
1 parent 004a330 commit fa2aa49

File tree

7 files changed

+74
-6
lines changed

7 files changed

+74
-6
lines changed

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ require (
4343
github.com/spf13/viper v1.7.1 // indirect
4444
github.com/stretchr/testify v1.6.1
4545
github.com/xanzy/ssh-agent v0.3.0 // indirect
46-
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
46+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb
4747
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415
4848
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18
4949
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.14.0 // indirect

Diff for: internal/project/library/libraryproperties/librarypropertiesschemas_test.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ func checkPropertyEnumMismatch(propertyName string, testTables []propertyValueTe
7979
}
8080
}
8181

82+
func checkPropertyFormatMismatch(propertyName string, testTables []propertyValueTestTable, t *testing.T) {
83+
libraryProperties := properties.NewFromHashmap(validLibraryPropertiesMap)
84+
var validationResult map[compliancelevel.Type]schema.ValidationResult
85+
86+
for _, testTable := range testTables {
87+
validationResult = changeValueUpdateValidationResult(propertyName, testTable.propertyValue, libraryProperties, validationResult)
88+
89+
t.Run(fmt.Sprintf("%s (%s)", testTable.testName, testTable.complianceLevel), func(t *testing.T) {
90+
testTable.assertion(t, schema.PropertyFormatMismatch(propertyName, validationResult[testTable.complianceLevel]))
91+
})
92+
}
93+
}
94+
8295
type validationErrorTestTable struct {
8396
testName string
8497
propertyValue string
@@ -322,13 +335,13 @@ func TestPropertiesCategoryEnum(t *testing.T) {
322335
}
323336

324337
func TestPropertiesUrlFormat(t *testing.T) {
325-
testTables := []validationErrorTestTable{
326-
{"Invalid URL format", "foo", "/format$", compliancelevel.Permissive, assert.False},
327-
{"Invalid URL format", "foo", "/format$", compliancelevel.Specification, assert.True},
328-
{"Invalid URL format", "foo", "/format$", compliancelevel.Strict, assert.True},
338+
testTables := []propertyValueTestTable{
339+
{"Invalid URL format", "foo", compliancelevel.Permissive, assert.False},
340+
{"Invalid URL format", "foo", compliancelevel.Specification, assert.True},
341+
{"Invalid URL format", "foo", compliancelevel.Strict, assert.True},
329342
}
330343

331-
checkValidationErrorMatch("url", testTables, t)
344+
checkPropertyFormatMismatch("url", testTables, t)
332345
}
333346

334347
func TestPropertiesDependsPattern(t *testing.T) {

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+
// PropertyFormatMismatch returns whether the given property has incorrect format.
58+
func PropertyFormatMismatch(propertyName string, validationResult ValidationResult) bool {
59+
return ValidationErrorMatch("^#/"+propertyName+"$", "/format$", "", "", validationResult)
60+
}
61+
5762
// MisspelledOptionalPropertyFound returns whether a misspelled optional property was found.
5863
func MisspelledOptionalPropertyFound(validationResult ValidationResult) bool {
5964
return ValidationErrorMatch("#/", "/misspelledOptionalProperties/", "", "", validationResult)

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

+26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package schema
1717

1818
import (
19+
"encoding/json"
20+
"fmt"
1921
"regexp"
2022
"testing"
2123

@@ -25,6 +27,7 @@ import (
2527
"github.com/ory/jsonschema/v3"
2628
"github.com/stretchr/testify/assert"
2729
"github.com/stretchr/testify/require"
30+
"github.com/xeipuuv/gojsonpointer"
2831
)
2932

3033
var validMap = map[string]string{
@@ -150,6 +153,29 @@ func TestPropertyEnumMismatch(t *testing.T) {
150153
assert.True(t, PropertyEnumMismatch("property3", validationResult))
151154
}
152155

156+
func TestPropertyFormatMismatch(t *testing.T) {
157+
propertyName := "TestPropertyFormatMismatch"
158+
instanceTemplate := `
159+
{
160+
"%s": "http://example.com"
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, PropertyFormatMismatch(propertyName, Validate(instance, validSchemaWithReferences)), "Property format 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, "foo")
174+
require.NoError(t, err)
175+
176+
assert.True(t, PropertyFormatMismatch(propertyName, Validate(instance, validSchemaWithReferences)), "Property format is incorrect")
177+
}
178+
153179
func TestPropertyDependenciesMissing(t *testing.T) {
154180
propertiesMap := properties.NewFromHashmap(validMap)
155181
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)

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+
"TestPropertyFormatMismatch": {
142+
"object": {
143+
"format": "uri"
144+
}
145+
},
141146
"misspelledOptionalProperties": {
142147
"propertyNames": {
143148
"not": {
@@ -217,6 +222,13 @@ var _validSchemaWithReferencesJson = []byte(`{
217222
"$ref": "referenced-schema-2.json#/definitions/notPatternObject"
218223
}
219224
]
225+
},
226+
"TestPropertyFormatMismatch": {
227+
"allOf": [
228+
{
229+
"$ref": "referenced-schema-2.json#/definitions/TestPropertyFormatMismatch/object"
230+
}
231+
]
220232
}
221233
},
222234
"allOf": [

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+
"TestPropertyFormatMismatch": {
30+
"object": {
31+
"format": "uri"
32+
}
33+
},
2934
"misspelledOptionalProperties": {
3035
"propertyNames": {
3136
"not": {

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

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

0 commit comments

Comments
 (0)