/
openapi_spec.go
141 lines (127 loc) · 3 KB
/
openapi_spec.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package config
import (
"encoding/json"
"fmt"
"github.com/go-openapi/spec"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/validate"
"github.com/go-openapi/validate/post"
"github.com/hashicorp/go-multierror"
"sigs.k8s.io/yaml"
)
const (
// TODO: use embedded file openapi_spec.yaml instead
schemaYaml = `type: object
required:
- giterminismConfigVersion
additionalProperties: {}
properties:
giterminismConfigVersion:
type: string
enum: ["1"]
config:
$ref: '#/definitions/Config'
helm:
$ref: '#/definitions/Helm'
definitions:
Config:
type: object
additionalProperties: {}
properties:
allowUncommitted:
type: boolean
goTemplateRendering:
$ref: '#/definitions/ConfigGoTemplateRendering'
stapel:
$ref: '#/definitions/ConfigStapel'
dockerfile:
$ref: '#/definitions/ConfigDockerfile'
ConfigGoTemplateRendering:
type: object
additionalProperties: {}
properties:
allowEnvVariables:
type: array
items:
type: string
ConfigStapel:
type: object
additionalProperties: {}
properties:
mount:
$ref: '#/definitions/ConfigStapelMount'
ConfigStapelMount:
type: object
additionalProperties: {}
properties:
allowBuildDir:
type: boolean
allowFromPaths:
type: array
items:
type: string
ConfigDockerfile:
type: object
additionalProperties: {}
properties:
allowUncommitted:
type: array
items:
type: string
allowUncommittedDockerignoreFiles:
type: array
items:
type: string
allowContextAddFile:
type: array
items:
type: string
Helm:
type: object
additionalProperties: {}
properties:
allowUncommittedFiles:
type: array
items:
type: string
`
)
func openAPISchema() *spec.Schema {
hash := map[string]interface{}{}
if err := yaml.UnmarshalStrict([]byte(schemaYaml), &hash); err != nil {
panic(fmt.Sprint("unexpected error: ", err))
}
data, err := json.Marshal(hash)
if err != nil {
panic(fmt.Sprint("unexpected error: ", err))
}
schema := &spec.Schema{}
if err := json.Unmarshal(data, schema); err != nil {
panic(fmt.Sprint("unexpected error: ", err))
}
err = spec.ExpandSchema(schema, schema, nil)
if err != nil {
panic(fmt.Sprint("unexpected error: ", err))
}
return schema
}
func processWithOpenAPISchema(dataObj *[]byte) error {
validator := validate.NewSchemaValidator(openAPISchema(), nil, "", strfmt.Default)
var blank map[string]interface{}
err := yaml.Unmarshal(*dataObj, &blank)
if err != nil {
return err
}
result := validator.Validate(blank)
if result.IsValid() {
post.ApplyDefaults(result)
*dataObj, err = json.Marshal(result.Data())
if err != nil {
panic(fmt.Sprint("unexpected error: ", err))
}
return nil
}
var allErrs *multierror.Error
allErrs = multierror.Append(allErrs, result.Errors...)
return allErrs.ErrorOrNil()
}