feat(safeJSONParse): add safe JSON parsing utility#1515
feat(safeJSONParse): add safe JSON parsing utility#1515Yeom-JinHo wants to merge 4 commits intotoss:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Deployment failed with the following error: |
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a new safeJSONParse utility function that provides a non-throwing wrapper around JSON.parse, complementing the existing isJSON predicate by separating validation from transformation concerns.
Key changes:
- Added
safeJSONParse<T>(value: unknown): T | nullutility that returns parsed JSON ornullon failure - Implemented comprehensive runtime and type-level tests covering edge cases and type inference
- Documented the utility with examples in English, Korean, Japanese, and Chinese
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/util/safeJSONParse.ts |
Core implementation with comprehensive JSDoc documentation |
src/util/safeJSONParse.spec.ts |
Runtime tests for valid/invalid inputs and type-level tests using expectTypeOf |
src/util/index.ts |
Export statement for the new utility |
docs/reference/util/safeJSONParse.md |
English documentation with usage examples |
docs/ko/reference/util/safeJSONParse.md |
Korean translation of documentation |
docs/ja/reference/util/safeJSONParse.md |
Japanese translation of documentation |
docs/zh_hans/reference/util/safeJSONParse.md |
Simplified Chinese translation of documentation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1515 +/- ##
=======================================
Coverage 99.97% 99.97%
=======================================
Files 468 469 +1
Lines 4459 4465 +6
Branches 1313 1314 +1
=======================================
+ Hits 4458 4464 +6
Misses 1 1 🚀 New features to boost your workflow:
|
|
I’m not fully sure if this is the best name for this helper, so I’m open to renaming if you have a better suggestion 🙌🏽 |
Description
This PR introduces a new
safeJSONParseutility that provides a non-throwing wrapper aroundJSON.parse, complementing the existingisJSONpredicate.The goal is to keep validation and transformation clearly separated while giving consumers a type-safe way to parse JSON strings.
Changes
Added
safeJSONParse<T = unknown>(value: unknown): T | nullutilityvalueis a string containing valid JSONnullwhenvalueis not a string or when parsing failsJSON.parsesemantics (e.g."null"→null) while never throwingImplemented comprehensive runtime tests for
safeJSONParse"null"input is handled as a valid JSON string whose parsed value isnullAdded type-level tests using
expectTypeOfTis respected (T | null)nullchecksT = unknown) when no type argument is providedDocumented
safeJSONParsein a dedicated MDX pageUser)safeJSONParsewithisJSONfor clearer validation + parsing flowsMotivation
try { JSON.parse(...) } catch { ... }patterns across the codebaseisJSON(value)→ “Is this a valid JSON string?” (pure validation)safeJSONParse(value)→ “Safely parse this JSON string into a value” (transformation)