Documentation ¶
Overview ¶
Package files provides various features to read, write and validate files with JSON schema.
Validation feature is based on JSON Schema (https://json-schema.org/) and santhosh-tekuri/jsonschema (https://github.com/santhosh-tekuri/jsonschema).
Example:
type config struct { ... } func main() { destdir, _ := os.Getwd() dest := filepath.Join(destdir, "filename.ext") if err := files.Validate( func(out any) error { return files.ReadJSON("path/to/schema.json", out, customfs.ReadFile) }, func(out any) error { return files.ReadYAML(dest, out, os.ReadFile) }, ); err != nil { logger.Fatal(err) } var c config if err := configuration.ReadYAML(dest, &config); err != nil { logger.Fatal(err) } ... if err := configuration.WriteYAML(dest, config, yaml.Indent(2), yaml.IndentSequence(true)); err != nil { logger.Fatal(err) } }
Index ¶
- Constants
- Variables
- func Exists(src string) bool
- func ReadJSON(src string, config any, read func(src string) ([]byte, error)) error
- func ReadYAML(src string, out any, read func(src string) ([]byte, error)) error
- func Validate(readSchema, readFile func(v any) error) error
- func WriteJSON(dst string, config any) error
- func WriteYAML(dst string, config any, opts ...yaml.EncodeOption) error
- type ValidationError
Constants ¶
const ( // Rw represents a file permission of read/write for current user // and no access for user's group and other groups. Rw fs.FileMode = 0o600 // RwRR represents a file permission of read/write for current user // and read-only access for user's group and other groups. RwRR fs.FileMode = 0o644 // RwRwRw represents a file permission of read/write for current user // and read/write too for user's group and other groups. RwRwRw fs.FileMode = 0o666 // RwxRxRxRx represents a file permission of read/write/execute for current user // and read/execute for user's group and other groups. RwxRxRxRx fs.FileMode = 0o755 )
Variables ¶
var ErrNilRead = errors.New("read function is nil")
ErrNilRead is returned when the read function is nil.
Functions ¶
func Exists ¶
Exists returns a boolean indicating whether the provided input src can be stat'ed or not.
func ReadJSON ¶
ReadJSON reads the input src and unmarshal it in JSON format into the out configuration.
func Validate ¶
Validate validates a given file (read from readFile) with a JSON schema (read from readSchema).
This function takes both delegated functions to be able to read from various fs.
Example:
func main() { err := files.Validate( func(out any) error { return files.ReadJSON("path/to/schema", out, os.ReadFile) }, func(out any) error { return files.ReadYAML("path/to/file/to/validate", out, os.ReadFile) }, ) // handle err }
Types ¶
type ValidationError ¶
ValidationError represents a simplified view of jsonschema.ValidationError.
It it used to override specific error messages (like kind.FalseSchema "false schema") in craft validation context.
func (*ValidationError) Error ¶
func (v *ValidationError) Error() string