THE DEV BENCH
✔️ JSON Schemahard

8. Match a string pattern (regex)

Write a schema for an object with a required "sku" string that must match the pattern: lowercase letters, a hyphen, then digits — e.g. "web-01". Reject "Web01" and "web".

How to approach this

The "pattern" keyword constrains a string to a regular expression — how you validate a SKU, a resource name, or a semver. Use "type": "string" plus "pattern": "^[a-z]+-[0-9]+$". (Note: in JSON, every backslash in a regex must be doubled — "\\d" not "\d". This pattern needs none.) The anchors ^ and $ force the WHOLE string to match.

schema.json