liveBook · Manning

Custom data type

As explained in API Design Patterns

A custom data type is a structured type with a predefined set of keys and specified types for their corresponding values.

A custom data type groups related fields under a predefined schema: each key is known, and each value has a specified type. It reorganizes fields without making their structure arbitrary. Use it when several resource fields share a meaningful theme; use a dynamic map when the data has no expected structure. section 5.7

Grouping chat-room security settings

Instead of placing password, requirePassword, allowAnonymousUsers, and allowChatBots directly on ChatRoom, define a securityConfig field of type SecurityConfig. This makes the resource easier to organize while preserving a fixed schema. section 5.7

Apply these design rules:

  • Group fields only when they share a common theme and belong naturally inside the resource. section 5.7
  • Prefer a singleton sub-resource when grouped fields are fundamentally distinct and meaningful outside the parent resource. section 5.7
  • Keep arbitrary, dynamic data in a map, and bound map keys and values when maps are used. section 5.7 section 5.7.1
Listing 5.8 Storing data directly on a resource or in a separate structure
interface ChatRoomFlat {
  id: string;
  name: string;
  password: string;                 #1
  requirePassword: boolean;         #1
  allowAnonymousUsers: boolean;     #1    
  allowChatBots: boolean;           #1
}
 
interface ChatRoomReGrouped {
  id: string;
  name: string;
  securityConfig: SecurityConfig;   #2
}
 
interface SecurityConfig {          #2
  password: string;
  requirePassword: boolean;
  allowAnonymousUsers: boolean;
  allowChatBots: boolean;
}

Compare flat and regrouped ChatRoom schemas. section 5.7

How API Design Patterns covers it

How deep it goes
Defines schemas, contrasts maps, and identifies grouping boundaries, but does not detail implementation beyond interface examples.
Examples
Uses ChatRoom securityConfig and contrasts it with grocery ingredientAmounts stored as a dynamic map.
What you can do after
Evaluate whether related fields need a predefined schema, a singleton sub-resource, or an unstructured map.
How it connects
Connects custom data types with resources, interfaces, maps, lists, and singleton sub-resources.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.