Skip to content

Commit edfc117

Browse files
authored
Merge pull request #984 from david-roper/enum-best-practices
2 parents fd99aea + c5b9774 commit edfc117

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

docs/en/5-reference/5.0-form-instrument.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,22 @@ Always consider using \`group\`ing to structure sections
5555
- Make sure your rules are correct given the paper form
5656
- Please familiarize yourself with the possible validation rules here
5757
- Be careful to write the most restrictive validation schema possible
58-
- Example: likert-scale data should be .int()
58+
- Example: likert-scale data should be `.int()`
59+
- Example: A select from multiple string options question should be `.enum(['option1','option2', ...])` instead of `.string()`
5960
- Remember the API data-entry is independent of the form, so the validation schema must properly restrict API entry as well
6061
5. Please make sure to format your code whenever possible, as this can prevent annoying issues with missing brackets. In the playground, you can do this with (Alt + F)
6162
6. If you find yourself repeating code frequently, consider extracting the logic into shared variables. The guiding principle of this should be to maximize readability.
62-
- For example, if your data schema is z.number().int().min(0).max(7) for every field, consider moving it into a variable, as this can reduce the cognitive load of programmers reading your form
63+
- For example, if your data schema is `z.number().int().min(0).max(7)` for every field, consider moving it into a variable, as this can reduce the cognitive load of programmers reading your form
6364
- On the other hand, if this is the schema for only two or three variables, creating a variable would probably increase cognitive load, rather than reducing it.
6465
- The same applies for computed measures (i.e., define a function at the top of your file instead of copy-pasting it to every field).
6566
7. Review common styling conventions
6667
- https://google.github.io/styleguide/jsguide.html
6768
8. Use [the playground](https://playground.opendatacapture.org/) to create instruments
6869
- It is updated regularly to use the latest runtime
6970
- It is setup with formatting, linting, and type checking by default
71+
9. Proper use of Zod's enum type
72+
- It is best to use Zod's enum type when the answer to a form question is a set of constant values.
73+
- Example: A question which asks the user to choose from a selection of options should use `z.enum([option1, option2, ...])` within its zod validation schema.
7074

7175
### Example
7276

0 commit comments

Comments
 (0)