Skip to content

Commit 47f9cd1

Browse files
authored
Merge pull request #25 from arduino/per1234/library_properties-schema
Add permissive and strict library.properties JSON schemas
2 parents 105bc86 + 3a0bf88 commit 47f9cd1

17 files changed

+1792
-57
lines changed

check/checkdata/library.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package checkdata
1717

1818
import (
19+
"github.com/arduino/arduino-check/check/checkdata/schema/compliancelevel"
1920
"github.com/arduino/arduino-check/configuration"
2021
"github.com/arduino/arduino-check/project"
2122
"github.com/arduino/arduino-check/project/library/libraryproperties"
@@ -48,9 +49,9 @@ func LibraryProperties() *properties.Map {
4849
return libraryProperties
4950
}
5051

51-
var libraryPropertiesSchemaValidationResult *jsonschema.ValidationError
52+
var libraryPropertiesSchemaValidationResult map[compliancelevel.Type]*jsonschema.ValidationError
5253

5354
// LibraryPropertiesSchemaValidationResult returns the result of validating library.properties against the JSON schema.
54-
func LibraryPropertiesSchemaValidationResult() *jsonschema.ValidationError {
55+
func LibraryPropertiesSchemaValidationResult() map[compliancelevel.Type]*jsonschema.ValidationError {
5556
return libraryPropertiesSchemaValidationResult
5657
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This file is part of arduino-check.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-check.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
// Package compliance level defines the levels of specification compliance.
17+
package compliancelevel
18+
19+
// Type is the type for the compliance levels.
20+
//go:generate stringer -type=Type -linecomment
21+
type Type int
22+
23+
// The line comments set the string for each level.
24+
const (
25+
Permissive Type = iota // permissive
26+
Specification // standard
27+
Strict // strict
28+
)

check/checkdata/schema/compliancelevel/type_string.go

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

check/checkdata/schema/parsevalidationresult.go

+20
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ func PropertyPatternMismatch(propertyName string, validationResult *jsonschema.V
3535
return ValidationErrorMatch("#/"+propertyName, "/pattern$", "", "", validationResult, schemasPath)
3636
}
3737

38+
// PropertyLessThanMinLength returns whether the given property is less than the minimum length allowed by the schema.
39+
func PropertyLessThanMinLength(propertyName string, validationResult *jsonschema.ValidationError, schemasPath *paths.Path) bool {
40+
return ValidationErrorMatch("^#/"+propertyName+"$", "/minLength$", "", "", validationResult, schemasPath)
41+
}
42+
43+
// PropertyGreaterThanMaxLength returns whether the given property is greater than the maximum length allowed by the schema.
44+
func PropertyGreaterThanMaxLength(propertyName string, validationResult *jsonschema.ValidationError, schemasPath *paths.Path) bool {
45+
return ValidationErrorMatch("^#/"+propertyName+"$", "/maxLength$", "", "", validationResult, schemasPath)
46+
}
47+
48+
// PropertyEnumMismatch returns whether the given property does not match any of the items in the enum array.
49+
func PropertyEnumMismatch(propertyName string, validationResult *jsonschema.ValidationError, schemasPath *paths.Path) bool {
50+
return ValidationErrorMatch("#/"+propertyName, "/enum$", "", "", validationResult, schemasPath)
51+
}
52+
53+
// MisspelledOptionalPropertyFound returns whether a misspelled optional property was found.
54+
func MisspelledOptionalPropertyFound(validationResult *jsonschema.ValidationError, schemasPath *paths.Path) bool {
55+
return ValidationErrorMatch("#/", "/misspelledOptionalProperties/", "", "", validationResult, schemasPath)
56+
}
57+
3858
// ValidationErrorMatch returns whether the given query matches against the JSON schema validation error.
3959
// See: https://godoc.org/github.com/ory/jsonschema#ValidationError
4060
func ValidationErrorMatch(

check/checkdata/schema/schema_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// This file is part of arduino-check.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-check.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
116
package schema
217

318
import (
@@ -17,6 +32,7 @@ var schemasPath *paths.Path
1732
var validMap = map[string]string{
1833
"property1": "foo",
1934
"property2": "bar",
35+
"property3": "baz",
2036
}
2137

2238
var validPropertiesMap = properties.NewFromHashmap(validMap)
@@ -107,6 +123,46 @@ func TestPropertyPatternMismatch(t *testing.T) {
107123
require.False(t, PropertyPatternMismatch("property1", validationResult, schemasPath))
108124
}
109125

126+
func TestPropertyLessThanMinLength(t *testing.T) {
127+
propertiesMap := properties.NewFromHashmap(validMap)
128+
validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath)
129+
require.False(t, PropertyLessThanMinLength("property1", validationResult, schemasPath))
130+
131+
propertiesMap.Set("property1", "a")
132+
validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath)
133+
require.True(t, PropertyLessThanMinLength("property1", validationResult, schemasPath))
134+
}
135+
136+
func TestPropertyGreaterThanMaxLength(t *testing.T) {
137+
propertiesMap := properties.NewFromHashmap(validMap)
138+
validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath)
139+
require.False(t, PropertyGreaterThanMaxLength("property1", validationResult, schemasPath))
140+
141+
propertiesMap.Set("property1", "12345")
142+
validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath)
143+
require.True(t, PropertyGreaterThanMaxLength("property1", validationResult, schemasPath))
144+
}
145+
146+
func TestPropertyEnumMismatch(t *testing.T) {
147+
propertiesMap := properties.NewFromHashmap(validMap)
148+
validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath)
149+
require.False(t, PropertyEnumMismatch("property3", validationResult, schemasPath))
150+
151+
propertiesMap.Set("property3", "invalid")
152+
validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath)
153+
require.True(t, PropertyEnumMismatch("property3", validationResult, schemasPath))
154+
}
155+
156+
func TestMisspelledOptionalPropertyFound(t *testing.T) {
157+
propertiesMap := properties.NewFromHashmap(validMap)
158+
validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath)
159+
require.False(t, MisspelledOptionalPropertyFound(validationResult, schemasPath))
160+
161+
propertiesMap.Set("porperties", "foo")
162+
validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath)
163+
require.True(t, MisspelledOptionalPropertyFound(validationResult, schemasPath))
164+
}
165+
110166
func TestValidationErrorMatch(t *testing.T) {
111167
propertiesMap := properties.NewFromHashmap(validMap)
112168
validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath)

