Skip to main content

JSON Schema Validation Patterns

JSON Schema patterns for bundle params. For $md.immutable, $md.enum, and the other Massdriver keywords, see Massdriver Annotations.

Human-readable Enum Labels​

Use oneOf with const and title to show a label in the dropdown and save a different value:

yaml
properties:
color:
title: Favorite Color
type: string
oneOf:
- title: Red
const: "#ff0000"
- title: Green
const: "#00ff00"
- title: Blue
const: "#0000ff"

Immutable Fields​

$md.immutable makes a field read-only once the instance is provisioned. Use it for fields whose change would recreate the cloud resource:

yaml
properties:
cidr:
title: CIDR Range
$md.immutable: true
type: string

It works on top-level properties and on properties nested in objects. See Massdriver Annotations.

Range Exclusion​

Exclude values from a range with not:

yaml
properties:
range:
title: Zero or between 10 and 1000
type: integer
minimum: 0
maximum: 1000
not:
minimum: 1
maximum: 9

Conditional Fields​

Conditionally Requiring Fields​

dependencies with a list requires fields only when another field is set:

yaml
properties:
billing:
title: Billing
type: object
properties:
name:
type: string
credit_card:
type: number
address:
type: string
required:
- name
dependencies:
credit_card:
- address

Conditionally Displaying Fields​

dependencies with a schema adds fields only when another field is set:

yaml
properties:
billing:
title: Billing
type: object
properties:
name:
type: string
credit_card:
type: number
required:
- name
dependencies:
credit_card:
properties:
address:
type: string
required:
- address

Boolean-based Conditionals​

Use oneOf under dependencies to show fields when a boolean is on:

yaml
stream:
type: object
title: "DynamoDB Streams"
description: "Enable DynamoDB stream emission"
dependencies:
enabled:
oneOf:
- properties:
enabled:
const: true
view_type:
type: string
default: NEW_IMAGE
enum:
- KEYS_ONLY
- NEW_IMAGE
- OLD_IMAGE
- NEW_AND_OLD_IMAGES
required:
- view_type
- properties:
enabled:
const: false
properties:
enabled:
type: boolean
default: false
title: "Enabled?"

Enum-based Conditionals​

Show different fields for each enum value:

yaml
capacity:
type: object
title: "Capacity"
dependencies:
billing_mode:
oneOf:
- properties:
billing_mode:
const: PROVISIONED
read_capacity:
type: integer
title: "Read Capacity"
minimum: 1
maximum: 3000
write_capacity:
type: integer
title: "Write Capacity"
minimum: 1
maximum: 1000
required:
- read_capacity
- write_capacity
- properties:
billing_mode:
const: PAY_PER_REQUEST

For other conditionals, see JSON Schema if-then-else.

Property Reuse with YAML Anchors​

YAML anchors (&) and aliases (*) reuse schema fragments.

Defining Anchors​

yaml
ValidIndexDataTypes: &valid_index_data_types
oneOf:
- title: String
const: "S"
- title: Number
const: "N"
- title: Binary
const: "B"

PartitionKeyProperties: &partition_key_properties
partition_key:
type: string
title: "Partition Key"
$md.immutable: true
partition_key_type:
type: string
title: "Partition Key Type"
$md.immutable: true
<<: *valid_index_data_types

SortKeyProperties: &sort_key_properties
sort_key:
type: string
title: "Sort Key"
$md.immutable: true
sort_key_type:
type: string
title: "Sort Key Type"
$md.immutable: true
<<: *valid_index_data_types

Using References​

The YAML merge key << copies an anchor's keys into the current map:

yaml
primary_index:
type: object
title: "Primary Index"
dependencies:
type:
oneOf:
- properties:
type:
const: "compound"
<<: [*partition_key_properties, *sort_key_properties]
required:
- partition_key
- partition_key_type
- sort_key
- sort_key_type
- properties:
type:
const: "simple"
<<: *partition_key_properties
required:
- partition_key
- partition_key_type
properties:
type:
type: string
title: "Type"
default: simple
$md.immutable: true
enum:
- simple
- compound

Sets (User-defined Keys)​

Use propertyNames with additionalProperties for a map whose keys the user chooses, such as when the IaC needs a unique identifier for each item:

yaml
type: object
propertyNames:
pattern: "^[a-z0-9]+$"
additionalProperties:
type: object
properties:
model:
type: string
make:
type: string
year:
type: string
required:
- model
- make
- year

The form asks for a unique key for each item.

Custom Validation Messages​

message replaces the error text for a keyword:

yaml
read_capacity:
type: integer
title: "Read Capacity"
minimum: 1
maximum: 3000
message:
minimum: "must be larger than 1."
maximum: "must be less than 3000."

It is most useful for regex patterns:

yaml
grn:
type: string
title: "GCP Resource Name (GRN)"
description: "Resource identifier provided by GCP"
examples:
- "projects/my-project/global/networks/my-global-network"
- "projects/my-project/regions/us-west2/subnetworks/my-subnetwork"
pattern: "^projects\\/[a-z0-9-]+\\/[a-z]+\\/[a-z0-9-]+(?:\\/[a-zA-Z0-9-_.]+){0,6}$"
message:
pattern: "The provided GRN does not follow the expected pattern. See the examples for valid GRNs."

See Also​