Skip to content

Allow full JSON pointers in schema validation result parsing functions #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions internal/project/packageindex/packageindexschemas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ func TestMinLength(t *testing.T) {
require.NoError(t, err)

t.Run(fmt.Sprintf("%s less than minimum length of %d (%s)", testTable.propertyPointerString, testTable.minLength, testTable.complianceLevel), func(t *testing.T) {
assert.True(t, schema.PropertyLessThanMinLength(strings.TrimPrefix(testTable.propertyPointerString, "/"), packageindex.Validate(packageIndex)[testTable.complianceLevel]))
assert.True(t, schema.PropertyLessThanMinLength(testTable.propertyPointerString, packageindex.Validate(packageIndex)[testTable.complianceLevel]))
})

// Test schema validation results with minimum value length.
propertyPointer.Set(packageIndex, strings.Repeat("a", testTable.minLength))

t.Run(fmt.Sprintf("%s at minimum length of %d (%s)", testTable.propertyPointerString, testTable.minLength, testTable.complianceLevel), func(t *testing.T) {
assert.False(t, schema.PropertyLessThanMinLength(strings.TrimPrefix(testTable.propertyPointerString, "/"), packageindex.Validate(packageIndex)[testTable.complianceLevel]))
assert.False(t, schema.PropertyLessThanMinLength(testTable.propertyPointerString, packageindex.Validate(packageIndex)[testTable.complianceLevel]))
})
}
}
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestRequired(t *testing.T) {

validationResult := packageindex.Validate(packageIndex)
t.Run(fmt.Sprintf("%s (%s)", testTable.propertyPointerString, testTable.complianceLevel), func(t *testing.T) {
testTable.assertion(t, schema.RequiredPropertyMissing(strings.TrimPrefix(testTable.propertyPointerString, "/"), validationResult[testTable.complianceLevel]))
testTable.assertion(t, schema.RequiredPropertyMissing(testTable.propertyPointerString, validationResult[testTable.complianceLevel]))
})
}
}
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestEnum(t *testing.T) {
require.NoError(t, err)

t.Run(fmt.Sprintf("%s: %s (%s)", testTable.propertyPointerString, testTable.propertyValue, testTable.complianceLevel), func(t *testing.T) {
testTable.assertion(t, schema.PropertyEnumMismatch(strings.TrimPrefix(testTable.propertyPointerString, "/"), packageindex.Validate(packageIndex)[testTable.complianceLevel]))
testTable.assertion(t, schema.PropertyEnumMismatch(testTable.propertyPointerString, packageindex.Validate(packageIndex)[testTable.complianceLevel]))
})
}
}
Expand Down Expand Up @@ -512,7 +512,7 @@ func TestPattern(t *testing.T) {
require.NoError(t, err)

t.Run(fmt.Sprintf("%s: %s (%s)", testTable.propertyPointerString, testTable.propertyValue, testTable.complianceLevel), func(t *testing.T) {
testTable.assertion(t, schema.PropertyPatternMismatch(strings.TrimPrefix(testTable.propertyPointerString, "/"), packageindex.Validate(packageIndex)[testTable.complianceLevel]))
testTable.assertion(t, schema.PropertyPatternMismatch(testTable.propertyPointerString, packageindex.Validate(packageIndex)[testTable.complianceLevel]))
})
}
}
Expand Down Expand Up @@ -567,7 +567,7 @@ func TestType(t *testing.T) {
_, err = propertyPointer.Set(packageIndex, testTable.propertyValue)

t.Run(fmt.Sprintf("%s: %v (%s)", testTable.propertyPointerString, testTable.propertyValue, complianceLevel), func(t *testing.T) {
testTable.assertion(t, schema.PropertyTypeMismatch(strings.TrimPrefix(testTable.propertyPointerString, "/"), packageindex.Validate(packageIndex)[complianceLevel]))
testTable.assertion(t, schema.PropertyTypeMismatch(testTable.propertyPointerString, packageindex.Validate(packageIndex)[complianceLevel]))
})
}
}
Expand Down Expand Up @@ -632,7 +632,7 @@ func TestFormat(t *testing.T) {
require.NoError(t, err)

t.Run(fmt.Sprintf("%s: %s (%s)", testTable.propertyPointerString, testTable.propertyValue, testTable.complianceLevel), func(t *testing.T) {
testTable.assertion(t, schema.PropertyFormatMismatch(strings.TrimPrefix(testTable.propertyPointerString, "/"), packageindex.Validate(packageIndex)[testTable.complianceLevel]))
testTable.assertion(t, schema.PropertyFormatMismatch(testTable.propertyPointerString, packageindex.Validate(packageIndex)[testTable.complianceLevel]))
})
}
}
Expand Down Expand Up @@ -693,7 +693,7 @@ func TestAdditionalProperties(t *testing.T) {
require.NoError(t, err)

t.Run(fmt.Sprintf("Additional property in the %s object (%s)", testTable.propertyPointerString, testTable.complianceLevel), func(t *testing.T) {
testTable.assertion(t, schema.ProhibitedAdditionalProperties(strings.TrimPrefix(testTable.propertyPointerString, "/"), packageindex.Validate(packageIndex)[testTable.complianceLevel]))
testTable.assertion(t, schema.ProhibitedAdditionalProperties(testTable.propertyPointerString, packageindex.Validate(packageIndex)[testTable.complianceLevel]))
})
}
}
16 changes: 8 additions & 8 deletions internal/rule/schema/parsevalidationresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,42 @@ import (

// RequiredPropertyMissing returns whether the given required property is missing from the document.
func RequiredPropertyMissing(propertyName string, validationResult ValidationResult) bool {
return ValidationErrorMatch("#", "/required$", "", "^#/"+propertyName+"$", validationResult)
return ValidationErrorMatch("#", "/required$", "", "^#/?"+propertyName+"$", validationResult)
}

// PropertyPatternMismatch returns whether the given property did not match the regular expression defined in the JSON schema.
func PropertyPatternMismatch(propertyName string, validationResult ValidationResult) bool {
return ValidationErrorMatch("#/"+propertyName, "/pattern$", "", "", validationResult)
return ValidationErrorMatch("#/?"+propertyName, "/pattern$", "", "", validationResult)
}

// PropertyLessThanMinLength returns whether the given property is less than the minimum length allowed by the schema.
func PropertyLessThanMinLength(propertyName string, validationResult ValidationResult) bool {
return ValidationErrorMatch("^#/"+propertyName+"$", "/minLength$", "", "", validationResult)
return ValidationErrorMatch("^#/?"+propertyName+"$", "/minLength$", "", "", validationResult)
}

// PropertyGreaterThanMaxLength returns whether the given property is greater than the maximum length allowed by the schema.
func PropertyGreaterThanMaxLength(propertyName string, validationResult ValidationResult) bool {
return ValidationErrorMatch("^#/"+propertyName+"$", "/maxLength$", "", "", validationResult)
return ValidationErrorMatch("^#/?"+propertyName+"$", "/maxLength$", "", "", validationResult)
}

// PropertyEnumMismatch returns whether the given property does not match any of the items in the enum array.
func PropertyEnumMismatch(propertyName string, validationResult ValidationResult) bool {
return ValidationErrorMatch("#/"+propertyName, "/enum$", "", "", validationResult)
return ValidationErrorMatch("#/?"+propertyName, "/enum$", "", "", validationResult)
}

// PropertyDependenciesMissing returns whether property dependencies of the given property are missing.
func PropertyDependenciesMissing(propertyName string, validationResult ValidationResult) bool {
return ValidationErrorMatch("", "/dependencies/"+propertyName+"/[0-9]+$", "", "", validationResult)
return ValidationErrorMatch("", "/dependencies/?"+propertyName+"/[0-9]+$", "", "", validationResult)
}

// PropertyTypeMismatch returns whether the given property has incorrect type.
func PropertyTypeMismatch(propertyName string, validationResult ValidationResult) bool {
return ValidationErrorMatch("^#/"+propertyName+"$", "/type$", "", "", validationResult)
return ValidationErrorMatch("^#/?"+propertyName+"$", "/type$", "", "", validationResult)
}

// PropertyFormatMismatch returns whether the given property has incorrect format.
func PropertyFormatMismatch(propertyName string, validationResult ValidationResult) bool {
return ValidationErrorMatch("^#/"+propertyName+"$", "/format$", "", "", validationResult)
return ValidationErrorMatch("^#/?"+propertyName+"$", "/format$", "", "", validationResult)
}

// ProhibitedAdditionalProperty returns whether the given property has prohibited additional subproperty(s).
Expand Down
5 changes: 2 additions & 3 deletions internal/rule/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"fmt"
"regexp"
"strings"
"testing"

"github.com/arduino/arduino-lint/internal/project/general"
Expand Down Expand Up @@ -228,7 +227,7 @@ func TestProhibitedAdditionalProperties(t *testing.T) {
var instance map[string]interface{}
json.Unmarshal([]byte(rawInstance), &instance)

assert.False(t, ProhibitedAdditionalProperties(strings.TrimPrefix(testTable.objectPointerString, "/"), Validate(instance, validSchemaWithReferences)), fmt.Sprintf("No additional properties in %s", testTable.objectPointerString))
assert.False(t, ProhibitedAdditionalProperties(testTable.objectPointerString, Validate(instance, validSchemaWithReferences)), fmt.Sprintf("No additional properties in %s", testTable.objectPointerString))

// Add additional property to object.
pointer, err := gojsonpointer.NewJsonPointer(testTable.objectPointerString + "/fooAdditionalProperty")
Expand All @@ -237,7 +236,7 @@ func TestProhibitedAdditionalProperties(t *testing.T) {
require.NoError(t, err)

t.Run(fmt.Sprintf("Additional property in the %s object", testTable.objectPointerString), func(t *testing.T) {
testTable.assertion(t, ProhibitedAdditionalProperties(strings.TrimPrefix(testTable.objectPointerString, "/"), Validate(instance, validSchemaWithReferences)))
testTable.assertion(t, ProhibitedAdditionalProperties(testTable.objectPointerString, Validate(instance, validSchemaWithReferences)))
})
}
}
Expand Down