check/checkdata/schema/testdata/referenced-schema-2.json

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
"definitions": {
66
"minLengthObject": {
77
"minLength": 2
8+
},
9+
"maxLengthObject": {
10+
"maxLength": 4
11+
},
12+
"enumObject": {
13+
"enum": ["baz"]
14+
},
15+
"misspelledOptionalProperties": {
16+
"propertyNames": {
17+
"not": {
18+
"pattern": "porpert([y]|[ies])"
19+
}
20+
}
821
}
922
}
1023
}

check/checkdata/schema/testdata/valid-schema-with-references.json

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"allOf": [
99
{
1010
"$ref": "referenced-schema-2.json#/definitions/minLengthObject"
11+
},
12+
{
13+
"$ref": "referenced-schema-2.json#/definitions/maxLengthObject"
1114
}
1215
]
1316
},
@@ -17,11 +20,21 @@
1720
"$ref": "referenced-schema-1.json#/definitions/patternObject"
1821
}
1922
]
23+
},
24+
"property3": {
25+
"allOf": [
26+
{
27+
"$ref": "referenced-schema-2.json#/definitions/enumObject"
28+
}
29+
]
2030
}
2131
},
2232
"allOf": [
2333
{
2434
"$ref": "referenced-schema-1.json#/definitions/requiredObject"
35+
},
36+
{
37+
"$ref": "referenced-schema-2.json#/definitions/misspelledOptionalProperties"
2538
}
2639
]
2740
}

check/checkfunctions/library.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package checkfunctions
2020
import (
2121
"github.com/arduino/arduino-check/check/checkdata"
2222
"github.com/arduino/arduino-check/check/checkdata/schema"
23+
"github.com/arduino/arduino-check/check/checkdata/schema/compliancelevel"
2324
"github.com/arduino/arduino-check/check/checkresult"
2425
"github.com/arduino/arduino-check/configuration"
2526
)
@@ -38,7 +39,7 @@ func LibraryPropertiesNameFieldMissing() (result checkresult.Type, output string
3839
return checkresult.NotRun, ""
3940
}
4041

41-
if schema.RequiredPropertyMissing("name", checkdata.LibraryPropertiesSchemaValidationResult(), configuration.SchemasPath()) {
42+
if schema.RequiredPropertyMissing("name", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Specification], configuration.SchemasPath()) {
4243
return checkresult.Fail, ""
4344
}
4445
return checkresult.Pass, ""
@@ -50,7 +51,7 @@ func LibraryPropertiesNameFieldDisallowedCharacters() (result checkresult.Type,
5051
return checkresult.NotRun, ""
5152
}
5253

53-
if schema.PropertyPatternMismatch("name", checkdata.LibraryPropertiesSchemaValidationResult(), configuration.SchemasPath()) {
54+
if schema.PropertyPatternMismatch("name", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Specification], configuration.SchemasPath()) {
5455
return checkresult.Fail, ""
5556
}
5657

@@ -63,7 +64,7 @@ func LibraryPropertiesVersionFieldMissing() (result checkresult.Type, output str
6364
return checkresult.NotRun, ""
6465
}
6566

66-
if schema.RequiredPropertyMissing("version", checkdata.LibraryPropertiesSchemaValidationResult(), configuration.SchemasPath()) {
67+
if schema.RequiredPropertyMissing("version", checkdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Specification], configuration.SchemasPath()) {
6768
return checkresult.Fail, ""
6869
}
6970
return checkresult.Pass, ""

0 commit comments

Comments
 (0)