This module provides YAML serialization/deserialization for Dgo using the gopkg.in/yaml.v3 module.
To use dgoyaml, first install the latest version of the library:
go get github.com/tada/dgoyaml
The dgo CLI command can be used to get acquainted with dgo concepts. It will allow you to declare types and values in YAML files and then use the types to validate the values. You install the command under $GOPATH/bin with:
go install github.com/tada/dgoyaml/cli/dgo
after that, you should be able to do:
dgo help
to get a description of avaliable sub commands and flags.
Let's assume some kind of typed parameters in YAML that the user enters like this:
host: example.com
port: 22
The task is to create a user friendly description of a parameter, also in YAML, which can be used to validate the above parameters. Something like this:
host:
type: string[1]
name: sample/service_host
required: true
port:
type: 1..999
name: sample/service_port
The value of each type
is a dgo type. They limit the host
parameter to a non empty string and the port parameter to an integer in the range 1-999. A special required
entry is
used to denote whether or not a parameter value must be present. The name
entry is optional and provides a freeform
text identifier.
Put the two above YAML examples in two separate files, params.yaml
and params_spec.yaml
. Then run the
command:
dgo validate --verbose --input params.yaml --spec params_spec.yaml
The output should be:
Got input yaml with:
host: example.com
port: 22
Validating 'host' against definition string[1]
'host' OK!
Validating 'port' against definition 1..999
'port' OK!
For examples of how to use the library functions that Dgo provides to perform the above validation in, please take a look at parameter_test.go. The source of the validate command may also be of help.