-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresource_json_schema.go
75 lines (57 loc) · 1.69 KB
/
resource_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cfschema
import (
"encoding/json"
"fmt"
)
// ResourceJsonSchema represents the resource schema.
type ResourceJsonSchema struct {
jsonSchema
}
// Resource parses the JSON Schema and returns Resource or an error.
func (s *ResourceJsonSchema) Resource() (*Resource, error) {
if s == nil {
return nil, nil
}
var result Resource
err := json.Unmarshal(s.source, &result)
if err != nil {
return nil, fmt.Errorf("parsing JSON Schema into Resource: %w", err)
}
return &result, nil
}
// ValidateConfigurationDocument validates the provided document against the resource schema.
func (s *ResourceJsonSchema) ValidateConfigurationDocument(document string) error {
if s == nil {
return nil
}
return s.validateDocument(document)
}
// ValidateConfigurationPath validates the provided document at the file path against the resource schema.
func (s *ResourceJsonSchema) ValidateConfigurationPath(path string) error {
if s == nil {
return nil
}
return s.validatePath(path)
}
// NewResourceJsonSchemaDocument returns a ResourceJsonSchema or any errors from the provided document.
func NewResourceJsonSchemaDocument(document string) (*ResourceJsonSchema, error) {
js, err := newJsonSchemaDocument(document)
if err != nil {
return nil, err
}
return &ResourceJsonSchema{
jsonSchema: *js,
}, nil
}
// NewResourceJsonSchemaPath returns a ResourceJsonSchema or any errors from the provided document at the file path.
func NewResourceJsonSchemaPath(path string) (*ResourceJsonSchema, error) {
js, err := newJsonSchemaPath(path)
if err != nil {
return nil, err
}
return &ResourceJsonSchema{
jsonSchema: *js,
}, nil
}