diff --git a/_examples/text/regex/regex.yml b/_examples/text/regex/regex.yml new file mode 100644 index 0000000..163956f --- /dev/null +++ b/_examples/text/regex/regex.yml @@ -0,0 +1,7 @@ +variables: + - name: Text + type: text + regex: ^[a-z]+$ # only allow lowercase letters + description: A string of lowercase letters +template: |- + {{ .Text }} diff --git a/cmd/serve.go b/cmd/serve.go index d2a9b7c..cdf4b49 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -6,6 +6,7 @@ import ( "github.com/gttp-cli/gttp/pkg/model" "github.com/gttp-cli/gttp/pkg/parser" "github.com/spf13/cobra" + "strings" ) func init() { @@ -47,7 +48,15 @@ var serveCmd = &cobra.Command{ }) } - tmpl, err := model.FromYAML(body.Template) + var tmpl model.Template + var err error + + if strings.HasPrefix(body.Template, "{") { + tmpl, err = model.FromJSON(body.Template) + } else { + tmpl, err = model.FromYAML(body.Template) + } + if err != nil { return c.Status(400).JSON(map[string]string{ "error": err.Error(),