Module 6 β Schema Types, Enums & Complex Models
π― Module Objective
By the end of this module, you will:
- Master primitive schema types
- Use complex schemas (Enum, Object, Array, Map)
- Design structured properties
- Build strongly-validated models
- Model real enterprise-grade systems
- Avoid weak data modeling practices
This module transforms your DTDL from basic to professional.
1οΈβ£ What Is a Schema?
In DTDL, a schema defines the type of data.
Example:
{
"@type": "Property",
"name": "temperature",
"schema": "double"
}
Here:
double = schema
Schemas define:
- Data type
- Validation
- Structure
Think of schema as:
Data contract.
2οΈβ£ Primitive Schema Types
These are the simplest types.
Common primitives:
- boolean
- integer
- double
- string
- date
- dateTime
- time
- duration
Example:
{
"@type": "Property",
"name": "isOperational",
"schema": "boolean"
}
Use primitive types when:
- Data is simple
- No structure required
3οΈβ£ Enum β Controlled Value Sets
Enum restricts values to a predefined list.
Example:
{
"@type": "Property",
"name": "operationalStatus",
"schema": {
"@type": "Enum",
"valueSchema": "string",
"enumValues": [
{ "name": "Running", "enumValue": "Running" },
{ "name": "Stopped", "enumValue": "Stopped" },
{ "name": "Maintenance", "enumValue": "Maintenance" }
]
}
}
Now only those values are valid.
Why this is powerful:
- Prevents invalid data
- Standardizes state
- Improves query reliability
Always use Enum when values are predictable.
4οΈβ£ Object β Structured Data
Objects allow grouping multiple related fields.
Example:
{
"@type": "Property",
"name": "location",
"schema": {
"@type": "Object",
"fields": [
{
"name": "latitude",
"schema": "double"
},
{
"name": "longitude",
"schema": "double"
}
]
}
}
Now location is structured.
Instead of two separate properties:
- latitude
- longitude
You have a single structured object.
Use Object when:
- Data belongs together logically
- You want grouping
- It represents a concept
5οΈβ£ Array β Multiple Values
Array allows storing multiple values.
Example:
{
"@type": "Property",
"name": "supportedProtocols",
"schema": {
"@type": "Array",
"elementSchema": "string"
}
}
Now the twin can store:
[βMQTTβ, βHTTPβ, βAMQPβ]
Use Array when:
- Multiple values of same type
- Dynamic lists
- Tags
- Capabilities
6οΈβ£ Map β Key-Value Structure
Map stores dynamic key-value pairs.
Example:
{
"@type": "Property",
"name": "configuration",
"schema": {
"@type": "Map",
"mapKey": {
"name": "configName",
"schema": "string"
},
"mapValue": {
"name": "configValue",
"schema": "string"
}
}
}
This allows flexible configuration storage.
Use Map when:
- Keys are dynamic
- Structure is not fixed
- You need flexibility
7οΈβ£ Complex Example β Smart Machine Model
Here is a production-style model combining everything:
{
"@id": "dtmi:com:smartfactory:Machine;1",
"@type": "Interface",
"@context": "dtmi:dtdl:context;2",
"displayName": "Machine",
"contents": [
{
"@type": "Property",
"name": "operationalStatus",
"schema": {
"@type": "Enum",
"valueSchema": "string",
"enumValues": [
{ "name": "Running", "enumValue": "Running" },
{ "name": "Stopped", "enumValue": "Stopped" },
{ "name": "Maintenance", "enumValue": "Maintenance" }
]
}
},
{
"@type": "Property",
"name": "location",
"schema": {
"@type": "Object",
"fields": [
{ "name": "x", "schema": "double" },
{ "name": "y", "schema": "double" }
]
}
},
{
"@type": "Property",
"name": "supportedModes",
"schema": {
"@type": "Array",
"elementSchema": "string"
}
}
]
}
This is real-world ready modeling.
8οΈβ£ When to Use What?
Use:
Primitive β Simple values
Enum β Controlled states
Object β Grouped structured data
Array β Lists
Map β Flexible dynamic key-value
Design rule:
Use the simplest schema that solves the problem.
9οΈβ£ Over-Engineering Warning
Avoid:
β Using Object when primitive is enough
β Using Map for everything
β Making deeply nested structures
β Creating overly complex schemas
Remember:
Digital Twin modeling is about clarity.
Not showing complexity.
π Schema Validation Thinking
Ask:
- Does this value have limited states? β Enum
- Does this represent a structured concept? β Object
- Is it a list? β Array
- Is it dynamic key-value? β Map
Model intentionally.
1οΈβ£1οΈβ£ Enterprise Modeling Example β Energy System
PowerUnit:
- status β Enum
- load β double
- voltage β double
- connectedDevices β Array
- configuration β Map
This creates:
Strong validation
Clear semantics
Query-friendly structure
π§ Key Takeaways
- Schema defines data contract
- Enum improves reliability
- Object groups related data
- Array supports lists
- Map allows flexibility
- Use structure intentionally
Now your DTDL is expressive and controlled.
π§ͺ Practice Exercise
Model a Smart Elevator.
Think:
- currentFloor β integer
- direction β Enum (Up, Down, Idle)
- capacity β integer
- passengers β Array?
- lastMaintenance β dateTime
Design the correct schema types.
That is professional modeling thinking.
π Whatβs Next?
In Module 7 we build:
A complete Real-World Smart Factory Case Study
You will:
- Design full model hierarchy
- Apply inheritance
- Use components
- Define relationships
- Combine schema mastery
This is where everything comes together.
You are now modeling like an enterprise architect.