Skip to content

Commit a6d104d

Browse files
authored
Merge pull request #31 from webgme/issue/30-load-schema-errors
Dispatch schema compile errors
2 parents 56a9be7 + c8fcbb5 commit a6d104d

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

src/lib/SchemaForm.svelte

+15-6
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}

src/lib/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Reexport your entry components here
22
export { default as default } from "./SchemaForm.svelte";
33
export type { JSONSchema7 } from "json-schema";
4-
export type { default as ValidationError } from "./ValidationError";
4+
export { default as ValidationError } from "./ValidationError";
55
export type { default as UISchema } from "./UISchema";

src/routes/+page.svelte

+14-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import Button, { Label as BtnLabel } from "@smui/button"
77
import Textfield from '@smui/textfield';
88
import Snackbar, { Label as SBLabel, Actions } from '@smui/snackbar';
9-
import SchemaForm, { type ValidationError } from "$lib";
9+
import SchemaForm, { ValidationError } from "$lib";
1010
import schemas, { type TestSchema } from "../schemas";
1111
1212
import type UISchema from "$lib/UISchema";
@@ -18,7 +18,7 @@
1818
let schemaString = "";
1919
let uischemaString = "";
2020
let dataString = "";
21-
let validationError: ValidationError | null = null;
21+
let schemaError: Error | ValidationError | null = null;
2222
2323
let schemaForm: SchemaForm;
2424
let errorSnackbar: Snackbar;
@@ -27,7 +27,7 @@
2727
$: setSchemaString(schema);
2828
$: setUISchemaString(uischema);
2929
$: setDataString(data);
30-
$: if (validationError != null) errorSnackbar.open();
30+
$: if (schemaError != null) errorSnackbar.open();
3131
3232
async function setSchemaString(schema: TestSchema["schema"]) {
3333
await tick();
@@ -87,7 +87,7 @@
8787
try {
8888
schemaForm.download();
8989
} catch (error) {
90-
validationError = error as ValidationError;
90+
schemaError = error as Error;
9191
}
9292
}
9393
</script>
@@ -98,21 +98,23 @@
9898
<Label>{tab.name}</Label>
9999
</Tab>
100100
</TabBar>
101-
<SchemaForm {schema} {uischema} bind:data bind:this={schemaForm}>
101+
<SchemaForm {schema} {uischema} bind:data bind:this={schemaForm} on:error={(event) => schemaError = event.detail}>
102102
<Button on:click={download} type="button" variant="raised">
103103
<BtnLabel>Download</BtnLabel>
104104
</Button>
105105
</SchemaForm>
106106

107107
<Snackbar class="schema-error" bind:this={errorSnackbar}>
108108
<SBLabel>
109-
{#if validationError}
110-
{validationError.message}
111-
<ul>
112-
{#each validationError.errors as error}
113-
<li>{error.message}</li>
114-
{/each}
115-
</ul>
109+
{#if schemaError}
110+
{schemaError.message}
111+
{#if schemaError instanceof ValidationError}
112+
<ul>
113+
{#each schemaError.errors as error}
114+
<li>{error.message}</li>
115+
{/each}
116+
</ul>
117+
{/if}
116118
{:else}
117119
Unknown error
118120
{/if}

0 commit comments

Comments
 (0)