Validate YAML Schema

Hi ,

I am trying to validate yaml schema which looks more like kubenetes deployment yaml but not the same.

version: v1
kind: Extension
metadata:
  name: 
  vendor-id:
  version:
artifacts:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - name: http
      containerPort: 80 
    volumeMounts:
    - mountPath: /test-ebs
      name: http-volume
  bundle:
  - name: my-bundle
    path: 
  web:
  - name: 
    path:

Fields containers, bundle, web are optional But one of them must exists. Found this discussion on stack overflow but it appears to be more than 3.5 years old. Wondering if there are any new/better way of going at it.

Thank you

The post that you linked to looks correct to me:

  • Define your model as a struct
  • Unmarshal into that model
  • Write the code that checks the unmarshaled result to see if it’s valid.

Adding to what @skillian said, a great starting point might be punching your yaml into https://zhwt.github.io/yaml-to-go/, which yields the following:

type AutoGenerated struct {
	Version  string `yaml:"version"`
	Kind     string `yaml:"kind"`
	Metadata struct {
		Name     interface{} `yaml:"name"`
		VendorID interface{} `yaml:"vendor-id"`
		Version  interface{} `yaml:"version"`
	} `yaml:"metadata"`
	Artifacts struct {
		Containers []struct {
			Name  string `yaml:"name"`
			Image string `yaml:"image"`
			Ports []struct {
				Name          string `yaml:"name"`
				ContainerPort int    `yaml:"containerPort"`
			} `yaml:"ports"`
			VolumeMounts []struct {
				MountPath string `yaml:"mountPath"`
				Name      string `yaml:"name"`
			} `yaml:"volumeMounts"`
		} `yaml:"containers"`
		Bundle []struct {
			Name string      `yaml:"name"`
			Path interface{} `yaml:"path"`
		} `yaml:"bundle"`
		Web []struct {
			Name interface{} `yaml:"name"`
			Path interface{} `yaml:"path"`
		} `yaml:"web"`
	} `yaml:"artifacts"`
}

Using that as a starting point, and hard-coding the yaml:

myYaml := []byte(`version: v1
kind: Extension
metadata:
  name: 
  vendor-id:
  version:
artifacts:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - name: http
      containerPort: 80 
    volumeMounts:
    - mountPath: /test-ebs
      name: http-volume
  bundle:
  - name: my-bundle
    path: 
  web:
  - name: 
    path:
`)

You could us the library mentioned in your StackOverflow link to unmarshal your yaml and then do whatever checking you want with it:

var myConfig AutoGenerated
err := yaml.Unmarshal(myYaml, &myConfig)
if err != nil {
	panic(err)
}

// Do whatever checking you want
fmt.Println(myConfig.Artifacts.Containers[0].Name, "Is a valid config option for container name")

That’s obviously contrived and not very safe. But it should get you started. You can run this example on the playground and edit it to actually satisfy your requirements.

@skillian @Dean_Davidson
Mean while I was going through compose-go and kubeval. It looks like they are reading Yaml and converting to JSON and then using JSON Validator.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.