|
1 | 1 | <script lang="ts">
|
2 | 2 | import 'core-js/actual/structured-clone';
|
3 | 3 | import type { JSONSchema7Definition } from "json-schema";
|
| 4 | + import { createEventDispatcher } from 'svelte'; |
4 | 5 | import DownloadOptions, { type DataTransform } from './DowloadOptions';
|
5 | 6 | import UISchema from "./UISchema";
|
6 | 7 | import JsonSchemaDereferencer from "@json-schema-tools/dereferencer";
|
7 |
| - import Ajv from "ajv"; |
| 8 | + import Ajv, { type ValidateFunction } from "ajv"; |
8 | 9 | import ajvFormats from "ajv-formats";
|
9 | 10 | import mergeAllOf from "json-schema-merge-allof";
|
10 | 11 | import Paper, { Title, Subtitle, Content } from '@smui/paper';
|
|
18 | 19 | export let data: { [prop: string]: any } = {};
|
19 | 20 | export let uischema: UISchema = {};
|
20 | 21 |
|
| 22 | + const dispatch = createEventDispatcher(); |
21 | 23 | /* A bit of a hack - When bulding the static test site, the dereferencer is still behind a
|
22 | 24 | * `.default` property for some reason. I'm guessing it has something to do with how modules are
|
23 | 25 | * imported during the svelte build process. When running in browser, it appears to be imported
|
|
39 | 41 | download
|
40 | 42 | };
|
41 | 43 | let uischemaStore = UISchema.store(uischema);
|
| 44 | + let validator: ValidateFunction; |
42 | 45 |
|
43 | 46 | $: dereferencing = new Dereferencer(
|
44 | 47 | isBoolean(schema) ? schema : mergeAllOf(structuredClone(schema)),
|
45 | 48 | { mutate: true }
|
46 |
| - ).resolve(); |
| 49 | + ).resolve().catch(error => { |
| 50 | + dispatch("error", error); |
| 51 | + return null; |
| 52 | + }); |
47 | 53 |
|
48 |
| - $: validator = ajv.compile(schema); |
| 54 | + $: try { |
| 55 | + validator = ajv.compile(schema); |
| 56 | + } |
| 57 | + catch (error) { |
| 58 | + dispatch("error", error); |
| 59 | + } |
49 | 60 |
|
50 | 61 | $: updateUischemaStore(uischema);
|
51 | 62 |
|
|
108 | 119 | <ObjectProps {...dereferenced} bind:data {uischema} force />
|
109 | 120 | </Content>
|
110 | 121 | </Paper>
|
111 |
| - {:else} |
| 122 | + {:else if dereferenced != null} |
112 | 123 | <Control schema={dereferenced} bind:data {uischema} force />
|
113 | 124 | {/if}
|
114 |
| - {:catch error} |
115 |
| - <div class="error">ERROR: {error.message}</div> |
116 | 125 | {/await}
|
117 | 126 |
|
118 | 127 | {#if $$slots.default}
|
|
0 commit comments