From dab7c950c813ffa62cb967a7ea56f154dcd1a96b Mon Sep 17 00:00:00 2001 From: EledenGreen <114846457+EledenGreen@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:44:33 +0530 Subject: [PATCH] update Ex: 9.27 instruction to use Zod library --- src/content/9/en/part9e.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/content/9/en/part9e.md b/src/content/9/en/part9e.md index 20da70adb99..1de8376a087 100644 --- a/src/content/9/en/part9e.md +++ b/src/content/9/en/part9e.md @@ -402,17 +402,18 @@ Remember that we have different kinds of entries in our app, so our backend shou In this exercise, you quite likely need to remember [this trick](/en/part9/grande_finale_patientor#omit-with-unions). -You may assume that the diagnostic codes are sent in the correct form and use eg. the following kind of parser to extract those from the request body: +You may define a Zod [array](https://zod.dev/?id=arrays) schema for the diagnostic codes and use it appropriately in other Zod [objects](https://zod.dev/?id=objects) as follows: ```js -const parseDiagnosisCodes = (object: unknown): Array => { - if (!object || typeof object !== 'object' || !('diagnosisCodes' in object)) { - // we will just trust the data to be in correct form - return [] as Array; - } - - return object.diagnosisCodes as Array; -}; +const DiagnosisCodesSchema = z + .array(z.string()) + .optional() + .transform((codes) => (codes ? codes : [])); + +const BaseHealthEntrySchema = z.object({ + ... + diagnosisCodes: DiagnosisCodesSchema, +}) ``` #### 9.28: Patientor, step 8