The config-file-validator can be called programmatically from within a Go program through the cli package.
Note: The
clipackage imports all validators, which pulls in dependencies for every supported file type. If you only need specific validators, importpkg/validatordirectly and use individual validators likevalidator.JSONValidator{}orvalidator.YAMLValidator{}without theclipackage.
The default configuration will perform the following actions:
package main
import (
"os"
"log"
"github.com/Boeing/config-file-validator/v2/pkg/cli"
)
func main() {
// Initialize the CLI
cfv := cli.Init()
// Run the config file validation
exitStatus, err := cfv.Run()
if err != nil {
log.Printf("Errors occurred during execution: %v", err)
}
os.Exit(exitStatus)
}
The below example will search the provided search paths.
package main
import (
"os"
"log"
"github.com/Boeing/config-file-validator/v2/pkg/cli"
"github.com/Boeing/config-file-validator/v2/pkg/finder"
)
func main() {
// Initialize a file system finder
fileSystemFinder := finder.FileSystemFinderInit(
finder.WithPathRoots("/path/to/search", "/another/path/to/search"),
)
// Initialize the CLI
cfv := cli.Init(
cli.WithFinder(fileSystemFinder),
)
// Run the config file validation
exitStatus, err := cfv.Run()
if err != nil {
log.Printf("Errors occurred during execution: %v", err)
}
os.Exit(exitStatus)
}
Will output JSON to stdout. To output to a file, pass a value to the reporter.NewJSONReporter constructor.
package main
import (
"os"
"log"
"github.com/Boeing/config-file-validator/v2/pkg/cli"
"github.com/Boeing/config-file-validator/v2/pkg/reporter"
)
func main() {
// Initialize reporter type
var outputDir string
jsonReporter := reporter.NewJSONReporter(outputDir)
// Initialize the CLI
cfv := cli.Init(
cli.WithFinder(fileSystemFinder),
cli.WithReporters(jsonReporter),
)
// Run the config file validation
exitStatus, err := cfv.Run()
if err != nil {
log.Printf("Errors occurred during execution: %v", err)
}
os.Exit(exitStatus)
}
excludeDirs := []string{"test", "subdir"}
fileSystemFinder := finder.FileSystemFinderInit(
finder.WithExcludeDirs(excludeDirs),
)
cfv := cli.Init(
cli.WithFinder(fileSystemFinder),
)
excludeFileTypes := []string{"yaml", "json"}
fileSystemFinder := finder.FileSystemFinderInit(
finder.WithExcludeFileTypes(excludeFileTypes),
)
cfv := cli.Init(
cli.WithFinder(fileSystemFinder),
)
fileSystemFinder := finder.FileSystemFinderInit(
finder.WithDepth(0)
)
cfv := cli.Init(
cli.WithFinder(fileSystemFinder),
)
cfv := cli.Init(
cli.WithQuiet(true),
)
groupOutput := []string{"pass-fail"}
cfv := cli.Init(
cli.WithGroupOutput(groupOutput),
)
Fail validation for files that support schema validation but don’t declare one.
cfv := cli.Init(
cli.WithRequireSchema(true),
)
Skip all schema validation. Only syntax is checked.
cfv := cli.Init(
cli.WithNoSchema(true),
)
Map file patterns to schema files. Use JSON Schema (.json) for JSON, YAML, TOML, and TOON files. Use XSD (.xsd) for XML files.
schemaMap := map[string]string{
"**/package.json": "schemas/package.schema.json",
"**/config.xml": "schemas/config.xsd",
}
cfv := cli.Init(
cli.WithSchemaMap(schemaMap),
)
Enable automatic schema lookup using the embedded SchemaStore catalog with remote fetching:
import "github.com/Boeing/config-file-validator/v2/pkg/schemastore"
// Use embedded catalog (schemas fetched remotely and cached)
store, err := schemastore.OpenEmbedded()
if err != nil {
log.Fatal(err)
}
cfv := cli.Init(
cli.WithSchemaStore(store),
)
For air-gapped environments, use a local clone:
store, err := schemastore.Open("/path/to/schemastore")
if err != nil {
log.Fatal(err)
}
cfv := cli.Init(
cli.WithSchemaStore(store),
)