This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@busyxiang/utilities-js': patch | ||
--- | ||
|
||
Add validation for Json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { isValidJson } from './json'; | ||
|
||
describe('isValidJson', () => { | ||
test('it should return true if JSON is valid', () => { | ||
const isValid = isValidJson(JSON.stringify({ x: 1 })); | ||
|
||
expect(isValid).toBe(true); | ||
}); | ||
|
||
test('it should return false if JSON is not valid', () => { | ||
const isValid = isValidJson('invalid json'); | ||
|
||
expect(isValid).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const isValidJson = (str: string) => { | ||
try { | ||
JSON.parse(str); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; |