Types

Declaring a type#

Declare a type as Name { fields }. Fields are separated by commas; the last comma is optional.

comat
// Built-in scalar types (custom types wrapping strings etc.; how each language generates them is decided in SPEC.md)
//   string, bool, int
//   email, phone
//   uuid   : UUID (any version)
//   uuidv4 : UUID v4
//   uuidv7 : UUID v7 (time-ordered)
//   date   : date (YYYY-MM-DD)
UserData {
    name: string,
    email: email,
    phone: phone,
    uid: uuidv4,
}

A field with ? is optional (the key may be missing). T | null allows null (the key is required). They mean different things.

comat
User {
    name: string,
    nickname?: string,           // the key may be omitted
    deletedAt: datetime | null,  // the key is required, and null is allowed
    tags: string[],              // an array
}

Built-in types#

TypeWire formatNotes
string / bool / intJSON string / boolean / number
emailstringThe email address format is validated
phonestringE.164 (+819012345678)
uuidstringAny version
uuidv4 / uuidv7stringThe version is validated
dateYYYY-MM-DD
datetimeRFC 3339 (with time zone)
timeHH:MM:SS
int64stringJavaScript loses precision above 2^53
decimalstringFor money and the like. Avoids rounding errors
urlstring
bytesbase64 string
jsonany JSON

Mapping per language#

TypeTypeScriptGoPython
string bool intstring boolean numberstring bool intstr bool int
email phone urlEmail Phone URLcomatype.Email Phone Urlstr with validation
uuid uuidv4 uuidv7Uuid Uuidv4 Uuidv7 (aliases of string)comatype.Uuid Uuidv4 Uuidv7 (types wrapping string)uuid.UUID
dateDateStr (alias)comatype.Datedatetime.date
datetimeDateTimeStr (alias)time.Timedatetime (with time zone)
timeTimeStr (alias)comatype.Timedatetime.time
int64bigintcomatype.Int64int
decimalDecimal (alias of string)comatype.Decimaldecimal.Decimal
bytesUint8Array[]bytebytes
jsonunknownjson.RawMessageAny

They are aliases in TypeScript

uuid and the like are aliases of string in TypeScript (no brand is attached). Being able to pass a plain string as { id } was preferred for ease of use. The runtime validates the format at run time.

enum and literal unions#

comat
enum Role {
    admin,
    member,
    guest,
}

Profile {
    role: Role,
    status: "active" | "banned",   // a literal union written in place
}

Constraints#

NotationMeaning
string(1..50)Number of characters
int(0..150)Range of values (one side can be omitted: int(0..) int(..150))
string(pattern: "^[0-9]{3}-[0-9]{4}$")Regular expression

Constraints are used to validate before sending and after receiving. The type in the language does not change (string(1..50) is still string).

comat
// A showcase of the type syntax (not used by any endpoint; it shows the list of types and the constraint syntax)

// Named enum (string values)
enum Role {
    admin,
    member,
    guest,
}

Profile {
    // Constraints: string(min..max), int(min..max). Either side can be omitted
    nickname: string(1..50),
    age?: int(0..150),
    // Regular expression
    zip: string(pattern: "^[0-9]{3}-[0-9]{4}$"),

    role: Role,
    // Inline literal union
    status: "active" | "banned",

    birthday?: date,
    createdAt: datetime,
    openAt: time,

    // 64-bit integers and decimals are sent as strings on the wire
    followers: int64,
    balance: decimal,

    website?: url,
    avatar?: bytes,
    // Arbitrary JSON
    extra?: json,

    // Arrays and null
    tags: string[],
    deletedAt: datetime | null,
}

Building objects#

...Type expands fields, and empty removes a field (what comes later wins).

comat
body {
    ...UserData,    // name, email, phone and uid come in
    uid: empty,     // remove uid
    password: string,
}

Resolving types#

Only names that you import, or types declared in the same file, can be used.

comat
import UserData from ../types/UserData.comat

The path is relative to the file that contains it. Put files that only declare types outside comatDir (in types/ and the like).

Notes

  • Keep type names unique within the project (TypeScript and Python gather them into one file).
  • A cyclic ...Type (A expands B and B expands A) is an error.
  • Go supports T | null and unions of only string literals. Other unions (such as string | int) are an error at generation time.