@@ -6,11 +6,20 @@ package config
66import (
77 "errors"
88 "fmt"
9+ "regexp"
910
1011 "github.com/pb33f/libopenapi/index"
1112 "gopkg.in/yaml.v3"
1213)
1314
15+ // This regex matches attribute locations, dot-separated, as represented as {attribute_name}.{nested_attribute_name}
16+ // - category = MATCH
17+ // - category.id = MATCH
18+ // - category.tags.name = MATCH
19+ // - category. = NO MATCH
20+ // - .category = NO MATCH
21+ var attributeLocationRegex = regexp .MustCompile (`^[\w]+(?:\.[\w]+)*$` )
22+
1423// Config is tagged with `yaml` struct tags, however YAML is a superset of JSON, so it can also be parsed from JSON
1524type Config struct {
1625 Provider Provider `yaml:"provider"`
@@ -44,7 +53,12 @@ type SchemaOptions struct {
4453 AttributeOptions AttributeOptions `yaml:"attributes"`
4554}
4655type AttributeOptions struct {
47- Aliases map [string ]string `yaml:"aliases"`
56+ Aliases map [string ]string `yaml:"aliases"`
57+ Overrides map [string ]Override `yaml:"overrides"`
58+ }
59+
60+ type Override struct {
61+ Description string `yaml:"description"`
4862}
4963
5064// ParseConfig takes in a byte array (of YAML), unmarshal into a Config struct, and validates the result
@@ -138,6 +152,11 @@ func (r Resource) Validate() error {
138152 result = errors .Join (result , fmt .Errorf ("invalid delete: %w" , err ))
139153 }
140154
155+ err = r .SchemaOptions .Validate ()
156+ if err != nil {
157+ result = errors .Join (result , fmt .Errorf ("invalid schema: %w" , err ))
158+ }
159+
141160 return result
142161}
143162
@@ -153,6 +172,11 @@ func (d DataSource) Validate() error {
153172 result = errors .Join (result , fmt .Errorf ("invalid read: %w" , err ))
154173 }
155174
175+ err = d .SchemaOptions .Validate ()
176+ if err != nil {
177+ result = errors .Join (result , fmt .Errorf ("invalid schema: %w" , err ))
178+ }
179+
156180 return result
157181}
158182
@@ -172,3 +196,26 @@ func (o *OpenApiSpecLocation) Validate() error {
172196
173197 return result
174198}
199+
200+ func (s * SchemaOptions ) Validate () error {
201+ var result error
202+
203+ err := s .AttributeOptions .Validate ()
204+ if err != nil {
205+ result = errors .Join (result , fmt .Errorf ("invalid attributes: %w" , err ))
206+ }
207+
208+ return result
209+ }
210+
211+ func (s * AttributeOptions ) Validate () error {
212+ var result error
213+
214+ for path := range s .Overrides {
215+ if ! attributeLocationRegex .MatchString (path ) {
216+ result = errors .Join (result , fmt .Errorf ("invalid key for override: %q - must be dot-separated string" , path ))
217+ }
218+ }
219+
220+ return result
221+ }
0 commit comments