Branded Types
Branded Types protocol schemas
Branded Types for ObjectStack Identifiers
Branded types provide compile-time safety by preventing accidental mixing
of different identifier kinds. For example, you cannot pass an ObjectName
where a FieldName is expected, even though both are strings at runtime.
@example
import \{ ObjectNameSchema, FieldNameSchema \} from '@objectstack/spec';
const objName = ObjectNameSchema.parse('project_task'); // ObjectName
const fieldName = FieldNameSchema.parse('task_name'); // FieldName
// TypeScript will catch this at compile time:
// const fn: FieldName = objName; // Error!Source: packages/spec/src/shared/branded-types.zod.ts
TypeScript Usage
import { AppName, FieldName, FlowName, ObjectName, RoleName, ViewName } from '@objectstack/spec/shared';
import type { AppName, FieldName, FlowName, ObjectName, RoleName, ViewName } from '@objectstack/spec/shared';
// Validate data
const result = AppName.parse(data);