Skip to content

Defining Types: Structs and Enums

While MON is flexible, sometimes you need to enforce a specific structure for your data. The optional MON type system helps you do just that, preventing typos and ensuring data integrity and schema validation. Unlike JSON, which relies on external JSON Schema definitions, or TOML with its more limited type capabilities, MON provides built-in mechanisms for defining robust configuration schemas. The two main ways to define types are Enums and Structs.

An Enum is a type that can only be one of several predefined values. It’s perfect for things like status, categories, or modes.

Syntax:

{
// Defines a type named `Status` that can only be one of these three values.
Status: #enum {
Active,
Inactive,
Pending,
},
}

To use an enum value, you reference it with a dollar sign $ followed by the type name and the variant name.

Example Usage:

{
Status: #enum { Active, Inactive, Pending },
// Using the enum value
current_status: $Status.Active,
}

Structs (#struct): Defining an Object’s Schema

Section titled “Structs (#struct): Defining an Object’s Schema”

A Struct defines the “shape” of an object. It specifies what keys are allowed, what type their values should be, and can provide default values for optional keys.

Syntax:

{
// Defines a schema for a User object
User: #struct {
// `id` is a required field of type Number
id(Number),
// `username` is a required field of type String
username(String),
// `email` is optional and defaults to null if not provided
email(String) = null,
// `is_active` is optional and defaults to `true`
is_active(Boolean) = true,
},
}

Built-in Types: You can use String, Number, Boolean, Null, Array, Object, and Any (which allows any value).


Fantastic! You’ve defined your first custom types. But how do you actually use them to validate your data? That’s next!