aboutsummaryrefslogtreecommitdiff
path: root/src/tools/json-diff/json-diff.vue
blob: df106c63e4b3e3ad5569fb99230944337869f001 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<template>
  <n-form-item label="Your first json" v-bind="leftJsonValidation.attrs">
    <n-input
      v-model:value="rawLeftJson"
      placeholder="Paste your first json here..."
      type="textarea"
      rows="20"
      autocomplete="off"
      autocorrect="off"
      autocapitalize="off"
      spellcheck="false"
      :input-props="{ 'data-test-id': 'leftJson' }"
    />
  </n-form-item>
  <n-form-item label="Your json to compare" v-bind="rightJsonValidation.attrs">
    <n-input
      v-model:value="rawRightJson"
      placeholder="Paste your json to compare here..."
      type="textarea"
      rows="20"
      autocomplete="off"
      autocorrect="off"
      autocapitalize="off"
      spellcheck="false"
      :input-props="{ 'data-test-id': 'rightJson' }"
    />
  </n-form-item>

  <DiffsViewer :left-json="leftJson" :right-json="rightJson" />
</template>

<script setup lang="ts">
import JSON5 from 'json5';

import { withDefaultOnError } from '@/utils/defaults';
import { useValidation } from '@/composable/validation';
import { isNotThrowing } from '@/utils/boolean';
import DiffsViewer from './diff-viewer/diff-viewer.vue';

const rawLeftJson = ref('');
const rawRightJson = ref('');

const leftJson = computed(() => withDefaultOnError(() => JSON5.parse(rawLeftJson.value), undefined));
const rightJson = computed(() => withDefaultOnError(() => JSON5.parse(rawRightJson.value), undefined));

const createJsonValidation = (json: Ref) =>
  useValidation({
    source: json,
    rules: [
      {
        validator: (value) => value === '' || isNotThrowing(() => JSON5.parse(value)),
        message: 'Invalid JSON',
      },
    ],
  });

const leftJsonValidation = createJsonValidation(rawLeftJson);
const rightJsonValidation = createJsonValidation(rawRightJson);
</script>