"Objects validation and comparison using runtime types (io-ts)", Oleksandr Suhak

fwdays 108 views 29 slides May 27, 2024
Slide 1
Slide 1 of 29
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29

About This Presentation

A common task in modern JS is parsing, validating and then comparing JSON objects. In this talk I will quickly go through most common ways to parse/validate and compare objects we use today and then focus more on how runtime types (based on io-ts) can help make such tasks easier and quicker to imple...


Slide Content

Знайомство
Олександр Сугак
> 15 років в IT
Тім(тех)лід, архітектор, senior software engineer
ютуб: www.youtube.com/@AleksandrSugak
інтеракивні курси: codereel.io
twitter.com/alsugak

Background
Used many static languages to build systems in various domains
C#, F#, Java, Scala, TypeScript
Always the same challenge: parse, validate, transform, compare
raw data

Types vs Otside World
My App
(My Types)
“The Outside World”

JSON
Server Client
Types vs Otside World

Form data
User App
Types vs Otside World

Types vs Otside World: concrete examples
Grammarly: back-end server sending text suggestions
ECommerce: 3rd party service sending product updates via
queue

Product data
3rd Party Service App
Parsing, Validation and Comparison

“External Schema”
JSON Schema, Swagger, GraphQL etc.
TS: Code (types) generation using 3rd party libs
Information duplication: schema + TS types
Merge conflicts in generated code
Hard to refactor code after schema changes
“closed” code generation logic
as any as GeneratedType

Runtime types
Defined inside the app code as values/functions
Available at runtime (not erased by TS compiler)
Seen it before:

IO-TS
Runtime types encoding from Giulio Canti (author of fp-ts)
TS native, static types inferred from runtime types
Heavy use of fp-ts abstractions but can be used on its own
Runtime types encoding, compositions, decoding and more
github.com/gcanti/io-ts

IO-TS: Primitives

IO-TS: Composition

IO-TS: Inferring Types

IO-TS: Decoding

IO-TS: much more
Decoding/Encoding/Equality and more.
Advanced compositions (e.g. recursive types)
Data transformations (.refine, .parse)
Branded types (E.g. PositiveInteger, ValidEmail, etc.)
Custom error reporters
Custom types
Custom interpreters (!)

IO-TS: Custom types

IO-TS: Custom types

IO-TS: Custom types

IO-TS: Schema and Schemable

IO-TS: Schema and Schemable
Type
Schema
Decoding
Equality
check
your
intepreter

IO-TS: Schema and Schemable
Type Schema
fast-check
arbitraries

Fast-check
https://fast-check.dev/

IO-TS: Schema and Schemable

IO-TS: Schema and Schemable

Extended example:
github.com/AlexSugak/runtime_types/blob/main/src/changed_products.ts

IO-TS: Comparing with Zod
io-ts and fp-ts can be hard for beginners
Zod is more developer-friendly
both are good but to io-ts to me feels more thought-through
custom interpreters > 3rd party libraries
https://zod.dev/

Some conclusions
Runtime types offer IO type-safety without info duplication
IO-TS is a good library offerring sound foundation for your app IO
logic (good library = powerful abstraction and its algebra)
IO-TS can be a good way to start “doing FP” in your project

“Unit tests for beginners” course
Leave your email to get notification when course is ready:
codereel.io/courses/unit-tests-intro