Skip to content

Commit c39f587

Browse files
committed
Create libraryproperties package and add validation helper functions
1 parent c04946b commit c39f587

File tree

3 files changed

+69
-31
lines changed

3 files changed

+69
-31
lines changed

check/checkdata/checkdata.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package checkdata
22

33
import (
44
"github.com/arduino/arduino-check/project"
5-
"github.com/arduino/arduino-check/project/library"
5+
"github.com/arduino/arduino-check/project/library/libraryproperties"
66
"github.com/arduino/arduino-check/project/projecttype"
77
"github.com/arduino/go-paths-helper"
88
"github.com/arduino/go-properties-orderedmap"
@@ -45,12 +45,12 @@ func Initialize(project project.Type) {
4545
switch project.ProjectType {
4646
case projecttype.Sketch:
4747
case projecttype.Library:
48-
libraryProperties, libraryPropertiesLoadError = library.Properties(project.Path)
48+
libraryProperties, libraryPropertiesLoadError = libraryproperties.Properties(project.Path)
4949
if libraryPropertiesLoadError != nil {
50-
libraryPropertiesSchemaValidationResult = library.ValidateProperties(libraryProperties)
51-
} else {
5250
// TODO: can I even do this?
5351
libraryPropertiesSchemaValidationResult = nil
52+
} else {
53+
libraryPropertiesSchemaValidationResult = libraryproperties.Validate(libraryProperties)
5454
}
5555
case projecttype.Platform:
5656
case projecttype.PackageIndex:

project/library/libraryproperties.go

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package libraryproperties
2+
3+
import (
4+
"net/url"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/arduino/go-paths-helper"
9+
"github.com/arduino/go-properties-orderedmap"
10+
"github.com/xeipuuv/gojsonschema"
11+
)
12+
13+
func Properties(libraryPath *paths.Path) (*properties.Map, error) {
14+
libraryProperties, err := properties.Load(libraryPath.Join("library.properties").String())
15+
if err != nil {
16+
return nil, err
17+
}
18+
return libraryProperties, nil
19+
}
20+
21+
func Validate(libraryProperties *properties.Map) *gojsonschema.Result {
22+
workingPath, _ := os.Getwd()
23+
schemaPath := paths.New(workingPath).Join("arduino-library-properties-schema.json")
24+
uriFriendlySchemaPath := filepath.ToSlash(schemaPath.String())
25+
schemaPathURI := url.URL{
26+
Scheme: "file",
27+
Path: uriFriendlySchemaPath,
28+
}
29+
schemaLoader := gojsonschema.NewReferenceLoader(schemaPathURI.String())
30+
31+
documentLoader := gojsonschema.NewGoLoader(libraryProperties)
32+
33+
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
34+
if err != nil {
35+
panic(err.Error())
36+
}
37+
38+
return result
39+
}
40+
41+
func FieldMissing(fieldName string, validationResult *gojsonschema.Result) bool {
42+
return ValidationErrorMatch("required", "(root)", fieldName+" is required", validationResult)
43+
}
44+
45+
func FieldPatternMismatch(fieldName string, validationResult *gojsonschema.Result) bool {
46+
return ValidationErrorMatch("pattern", fieldName, "", validationResult)
47+
}
48+
49+
func ValidationErrorMatch(typeQuery string, fieldQuery string, descriptionQuery string, validationResult *gojsonschema.Result) bool {
50+
if validationResult.Valid() {
51+
// No error, so nothing to match
52+
return false
53+
}
54+
for _, validationError := range validationResult.Errors() {
55+
if typeQuery == "" || typeQuery == validationError.Type() {
56+
if fieldQuery == "" || fieldQuery == validationError.Field() {
57+
if descriptionQuery == "" || descriptionQuery == validationError.Description() {
58+
return true
59+
}
60+
}
61+
}
62+
}
63+
64+
return false
65+
}

0 commit comments

Comments
 (0)