JSON to CUE Constraint Generator

Generate CUE constraint definitions from JSON data with type inference, optional fields, and validation rules.

What is JSON to CUE Constraint Generator?

CUE (Configuration Unification Engine) is an open-source data constraint language that merges types, values, and constraints into a single declarative model — unlike JSON Schema or Protobuf, which separate type definitions from data. CUE excels at validating configuration files, API schemas, and data pipelines because every CUE definition is simultaneously a type and a value, enabling powerful unification and default-value logic. This generator inspects your JSON sample, infers the correct CUE primitive type for each field (`string`, `int`, `number`, `bool`), marks null fields as optional with the `?` syntax, and can optionally add value constraints like `>=0`, `<=1000`, `maxLen(255)`, and regex patterns for email and URL fields. Nested objects become nested CUE struct definitions, and the output supports both closed structs (no extra fields allowed) and open structs (extensible with `...`).

How to Use

  1. Configure options: toggle closed structs for strict validation, package declaration for multi-file projects, and value constraints for range and pattern enforcement
  2. Paste a representative JSON configuration or API response into the input area
  3. Click "Generate CUE" to produce constraint definitions with inferred types and optional validation rules
  4. Save the output as a .cue file and validate it with the cue vet command before integrating into your project
  5. Use cue export to generate JSON or YAML from your CUE definitions, or cue eval to compute unified values

Why Use This Tool?

Infers CUE primitive types from JSON values — string, int, number, bool — without requiring manual type annotations
Null values automatically generate optional fields with the ? syntax, following CUE conventions for nullable data
Value constraints add practical validation: range limits for numbers, regex patterns for email and URL fields, and maxLen for strings
Closed structs prevent typos and unexpected fields in configuration data, catching mistakes at validation time rather than runtime
Package declarations enable multi-file CUE projects where definitions are shared across modules

Tips & Best Practices

  • Enable "Closed structs" for Kubernetes configs and CI/CD pipelines where extra fields indicate a typo or misconfiguration — open structs are better for extensible application configs
  • Enable "Include value constraints" to get automatic regex patterns for email and URL fields, plus range limits for numbers based on the sample values
  • CUE distinguishes between int (whole numbers) and number (any numeric) — the generator uses int for integers and number for floats, matching CUE semantics
  • Use the #Name syntax for type definitions (hidden from output) and Name for concrete values — the generator uses #Root for the main definition to keep it separate from data
  • Run cue vet ./... after saving the .cue file to validate all data files against the generated constraints

Frequently Asked Questions

How does JSON map to CUE types?

JSON strings map to string, integers map to int, floating-point numbers map to number, booleans map to bool, null values produce optional fields with the ? suffix, arrays become [...Type] open lists, and nested objects become nested CUE struct definitions with proper indentation.

When should I avoid using this generator?

Skip this tool if your CUE definitions need default values, comprehensions, or conditional logic — the generator produces type and constraint definitions only, not CUE expressions. Also avoid it if you need to define recursive types or references to external packages, which require manual CUE authoring.

What are closed vs open structs?

Closed structs (the default) do not allow additional fields beyond those defined, providing strict validation — any extra field causes a vet error. Open structs (using the ... marker) allow extra fields, which is useful for extensible configurations where downstream tools may add their own keys.

How do value constraints work in CUE?

Value constraints are conjunctions in CUE — a field like port: int & >=1 & <=65535 means the value must be an integer AND at least 1 AND at most 65535. The generator creates these constraints from your sample data, estimating reasonable ranges for numbers and regex patterns for known formats like email.

Can I use this with Kubernetes or Docker Compose?

Yes. CUE is increasingly used for Kubernetes configuration validation. Save the generated .cue file, then use cue vet to validate your Kubernetes YAML manifests against the constraints. For Docker Compose, generate CUE constraints from a sample docker-compose.json and validate future configurations.

Is my data sent to a server?

No. All constraint generation runs entirely in your browser. Your JSON data never leaves your device — no network requests, no server-side processing, and no data retention.

Real-world Examples

Kubernetes deployment config validation

Generate CUE constraints for a Kubernetes deployment configuration to catch misconfigurations before applying to the cluster.

Input
{
  "name": "web-app",
  "replicas": 3,
  "image": "nginx:latest",
  "port": 8080,
  "isProduction": false,
  "healthCheck": {
    "path": "/healthz",
    "interval": 30
  }
}
Output
package main

#HealthCheck: {
  path: string & =~"^/"
  interval: int & >=1 & <=300
}

#Root: {
  name: string & maxLen(63)
  replicas: int & >=1 & <=30
  image: string & =~"^\S+:\S+$"
  port: int & >=1 & <=65535
  isProduction: bool
  healthCheck: #HealthCheck
}

API configuration schema

Create CUE constraints for an application configuration file with email and URL validation.

Input
{
  "apiUrl": "https://api.example.com",
  "adminEmail": "admin@example.com",
  "timeout": 30,
  "retries": 3,
  "debug": false
}
Output
package main

#Root: {
  apiUrl: string & =~"^https?://"
  adminEmail: string & =~"^[^@]+@[^@]+\.[^@]+$"
  timeout: int & >=1 & <=300
  retries: int & >=0 & <=30
  debug: bool
}

Related Tools