-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_schema.go
112 lines (83 loc) · 2.67 KB
/
json_schema.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cfschema
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/xeipuuv/gojsonschema"
)
// jsonSchema is an internal implementation for shared JSON Schema functionality.
type jsonSchema struct {
path string
loader gojsonschema.JSONLoader
schema *gojsonschema.Schema
source []byte
}
// validateDocument validates the provided document against the meta-schema.
func (s *jsonSchema) validateDocument(document string) error {
documentLoader := gojsonschema.NewStringLoader(document)
return s.validate(documentLoader)
}
// validateJsonSchema validates the provided jsonSchema against the meta-schema.
func (s *jsonSchema) validateJsonSchema(s2 jsonSchema) error {
return s.validate(s2.loader)
}
// validatePath validates the document at the provided file path against the meta-schema.
func (s *jsonSchema) validatePath(path string) error {
documentLoader := gojsonschema.NewReferenceLoader("file://" + path)
return s.validate(documentLoader)
}
// validate performs common validation logic.
func (s *jsonSchema) validate(loader gojsonschema.JSONLoader) error {
result, err := s.schema.Validate(loader)
if err != nil {
return fmt.Errorf("validating JSON Schema: %w", err)
}
if !result.Valid() {
var errs []error
for _, resultError := range result.Errors() {
errs = append(errs, errors.New(resultError.String()))
}
return fmt.Errorf("validation errors: %w", errors.Join(errs...))
}
return nil
}
// newJsonSchemaDocument returns a jsonSchema or any errors from a provided document.
func newJsonSchemaDocument(document string) (*jsonSchema, error) {
schemaLoader := gojsonschema.NewStringLoader(document)
schema, err := gojsonschema.NewSchema(schemaLoader)
if err != nil {
return nil, fmt.Errorf("loading JSON Schema (%s): %w", document, err)
}
return &jsonSchema{
loader: schemaLoader,
schema: schema,
source: []byte(document),
}, nil
}
// newJsonSchemaPath returns a jsonSchema or any errors from a provided document at the file path.
func newJsonSchemaPath(path string) (*jsonSchema, error) {
// To prevent reading the file twice to populate source bytes,
// manually read file path and use string handler.
f, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading file (%s): %w", path, err)
}
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("getting current directory: %w", err)
}
defer func() {
os.Chdir(cwd)
}()
// CD to the schema's directory so as to resolve any relative 'file://' URLs.
os.Chdir(filepath.Dir(path))
js, err := newJsonSchemaDocument(string(f))
if err != nil {
return nil, err
}
js.path = path
return js, nil
}