Part 15 – Anti-Corruption Layer
This is a series of posts I’m writing about using types as another tool in software development, Continuous Delivery, & keeping LLM’s honest. They’re also a design & refactoring tool, a communication tool, and reduce how many tests you have to write.
Parts
- Part 1 – Branded Types
- Part 2 – Product Types
- Part 3 – Union & Discriminated Unions
- Part 4 – Non-Empty Collections
- Part 5 – Indexed Types
- Part 6 – unknown vs any
- Part 7 – Result
- Part 8 – Schema
- Part 9 – Total Function
- Part 10 – Errors as Values
- Part 11 – Property Tests
- Part 12 – Type Proofs
- Part 13 – Exhaustiveness Checking
- Part 14 – Parse, Don’t Validate
- Part 15 – Anti-Corruption Layer
- Part 16 – Opaque Types
- Part 17 – Maybe
- Part 18 – Smart Constructors
- Part 19 – Pipeline
- Part 20 – Railway Oriented Programming
- Part 21 – Typestate
- Part 22 – Capabilities
- Part 23 – Immutability
- Part 24 – Making Impossible States Impossible
- Part 25 – Type Driven Development: How to do it
- Part 26 – Final Thoughts


An Anti-Corruption Layer turns someone else’s model into your own. Where unknown, schemas, & Parse, Don’t Validate convert untrusted data into trusted types, the ACL is sometimes a different layer that converts “their types to _our_ types”. Sometimes you can do this using the same Zod schema, other times it’s truly an entirely different domain boundary, shielding a legacy system away.
For example “Python devs don’t talk like us”. What Python devs call a “Person”:
person_dict = {
'first_name': string,
'last_name': string,
'birthdate': '1980-02-06T12:44:53.063Z'
}Code language: JavaScript (javascript)
is NOT what we call a Person; firstName & lastName are camel cased, are 2 different Branded types, and birthday is a POSIX time with a Timezone vs. the whack ISO Date Strings.
person = {
first: FirstName,
last: LastName,
birthday: { time: Time.Posix, zone: Time.Zone }
}
OOP devs call that “converting Data Transfer Objects (DTO) to Value Objects (VO)”.
The Domain angle is “We talk differently”; a legacy API talks about Products, still uses a hard to work w/Transactions list you have to search, & AccountType is a string with already known rules.
legacy = {
// what accounts they have, usually just 1
products: Array<number>,
// a mix of credit and payments
transactions: Array<Transaction>,
// 'individual' or 'warehouse'
accountTypes: Array<string>
}Code language: JavaScript (javascript)
Our team instead talks about different Customers with always at least 1 Account, transctions are either Credits or Payments, and the AccountType string (a known Union/Enum list) that dictate what kind of Customer it is, Individual or Warehouse:
type IndividualCustomer = {
credits: Array<CreditTransaction>,
payments: Array<PaymentTransaction>,
firstAccount: Account,
otherAccounts: Array<Account>
}
type WarehouseCustomer = { ... }Code language: HTML, XML (xml)
Often an ACL can just be a Schema and map function:
neverThrowPromise()
.andThen(safeParseJSON)
.andThen(legacySchema)
.andThen(mapToCustomer)Code language: CSS (css)
Other times it’s larger abstractions in a module mapping back and forth from legacy to modern, not just data but also behavior.
customerToLegacy
|> legacyToJSON
|> legacySave
The key is, like how “Parse, Don’t Validate” ensures you don’t have their bad data model in your code causing Shotgun Parsing, you don’t want legacy concepts/words to “infect” your Domain, so you use an ACL at the boundary. Other times it’s just different systems use different words and concepts than your team does and it causes confusion & communication problems. It’s easier if your team uses the same language and words, and puts that into the code, then translate to/from others.
Leave a Reply