"Objects validation and comparison using runtime types (io-ts)", Oleksandr Suhak
fwdays
108 views
29 slides
May 27, 2024
Slide 1 of 29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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...
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 implement.
Size: 5.24 MB
Language: en
Added: May 27, 2024
Slides: 29 pages
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
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