YAML Schema String Type Constraints from JSON

Learn how JSON string values map to YAML Schema string type definitions. Covers minLength, maxLength, pattern regex, format keywords, enum restrictions, and content encoding.

Type Mapping

Detailed Explanation

String Type Mapping in YAML Schema

JSON string values map to type: string in YAML Schema. Beyond basic type assertion, YAML Schema offers rich constraints that let you validate string content precisely.

Basic String Property

{
  "name": "my-service",
  "email": "admin@example.com",
  "version": "2.1.0"
}
type: object
properties:
  name:
    type: string
  email:
    type: string
    format: email
  version:
    type: string
    pattern: "^\\d+\\.\\d+\\.\\d+$"

String Constraints

Constraint Purpose Example
minLength Minimum character count minLength: 1 (non-empty)
maxLength Maximum character count maxLength: 255
pattern Regex the value must match pattern: "^[a-z0-9-]+$"
format Semantic format hint format: email
enum Fixed set of allowed values enum: ["info", "warn", "error"]
default Default value when absent default: "info"

Format Keywords

The format keyword provides semantic validation beyond regex:

properties:
  email:
    type: string
    format: email
  homepage:
    type: string
    format: uri
  createdAt:
    type: string
    format: date-time
  ip:
    type: string
    format: ipv4

Common formats: date-time, date, time, email, uri, hostname, ipv4, ipv6, uuid.

Pattern Validation

Use pattern for custom validation rules:

properties:
  slug:
    type: string
    pattern: "^[a-z0-9]+(-[a-z0-9]+)*$"
    description: URL-safe slug (lowercase, hyphens only)
  semver:
    type: string
    pattern: "^\\d+\\.\\d+\\.\\d+(-[a-zA-Z0-9.]+)?$"
    description: Semantic version string

Content Encoding

For binary data stored as strings:

certificate:
  type: string
  contentEncoding: base64
  contentMediaType: application/x-pem-file

Smart Inference

An intelligent converter detects common string patterns and automatically applies appropriate constraints -- email addresses get format: email, ISO dates get format: date-time, and URL strings get format: uri.

Use Case

API request validation often requires precise string constraints -- email fields must be valid emails, slugs must match URL-safe patterns, and version strings must follow semver. Generating a YAML schema from sample JSON with string constraints ensures that invalid data is rejected at the configuration layer before it reaches application code.

Try It — JSON to YAML Schema

Open full tool