Effect.ts: Absence as First-Class

ValidationLesson 31 of 51

31. Schema Transformations

Transform absent data from one shape to another

Code Example
// Source shape (API response)
const ApiResponse = Schema.Struct({
  user_name: Schema.String,
  created_at: Schema.String  // ISO date string
});

// Target shape (domain model)
const User = Schema.Struct({
  name: Schema.String,
  createdAt: Schema.Date
});

// Transform: from one absence to another!
const UserFromApi = Schema.transform(
  ApiResponse,
  User,
  {
    decode: (api) => ({
      name: api.user_name,
      createdAt: new Date(api.created_at)
    }),
    encode: (user) => ({
      user_name: user.name,
      created_at: user.createdAt.toISOString()
    })
  }
);
Interactive Example
const output: string[] = [];
output.push('Schema transforms:');
output.push('');
output.push('API: { user_name, created_at }');
output.push('     ↓ transform');
output.push('Domain: { name, createdAt }');
output.push('');
output.push('• Bidirectional (encode/decode)');
output.push('• Type-safe transformations');
output.push('• Validated at boundaries');
output.push('');
output.push(' Safe data transformation!');
return output.join('
');
Explanation

Schema transforms: converting between different absences.

Traditional approach: manual transformation, hope it works. Schema types absence transformation:

  • API data is absent (needs fetching & validation)
  • Domain model is absent (needs transformation)
  • No assumptions about data shape
  • Describe: "HOW to transform one absent shape into another"
  • Bidirectional: encode back to original absence

Absence-first transformation: data doesn't exist in your shape until validated!


Part 31 of 51 in the Effect.ts Absence Modeling series