Use Regex Pattern Matching in YAML Schema
Learn how to apply regex pattern constraints in YAML Schema to validate string formats. Covers the pattern keyword, patternProperties for dynamic keys, common patterns for URLs, emails, and semver.
Detailed Explanation
Pattern Matching in YAML Schema
The pattern keyword validates string values against a regular expression. When converting JSON to YAML Schema, an intelligent converter detects common formats and automatically applies appropriate patterns.
Example JSON Input
{
"serviceName": "my-api-service",
"version": "2.1.0",
"endpoint": "https://api.example.com/v2",
"apiKey": "sk_live_abc123def456",
"cronSchedule": "0 */6 * * *"
}
Generated YAML Schema with Patterns
type: object
properties:
serviceName:
type: string
pattern: "^[a-z][a-z0-9-]*[a-z0-9]$"
description: Lowercase alphanumeric with hyphens
version:
type: string
pattern: "^\\d+\\.\\d+\\.\\d+$"
description: Semantic version (major.minor.patch)
endpoint:
type: string
format: uri
pattern: "^https://"
description: Must be an HTTPS URL
apiKey:
type: string
pattern: "^sk_(live|test)_[a-zA-Z0-9]+$"
description: API key with environment prefix
cronSchedule:
type: string
pattern: "^[\\d*,/-]+ [\\d*,/-]+ [\\d*,/-]+ [\\d*,/-]+ [\\d*,/-]+$"
description: Standard 5-field cron expression
Common Patterns Reference
| Use case | Pattern |
|---|---|
| Semver | ^\\d+\\.\\d+\\.\\d+$ |
| Slug / kebab-case | ^[a-z0-9]+(-[a-z0-9]+)*$ |
| UUID v4 | ^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ |
| IPv4 address | ^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$ |
| ISO date | ^\\d{4}-\\d{2}-\\d{2}$ |
| Docker image tag | ^[a-z0-9][a-z0-9._/-]*:[a-zA-Z0-9._-]+$ |
patternProperties for Dynamic Keys
When JSON has dynamic keys that follow a naming pattern:
{
"env_DATABASE_URL": "postgres://...",
"env_API_KEY": "sk_live_...",
"env_LOG_LEVEL": "info"
}
type: object
patternProperties:
"^env_[A-Z_]+$":
type: string
additionalProperties: false
This validates that all keys matching the pattern have string values, and no other keys are allowed.
Combining Pattern with Format
endpoint:
type: string
format: uri
pattern: "^https://"
format: uri validates general URI syntax while pattern adds the HTTPS requirement. The two constraints are applied independently (both must pass).
Pattern Limitations
- YAML Schema uses ECMA-262 regex (JavaScript-compatible)
- No lookbehind support in older validators
- Patterns are unanchored by default -- use
^and$for full-string matching - Complex patterns reduce schema readability -- consider
enumfor small value sets
Use Case
Infrastructure-as-code tools like Terraform, Pulumi, and CloudFormation accept configuration values that must follow specific formats -- Docker image tags, AWS ARNs, Kubernetes labels. YAML schema patterns catch format violations during local validation, before the slow and expensive cloud deployment cycle.
Try It — JSON to YAML Schema
Related Topics
YAML Schema String Type Constraints from JSON
Type Mapping
Define Enum Constraints in YAML Schema
Validation Rules
Generate YAML Schema for OpenAPI Specification
Advanced Patterns
Generate YAML Schema for Kubernetes Configuration
Real-World Configs
YAML Schema for CI/CD Pipeline Configuration
Advanced Patterns