@@ -24,6 +24,8 @@ function App() {
24
24
const [ result , setResult ] = useState ( JSON . stringify ( [ ] , null , 4 ) ) ;
25
25
const [ resultType , setResultType ] = useState < 'value' | 'path' > ( 'value' ) ;
26
26
const [ query , setQuery ] = useState ( '$.phoneNumbers[:1].type' ) ;
27
+ const [ isQueryValid , setQueryValid ] = useState ( true ) ;
28
+ const [ queryParseError , setQueryParseError ] = useState ( '' ) ;
27
29
28
30
const queryInput = useRef < HTMLInputElement > ( null ) ;
29
31
@@ -38,23 +40,34 @@ function App() {
38
40
39
41
function onChangeResultType ( event : any ) {
40
42
const type = event . target . checked ? 'path' : 'value' ;
41
- setResultType ( type )
43
+ setResultType ( type ) ;
42
44
}
43
45
44
46
function applyJsonPath ( jsonStr : string , jsonPath : string ) {
45
47
let json = '' ;
48
+ let result = '' ;
49
+
46
50
try {
47
51
json = JSON . parse ( jsonStr . replace ( / ( \r \n | \n | \r ) / gm, '' ) ) ;
48
52
} catch ( error ) {
49
53
setResult ( 'JSON Parse Error' ) ;
50
54
return ;
51
55
}
52
56
53
- const result = JSONPath ( {
54
- json,
55
- path : jsonPath ,
56
- resultType : resultType ,
57
- } ) ;
57
+ try {
58
+ result = JSONPath ( {
59
+ json,
60
+ path : jsonPath ,
61
+ resultType : resultType ,
62
+ } ) ;
63
+ setQueryValid ( true ) ;
64
+ setQueryParseError ( '' ) ;
65
+ } catch ( error ) {
66
+ setQueryValid ( false ) ;
67
+ if ( error instanceof Error ) {
68
+ setQueryParseError ( error . message ) ;
69
+ }
70
+ }
58
71
59
72
if ( 0 < result . length ) {
60
73
setResult ( JSON . stringify ( result , undefined , 2 ) ) ;
@@ -77,7 +90,7 @@ function App() {
77
90
< input
78
91
id = "jsonpath-query"
79
92
type = "text"
80
- className = "form-control"
93
+ className = { "form-control " + ( isQueryValid ? "" : "is-invalid" ) }
81
94
placeholder = "Put JSONPath syntax"
82
95
value = { query }
83
96
onInput = { onInputQuery }
@@ -86,6 +99,9 @@ function App() {
86
99
< label htmlFor = "jsonpath-query" >
87
100
JSONPath
88
101
</ label >
102
+ < div className = "invalid-feedback" >
103
+ { queryParseError }
104
+ </ div >
89
105
</ div >
90
106
91
107
< div className = "row py-2" >
0 commit comments