-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (97 loc) · 2.27 KB
/
Copy pathmain.go
File metadata and controls
114 lines (97 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"os"
"github.com/santhosh-tekuri/jsonschema/v6"
)
type validationError struct {
tokenType TokenType
err error
file string
}
type TokenType string
const (
ERC20 TokenType = "erc20"
ERC721 TokenType = "erc721"
ERC1155 TokenType = "erc1155"
)
func main() {
var errors []validationError
comp := jsonschema.NewCompiler()
// Single schema for all token types
schemaLocation := "schemas/tokenSchema.json"
// Data file locations by token type
dataFiles := map[TokenType][2]string{
ERC20: {
"data/testnet/erc20.json",
"data/mainnet/erc20.json",
},
ERC721: {
"data/testnet/erc721.json",
"data/mainnet/erc721.json",
},
ERC1155: {
"data/testnet/erc1155.json",
"data/mainnet/erc1155.json",
},
}
// Compile the single schema
if _, err := os.Stat(schemaLocation); os.IsNotExist(err) {
fmt.Printf("Schema file not found: %s\n", err)
os.Exit(1)
}
schema, err := comp.Compile(schemaLocation)
if err != nil {
fmt.Printf("Schema compilation error: %s\n", err)
os.Exit(1)
}
// Validate each data file against the schema
for tokenType, locations := range dataFiles {
for _, fileLocation := range locations {
if _, err := os.Stat(fileLocation); os.IsNotExist(err) {
errors = append(errors, validationError{
tokenType: tokenType,
err: err,
file: fileLocation,
})
continue
}
file, err := os.Open(fileLocation)
if err != nil {
errors = append(errors, validationError{
tokenType: tokenType,
err: err,
file: fileLocation,
})
continue
}
inst, err := jsonschema.UnmarshalJSON(file)
if err != nil {
errors = append(errors, validationError{
tokenType: tokenType,
err: err,
file: fileLocation,
})
file.Close()
continue
}
file.Close()
err = schema.Validate(inst)
if err != nil {
errors = append(errors, validationError{
tokenType: tokenType,
err: err,
file: fileLocation,
})
}
}
}
if len(errors) > 0 {
fmt.Println("Validation errors occurred:")
for _, err := range errors {
fmt.Printf("%s: %s in file %s\n", err.tokenType, err.err.Error(), err.file)
}
os.Exit(1)
}
fmt.Println("All token configurations validated successfully.")
}