Skip to content

Commit

Permalink
Draft implementation for json schema export
Browse files Browse the repository at this point in the history
Signed-off-by: Mathias Petermann <mathias.petermann@gmail.com>
  • Loading branch information
peschmae committed Apr 20, 2024
1 parent 2e686ef commit 162f40c
Show file tree
Hide file tree
Showing 4 changed files with 778 additions and 9 deletions.
14 changes: 11 additions & 3 deletions pkg/cmd/template/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (o *Options) RunWithFiles(in Input, ui ui.UI) Output {
if err != nil {
return Output{Err: err}
}
if schemaType == RegularFilesOutputTypeOpenAPI {
if schemaType == RegularFilesOutputTypeOpenAPI || schemaType == RegularFilesOutputTypeJSONSchema {
return Output{Err: fmt.Errorf("Output type currently only supported for data values schema (i.e. include --data-values-schema-inspect)")}
}

Expand Down Expand Up @@ -190,8 +190,16 @@ func (o *Options) inspectSchema(dataValuesSchema *datavalues.Schema) Output {
},
}
}
return Output{Err: fmt.Errorf("Data values schema export only supported in OpenAPI v3 format; specify format with --output=%s flag",
RegularFilesOutputTypeOpenAPI)}
if format == RegularFilesOutputTypeJSONSchema {
jsonSchemaDoc := schema.NewJSONSchemaDocument(dataValuesSchema.GetDocumentType())
return Output{
DocSet: &yamlmeta.DocumentSet{
Items: []*yamlmeta.Document{jsonSchemaDoc.AsDocument()},
},
}
}
return Output{Err: fmt.Errorf("Data values schema export only supported in OpenAPI v3 and JSON Schema format; specify format with --output=(%s|%s) flag",
RegularFilesOutputTypeOpenAPI, RegularFilesOutputTypeJSONSchema)}
}

func (o *Options) pickSource(srcs []FileSource, pickFunc func(FileSource) bool) FileSource {
Expand Down
7 changes: 4 additions & 3 deletions pkg/cmd/template/regular_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@ const (

// When the FileSource are RegularFilesSource, indicates which schema type to use when rendering the output.
const (
RegularFilesOutputTypeOpenAPI = "openapi-v3"
RegularFilesOutputTypeNone = ""
RegularFilesOutputTypeOpenAPI = "openapi-v3"
RegularFilesOutputTypeJSONSchema = "json-schema"
RegularFilesOutputTypeNone = ""
)

// Collections of each category of output type
var (
RegularFilesOutputFormatTypes = []string{RegularFilesOutputTypeYAML, RegularFilesOutputTypeJSON, RegularFilesOutputTypePos}
RegularFilesOutputSchemaTypes = []string{RegularFilesOutputTypeOpenAPI}
RegularFilesOutputSchemaTypes = []string{RegularFilesOutputTypeOpenAPI, RegularFilesOutputTypeJSONSchema}
RegularFilesOutputTypes = append(RegularFilesOutputFormatTypes, RegularFilesOutputSchemaTypes...)
)

Expand Down
Loading

0 comments on commit 162f40c

Please sign in to comment.