Skip to content

Commit

Permalink
Merge pull request #90 from rsteube/schema-use-examples
Browse files Browse the repository at this point in the history
schema: use `examples` to skip validation
  • Loading branch information
rsteube authored Oct 16, 2022
2 parents 49df573 + 46f744a commit 60c8d41
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
24 changes: 24 additions & 0 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,39 @@ import (
"fmt"
"os"
"regexp"
"sort"
"strings"

"github.com/invopop/jsonschema"
"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
)

// static value or macro
type action string

func (action) JSONSchema() *jsonschema.Schema {
sortedNames := make([]string, 0, len(macros))
for name := range macros {
sortedNames = append(sortedNames, name)
}
sort.Strings(sortedNames)

examples := make([]interface{}, 0, len(macros))
for _, name := range sortedNames {
examples = append(examples, fmt.Sprintf("$%v(%v)", name, macros[name].Signature()))
}
return &jsonschema.Schema{
Type: "string",
Title: "Action",
Description: "A static value or a macro",
Examples: examples,
}
}

// ActionMacro completes given macro
func ActionMacro(s string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
Expand Down
17 changes: 0 additions & 17 deletions command.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
package spec

import (
"github.com/invopop/jsonschema"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

// static value or macro
type action string

func (action) JSONSchema() *jsonschema.Schema {
enum := make([]interface{}, 0, len(macros))
for macro := range macros {
enum = append(enum, "$"+macro) // TODO full signature as in `carapace --macros XX`
}
return &jsonschema.Schema{
Type: "string",
Title: "Action",
Description: "A static value or a macro",
Enum: enum,
}
}

type Command struct {
Name string `json:"name" jsonschema_description:"Name of the command"`
Aliases []string `json:"aliases,omitempty" jsonschema_description:"Aliases of the command"`
Expand Down

0 comments on commit 60c8d41

Please sign in to comment.