diff --git a/pkg/helm/helmtoschema.go b/pkg/helm/helmtoschema.go index 12cd1d4..1124158 100644 --- a/pkg/helm/helmtoschema.go +++ b/pkg/helm/helmtoschema.go @@ -53,7 +53,9 @@ func HelmToSchema(valuesPath string) result.SchemaResult { // the top level node is a document node. We need to go one layer // deeper to get the actual yaml content - result.Diags = parseMapNode(sch, valuesDocument.Content[0], result.Diags) + if len(valuesDocument.Content) > 0 { + result.Diags = parseMapNode(sch, valuesDocument.Content[0], result.Diags) + } return result } diff --git a/pkg/schema/expand.go b/pkg/schema/expand.go index ec8e8d3..1103a47 100644 --- a/pkg/schema/expand.go +++ b/pkg/schema/expand.go @@ -27,10 +27,12 @@ func ExpandProperties(schema *Schema) *orderedmap.OrderedMap[string, *Schema] { result = mergeProperties(result, allOf) } - // dependencies - for _, item := range schema.Dependencies { - dep := ExpandProperties(item) - result = mergeProperties(result, dep) + // if dependencies is a map, we need to expand it + if dependentSchema, ok := schema.Dependencies.(map[string]*Schema); ok { + for _, item := range dependentSchema { + dep := ExpandProperties(item) + result = mergeProperties(result, dep) + } } return result diff --git a/pkg/schema/marshal.go b/pkg/schema/marshal.go index 7418450..cc42711 100644 --- a/pkg/schema/marshal.go +++ b/pkg/schema/marshal.go @@ -12,6 +12,15 @@ func (s *Schema) MarshalJSON() ([]byte, error) { s.AdditionalPropertiesRaw = &addPropRaw } + if s.Dependencies != nil { + dependenciesBytes, err := json.Marshal(s.Dependencies) + if err != nil { + return nil, err + } + dependenciesRaw := json.RawMessage(dependenciesBytes) + s.DependenciesRaw = &dependenciesRaw + } + type Alias Schema return json.Marshal(&struct { *Alias @@ -52,5 +61,22 @@ func (s *Schema) UnmarshalJSON(data []byte) error { s.AdditionalPropertiesRaw = nil } + if s.DependenciesRaw != nil { + var dependentRequired map[string][]string + err := json.Unmarshal(*s.DependenciesRaw, &dependentRequired) + if err != nil { + // dependencies is map, try to unmarshal as map[string]*Schema + var dependentSchema map[string]*Schema + err = json.Unmarshal(*s.DependenciesRaw, &dependentSchema) + if err != nil { + return err + } + s.Dependencies = dependentSchema + } else { + // dependencies is map[string][]string + s.Dependencies = dependentRequired + } + } + return nil } diff --git a/pkg/schema/types.go b/pkg/schema/types.go index 9e88bc3..bc48cd6 100644 --- a/pkg/schema/types.go +++ b/pkg/schema/types.go @@ -23,10 +23,11 @@ type Schema struct { OneOf []*Schema `json:"oneOf,omitempty"` // section 10.2.1.3 Not *Schema `json:"not,omitempty"` // section 10.2.1.4 // RFC draft-bhutton-json-schema-00 section 10.2.2 (Apply sub-schemas conditionally) - If *Schema `json:"if,omitempty"` // section 10.2.2.1 - Then *Schema `json:"then,omitempty"` // section 10.2.2.2 - Else *Schema `json:"else,omitempty"` // section 10.2.2.3 - Dependencies map[string]*Schema `json:"dependencies,omitempty"` // section 10.2.2.4 + If *Schema `json:"if,omitempty"` // section 10.2.2.1 + Then *Schema `json:"then,omitempty"` // section 10.2.2.2 + Else *Schema `json:"else,omitempty"` // section 10.2.2.3 + DependenciesRaw *json.RawMessage `json:"dependencies,omitempty"` // section 10.2.2.4 + Dependencies any `json:"-"` // section 10.2.2.4 // RFC draft-bhutton-json-schema-00 section 10.3.1 (arrays) PrefixItems []*Schema `json:"prefixItems,omitempty"` // section 10.3.1.1 Items *Schema `json:"items,omitempty"` // section 10.3.1.2 (replaces additionalItems) @@ -36,7 +37,7 @@ type Schema struct { Properties *orderedmap.OrderedMap[string, *Schema] `json:"properties,omitempty"` PatternProperties map[string]*Schema `json:"patternProperties,omitempty"` // section 10.3.2.2 AdditionalPropertiesRaw *json.RawMessage `json:"additionalProperties,omitempty"` // section 10.3.2.3 - AdditionalProperties interface{} `json:"-"` + AdditionalProperties any `json:"-"` // AdditionalProperties *Schema `json:"additionalProperties,omitempty"` // section 10.3.2.3 PropertyNames *Schema `json:"propertyNames,omitempty"` // section 10.3.2.4 // RFC draft-bhutton-json-schema-validation-00, section 6