Skip to main content
AllDevToolsHub
Back to all patterns

YAML Key Validation

Validation

Validates simple YAML keys: letters, digits, underscores, and hyphens, starting with a letter or underscore.

/^[a-zA-Z_][a-zA-Z0-9_-]*$/

How it works

YAML keys can technically be any scalar value, but this pattern validates the most common convention for configuration file keys: alphanumeric with underscores and hyphens, starting with a letter or underscore.

Test Cases

Should Match

  • database_url
  • max-connections
  • PORT
  • _internal

Should NOT Match

  • 1invalid
  • has space
  • has.dot

Quick Summary

Validates common YAML configuration keys: letters, digits, underscores, and hyphens, starting with a letter or underscore. YAML technically allows any scalar as a key, but this pattern enforces the most common convention.

Key Takeaways

Key Takeaways

  • Must start with a letter or underscore
  • Allows letters, digits, underscores, and hyphens
  • YAML technically allows any scalar as a key, this pattern enforces a common convention
  • Dots are not allowed, use nested keys (parent.child) as separate levels instead
Use Cases

When to use it

  • Validating configuration key names in YAML schema validators
  • Checking key names in Helm chart values files
  • Linting Kubernetes manifest keys
Watch out

Common Mistakes

  • Using dots in YAML keys, dots are valid in YAML but can cause confusion with nested key notation
  • Not quoting YAML keys that contain special characters, unquoted keys with colons or hashes break YAML parsing
FAQ

YAML Key Validation, Frequently Asked

Do YAML keys need to be quoted?

Only if they contain special characters like :, #, or start with special YAML characters. Simple alphanumeric keys do not need quotes.

Are YAML keys case-sensitive?

Yes. 'Port' and 'port' are different keys in YAML.