Skip to main content
Payload schemas define the expected structure and data types for MQTT messages. The gateway validates every incoming message against its flow’s schema before processing, ensuring data integrity.

Schema format

Schemas are stored as JSON in the payload_schema column of the flows table. They define expected attributes and their types:

Supported formats

The gateway supports two schema formats: Simple format (recommended):
JSON Schema format:
Both formats are equivalent. The validation logic extracts types from either format:

Supported types

The gateway validates against these Python types:
The number and float types accept both integers and floats. Use int or integer if you need to enforce whole numbers only.

Validation process

When a message arrives, the gateway validates it before processing:

Validation rules

  1. All schema attributes are required: Every attribute defined in the schema must be present in the payload
  2. Type checking is strict: Values must match the expected Python type exactly
  3. Extra attributes are allowed: Payloads can contain additional attributes not in the schema
  4. Unknown types are ignored: If a schema type isn’t in _TYPE_MAP, validation for that attribute is skipped
If validation fails, the message is rejected and an error is logged. The flow’s last_msg_id is not incremented.

Validation examples

Valid payload

Schema:
Payload:
Result: ✓ Valid

Valid with extra attributes

Payload:
Result: ✓ Valid (extra attributes are allowed)

Missing required attribute

Payload:
Result: ✗ Error: Missing attribute 'humidity' in payload

Type mismatch

Payload:
Result: ✗ Error: Invalid type for 'temperature'. Expected 'float', got 'str'

Complex types

Schemas can define object and array types:
Valid payload:
The gateway validates that objects are dictionaries and arrays are lists, but does not validate their internal structure. Nested validation is not supported.

Integration with message processing

Validation happens inside the database transaction, before any action is executed:
If validation fails, the transaction is rolled back and no changes are made to the database.