4 views
Нужно ли валидировать JSON-данные, полученные от API, и что ещё нужно учесть при вычислении процента на клиенте?
Asked at
AI answer
Yes. JSON is only a data-transfer format: after parsing, validate the data at runtime against a schema or manually, since TypeScript types do not validate an API response by themselves. Check that fields are present, verify types and allowed ranges, and handle null and malformed responses.
When calculating a percentage, make sure the values are numeric and within the expected range. Handle a zero denominator, avoid accidental integer division, and decide where to round the result. For display, use localized formatting, such as Intl.NumberFormat; do not round intermediate calculations unnecessarily. Do not use client-side validation as a substitute for server-side validation.
Also asked at ООО Цитадель
- Tell me about closures in JavaScript. How often do you have to use them in practice?
- Have you encountered cross-browser issues and how did you solve them?
- Have you worked with architectural approaches like FSD (Feature-Sliced Design)?
- If you realize that you will not have time to complete the task within the planned deadline, should you report it, to whom, and at what stage?
- What design system or UI-kit did you use: a ready-made set of components or your own components?
Similar questions
- In what formats can data be transferred between the frontend and backend?
- How can you protect against cross-site scripting (XSS) on the frontend?
- What data formats can be sent to the backend via an API?
- How would you handle an error if a request to the server fails?
- What is the result of adding 0.2 and 0.3 in JavaScript, and why?