Add JSON schema for package index · arduino/arduino-lint@5861954 (original) (raw)

`@@ -20,12 +20,55 @@ See: https://arduino.github.io/arduino-cli/latest/package_index_json-specificati

`

20

20

`package packageindex

`

21

21

``

22

22

`import (

`

``

23

`+

"encoding/json"

`

23

24

`"fmt"

`

24

25

`"regexp"

`

25

26

``

``

27

`+

"github.com/arduino/arduino-lint/internal/rule/schema"

`

``

28

`+

"github.com/arduino/arduino-lint/internal/rule/schema/compliancelevel"

`

``

29

`+

"github.com/arduino/arduino-lint/internal/rule/schema/schemadata"

`

26

30

`"github.com/arduino/go-paths-helper"

`

27

31

`)

`

28

32

``

``

33

`+

// Properties parses the package index from the given path and returns the data.

`

``

34

`+

func Properties(packageIndexPath *paths.Path) (map[string]interface{}, error) {

`

``

35

`+

rawIndex, err := packageIndexPath.ReadFile()

`

``

36

`+

if err != nil {

`

``

37

`+

panic(err)

`

``

38

`+

}

`

``

39

`+

var indexData map[string]interface{}

`

``

40

`+

err = json.Unmarshal(rawIndex, &indexData)

`

``

41

`+

if err != nil {

`

``

42

`+

return nil, err

`

``

43

`+

}

`

``

44

+

``

45

`+

return indexData, nil

`

``

46

`+

}

`

``

47

+

``

48

`+

var schemaObject = make(map[compliancelevel.Type]schema.Schema)

`

``

49

+

``

50

`+

// Validate validates boards.txt data against the JSON schema and returns a map of the result for each compliance level.

`

``

51

`+

func Validate(packageIndex map[string]interface{}) map[compliancelevel.Type]schema.ValidationResult {

`

``

52

`+

referencedSchemaFilenames := []string{

`

``

53

`+

"general-definitions-schema.json",

`

``

54

`+

"arduino-package-index-definitions-schema.json",

`

``

55

`+

}

`

``

56

+

``

57

`+

var validationResults = make(map[compliancelevel.Type]schema.ValidationResult)

`

``

58

+

``

59

`+

if schemaObject[compliancelevel.Permissive].Compiled == nil { // Only compile the schemas once.

`

``

60

`+

schemaObject[compliancelevel.Permissive] = schema.Compile("arduino-package-index-permissive-schema.json", referencedSchemaFilenames, schemadata.Asset)

`

``

61

`+

schemaObject[compliancelevel.Specification] = schema.Compile("arduino-package-index-schema.json", referencedSchemaFilenames, schemadata.Asset)

`

``

62

`+

schemaObject[compliancelevel.Strict] = schema.Compile("arduino-package-index-strict-schema.json", referencedSchemaFilenames, schemadata.Asset)

`

``

63

`+

}

`

``

64

+

``

65

`+

validationResults[compliancelevel.Permissive] = schema.Validate(packageIndex, schemaObject[compliancelevel.Permissive])

`

``

66

`+

validationResults[compliancelevel.Specification] = schema.Validate(packageIndex, schemaObject[compliancelevel.Specification])

`

``

67

`+

validationResults[compliancelevel.Strict] = schema.Validate(packageIndex, schemaObject[compliancelevel.Strict])

`

``

68

+

``

69

`+

return validationResults

`

``

70

`+

}

`

``

71

+

29

72

`var empty struct{}

`

30

73

``

31

74

`// Reference: https://arduino.github.io/arduino-cli/latest/package_index_json-specification/#naming-of-the-json-index-file

`