DEV Community

Alex Spinov
Alex Spinov

Posted on

CUE Has a Free API: The Language That Validates Your YAML, JSON, and Protobuf

Your Kubernetes YAML has 200 lines. Line 147 has a typo. You won't find it until the deploy fails at 2 AM. CUE finds it at write time.

What Is CUE?

CUE is a data validation language and configuration system from the creator of Go's template package. It unifies types, values, and constraints into one concept.

// Define a constraint (schema)
#Deployment: {
    apiVersion: "apps/v1"
    kind:       "Deployment"
    metadata: {
        name:      string & =~"^[a-z][a-z0-9-]*$"
        namespace: string | *"default"
    }
    spec: {
        replicas: int & >=1 & <=100
        selector: matchLabels: [string]: string
        template: spec: containers: [...#Container]
    }
}

#Container: {
    name:  string
    image: string & =~"^[a-z].*:.*$"
    ports: [...{containerPort: int & >0 & <=65535}]
    resources: {
        limits: {cpu: string; memory: string}
        requests: {cpu: string; memory: string}
    }
}

// Use it — CUE validates automatically
myApp: #Deployment & {
    metadata: name: "my-app"
    spec: {
        replicas: 3
        selector: matchLabels: app: "my-app"
        template: spec: containers: [{
            name:  "app"
            image: "myapp:v1.2.3"
            ports: [{containerPort: 8080}]
            resources: {
                limits: {cpu: "500m", memory: "256Mi"}
                requests: {cpu: "100m", memory: "128Mi"}
            }
        }]
    }
}
Enter fullscreen mode Exit fullscreen mode

Validation

# Validate
cue vet deployment.cue

# Export to YAML
cue export deployment.cue --out yaml

# Export to JSON
cue export deployment.cue --out json

# Validate existing YAML against CUE schema
cue vet schema.cue deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Why CUE

  • Validate anything — YAML, JSON, Protobuf, Go structs
  • Merge & override — layer configurations safely
  • No Turing-completeness — guaranteed termination (unlike Jsonnet)
  • Go integration — import CUE schemas in Go code
  • Used by Dagger — CUE powers Dagger CI/CD pipelines
go install cuelang.org/go/cmd/cue@latest
Enter fullscreen mode Exit fullscreen mode

Building configuration and validation systems? Check out my developer tools or email spinov001@gmail.com.

Top comments (0)