Skip to content
/ ts-zod Public

Typescript node demo with Zod type validation

Notifications You must be signed in to change notification settings

jrollin/ts-zod

Repository files navigation

Test

Zod logo

Typescript + Zod schema validation

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

API Nobel Prize

URL : http://api.nobelprize.org/v1/prize.json

This API can be tricky to validate

Expected Response schema

{
  "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"
        }
      ]
    }
  ]
}

Special Response

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.\""
}

About

Typescript node demo with Zod type validation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages