13
13
14
14
const $ = ( s ) => document . querySelector ( s ) ;
15
15
16
+ const jsonpathEl = $ ( '#jsonpath' ) ;
16
17
const updateResults = ( ) => {
17
18
const jsonSample = $ ( '#jsonSample' ) ;
18
19
const reportValidity = ( ) => {
19
20
// Doesn't work without a timeout
20
21
setTimeout ( ( ) => {
21
22
jsonSample . reportValidity ( ) ;
23
+ jsonpathEl . reportValidity ( ) ;
22
24
} ) ;
23
25
} ;
24
26
let json ;
27
+ jsonSample . setCustomValidity ( '' ) ;
28
+ jsonpathEl . setCustomValidity ( '' ) ;
29
+ reportValidity ( ) ;
25
30
try {
26
31
json = JSON . parse ( jsonSample . value ) ;
27
- jsonSample . setCustomValidity ( '' ) ;
28
- reportValidity ( ) ;
29
32
} catch ( err ) {
30
33
jsonSample . setCustomValidity ( 'Error parsing JSON: ' + err . toString ( ) ) ;
31
34
reportValidity ( ) ;
32
35
return ;
33
36
}
34
- const result = new JSONPath . JSONPath ( {
35
- path : $ ( '#jsonpath' ) . value ,
36
- json,
37
- eval : $ ( '#eval' ) . value === 'false' ? false : $ ( '#eval' ) . value ,
38
- ignoreEvalErrors : $ ( '#ignoreEvalErrors' ) . value === 'true'
39
- } ) ;
40
-
41
- $ ( '#results' ) . value = JSON . stringify ( result , null , 2 ) ;
37
+ try {
38
+ const result = new JSONPath . JSONPath ( {
39
+ path : jsonpathEl . value ,
40
+ json,
41
+ eval : $ ( '#eval' ) . value === 'false' ? false : $ ( '#eval' ) . value ,
42
+ ignoreEvalErrors : $ ( '#ignoreEvalErrors' ) . value === 'true'
43
+ } ) ;
44
+ $ ( '#results' ) . value = JSON . stringify ( result , null , 2 ) ;
45
+ } catch ( err ) {
46
+ jsonpathEl . setCustomValidity (
47
+ 'Error executing JSONPath: ' + err . toString ( )
48
+ ) ;
49
+ reportValidity ( ) ;
50
+ $ ( '#results' ) . value = '' ;
51
+ }
42
52
} ;
43
53
44
54
$ ( '#jsonpath' ) . addEventListener ( 'input' , ( ) => {
@@ -58,4 +68,3 @@ $('#ignoreEvalErrors').addEventListener('change', () => {
58
68
} ) ;
59
69
60
70
window . addEventListener ( 'load' , updateResults ) ;
61
-
0 commit comments