Project to demonstrate Typescript Type validation at runtime thanks to Zod
Project used in my article about TS + Zod (:fr:)
Libraries used :
- Zod to ensure schema and type validation
- Vitest as test framework
- cross-fetch to provide
fetch
in browser and node context
URL : http://api.nobelprize.org/v1/prize.json
This API can be tricky to validate
{
"prizes": [
{
"year": "2022",
"category": "literature",
"laureates": [
{
"id": "1017",
"firstname": "Annie",
"surname": "Ernaux",
"motivation": "\"for the courage and clinical acuity with which she uncovers the roots, estrangements and collective restraints of personal memory\"",
"share": "1"
}
]
}
]
}
Sometimes, there are no laureate but a reason is provided in another key overallMotivation
:
{
"year": "1939",
"category": "peace",
"overallMotivation": "\"No Nobel Prize was awarded this year. The prize money was with 1/3 allocated to the Main Fund and with 2/3 to the Special Fund of this prize section.\""
}