Skip to content

Commit 72e5da7

Browse files
author
Sean Wilson
committed
catch & dispatch schema compile errors
1 parent 56a9be7 commit 72e5da7

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/lib/SchemaForm.svelte

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<script lang="ts">
22
import 'core-js/actual/structured-clone';
33
import type { JSONSchema7Definition } from "json-schema";
4+
import { createEventDispatcher } from 'svelte';
45
import DownloadOptions, { type DataTransform } from './DowloadOptions';
56
import UISchema from "./UISchema";
67
import JsonSchemaDereferencer from "@json-schema-tools/dereferencer";
7-
import Ajv from "ajv";
8+
import Ajv, { type ValidateFunction } from "ajv";
89
import ajvFormats from "ajv-formats";
910
import mergeAllOf from "json-schema-merge-allof";
1011
import Paper, { Title, Subtitle, Content } from '@smui/paper';
@@ -18,6 +19,7 @@
1819
export let data: { [prop: string]: any } = {};
1920
export let uischema: UISchema = {};
2021
22+
const dispatch = createEventDispatcher();
2123
/* A bit of a hack - When bulding the static test site, the dereferencer is still behind a
2224
* `.default` property for some reason. I'm guessing it has something to do with how modules are
2325
* imported during the svelte build process. When running in browser, it appears to be imported
@@ -39,13 +41,22 @@
3941
download
4042
};
4143
let uischemaStore = UISchema.store(uischema);
44+
let validator: ValidateFunction;
4245
4346
$: dereferencing = new Dereferencer(
4447
isBoolean(schema) ? schema : mergeAllOf(structuredClone(schema)),
4548
{ mutate: true }
46-
).resolve();
49+
).resolve().catch(error => {
50+
dispatch("error", error);
51+
return null;
52+
});
4753
48-
$: validator = ajv.compile(schema);
54+
$: try {
55+
validator = ajv.compile(schema);
56+
}
57+
catch (error) {
58+
dispatch("error", error);
59+
}
4960
5061
$: updateUischemaStore(uischema);
5162
@@ -108,11 +119,9 @@
108119
<ObjectProps {...dereferenced} bind:data {uischema} force />
109120
</Content>
110121
</Paper>
111-
{:else}
122+
{:else if dereferenced != null}
112123
<Control schema={dereferenced} bind:data {uischema} force />
113124
{/if}
114-
{:catch error}
115-
<div class="error">ERROR: {error.message}</div>
116125
{/await}
117126

118127
{#if $$slots.default}

0 commit comments

Comments
 (0)