refactor(json-prettifier): more permissive json parser
This commit is contained in:
		
							parent
							
								
									d30cd8a9ab
								
							
						
					
					
						commit
						8089c60000
					
				| @ -24,25 +24,21 @@ | |||||||
| <script setup lang="ts"> | <script setup lang="ts"> | ||||||
| import TextareaCopyable from '@/components/TextareaCopyable.vue'; | import TextareaCopyable from '@/components/TextareaCopyable.vue'; | ||||||
| import { useValidation } from '@/composable/validation'; | import { useValidation } from '@/composable/validation'; | ||||||
|  | import { withDefaultOnError } from '@/utils/defaults'; | ||||||
|  | import JSON5 from 'json5'; | ||||||
| import { computed, ref } from 'vue'; | import { computed, ref } from 'vue'; | ||||||
| 
 | 
 | ||||||
| const inputElement = ref<HTMLElement>(); | const inputElement = ref<HTMLElement>(); | ||||||
| 
 | 
 | ||||||
| const rawJson = ref('{"hello": "world"}'); | const rawJson = ref('{"hello": "world"}'); | ||||||
| const cleanJson = computed(() => { | const cleanJson = computed(() => withDefaultOnError(() => JSON.stringify(JSON5.parse(rawJson.value), null, 3), '')); | ||||||
|   try { |  | ||||||
|     return JSON.stringify(JSON.parse(rawJson.value), null, 3); |  | ||||||
|   } catch (_) { |  | ||||||
|     return ''; |  | ||||||
|   } |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| const rawJsonValidation = useValidation({ | const rawJsonValidation = useValidation({ | ||||||
|   source: rawJson, |   source: rawJson, | ||||||
|   rules: [ |   rules: [ | ||||||
|     { |     { | ||||||
|       validator: (v) => v === '' || JSON.parse(v), |       validator: (v) => v === '' || JSON5.parse(v), | ||||||
|       message: 'Invalid json', |       message: 'Provided JSON is not valid.', | ||||||
|     }, |     }, | ||||||
|   ], |   ], | ||||||
| }); | }); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user