State Management · Senior · Question 60 of 75

Why validate state read from the URL (search params) with a schema?

const filters = FiltersSchema.parse(Object.fromEntries(searchParams));

Explanation

A schema (e.g. Zod) coerces strings to numbers, applies defaults and rejects garbage. Treat the URL like any external API.

Further reading

Problem with a question

State Management · Senior · question 60

Test yourself

Take the State Management · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz