Implement validation command

I just need to fill base.schema with the simplest possible test case now
main
Nick Dumas 2 years ago
parent 436c553894
commit 7f1951947a

@ -16,6 +16,7 @@ var cfgFile string
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
TraverseChildren: true,
Use: "obp", Use: "obp",
Short: "obp is a toolkit for managing your vault in headless contexts", Short: "obp is a toolkit for managing your vault in headless contexts",
Long: `a suite of tools for managing your obsidian vault`, Long: `a suite of tools for managing your obsidian vault`,

@ -4,26 +4,81 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package cmd package cmd
import ( import (
"bytes"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gopkg.in/yaml.v3"
) )
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var validateCmd = &cobra.Command{ var validateCmd = &cobra.Command{
Use: "validate", Use: "validate",
Short: "loads a note and ensures its frontmatter follows the provided protobuf schema", Short: "loads a note and ensures its frontmatter follows the provided protobuf schema",
Long: `parses frontmatter in a markdown note and attempts to write the struct out using the provided protobuf schema. Long: `Validate YAML frontmatter with jsonschema
If the write succeeds, the data satisfies the schema.
`, `,
// Uncomment the following line if your bare application // Uncomment the following line if your bare application
// has an action associated with it: // has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { }, PreRunE: func(cmd *cobra.Command, args []string) error {
schemaFilename := cmd.Flag("schema").Value.String()
if len(schemaFilename) == 0 {
return fmt.Errorf("Please profide a schema filename")
}
target := cmd.Flag("target").Value.String()
if len(target) == 0 {
return fmt.Errorf("Please profide a target filename")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
var m interface{}
var schemaBytes bytes.Buffer
schemaFilename := cmd.Flag("schema").Value.String()
schemaFile, err := os.Open(schemaFilename)
if err != nil {
log.Fatalf("could not open schema file: %s\n", err)
}
io.Copy(&schemaBytes, schemaFile)
// err := yaml.Unmarshal([]byte(yamlText), &m)
targetFilename := cmd.Flag("target").Value.String()
target, err := os.Open(targetFilename)
if err != nil {
log.Fatalf("could not open target file: %s\n", err)
}
dec := yaml.NewDecoder(target)
err = dec.Decode(&m)
if err != nil {
log.Fatalf("error decoding YAML: %s\n", err)
}
compiler := jsonschema.NewCompiler()
if err := compiler.AddResource(schemaFilename, strings.NewReader(schemaBytes.String())); err != nil {
log.Fatalf("error adding resource to jsonschema compiler: %s\n", err)
}
schema, err := compiler.Compile("schema.json")
if err != nil {
log.Fatalf("error compiling schema: %s\n", err)
}
if err := schema.Validate(m); err != nil {
log.Fatalf("error validating: %s\n", err)
}
fmt.Println("validation successfull")
},
} }
func init() { func init() {
// Cobra also supports local flags, which will only run // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
validateCmd.Flags().StringP("schema", "s", "schema.proto", "path to protobuf file") validateCmd.Flags().StringP("schema", "s", "base.schema", "path to protobuf file")
validateCmd.Flags().StringP("target", "t", "", "list of filenames to validate")
rootCmd.AddCommand(validateCmd) rootCmd.AddCommand(validateCmd)
} }

Loading…
Cancel
Save