Skip to content

Commit

Permalink
fix: json schema generation (#150)
Browse files Browse the repository at this point in the history
Co-authored-by: Puria Nafisi Azizi <[email protected]>
  • Loading branch information
phoebus-84 and puria authored Feb 22, 2024
1 parent a7441e9 commit f10902b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
1 change: 1 addition & 0 deletions messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"Get_this_credential": "Get this credential",
"Decline": "Decline",
"Scan": "Scan",
"We_are_generating_this_credential": "We are generating this credential",
"Nothing_in_your_wallet": "Nothing in your wallet",
"Start_getting_your_first_credential": "Start getting your first credential",
"See_issuance_services": "See issuance services"
Expand Down
11 changes: 5 additions & 6 deletions src/lib/JSONSchemaForms/JSONSchemaForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import { Form, FormError } from '$lib/forms';
import ErrorDisplay from '$lib/components/errorDisplay.svelte';
import { objectSchemaToSuperformsValidators } from './errors';
import { m } from '$lib/i18n';
import { unlockApp } from '$lib/preferences/locked';
//
export let schema: ObjectSchema;
export let id: string;
export let onSubmit: (data: Record<string, unknown>) => Promise<void> | void = () => {};
//
Expand All @@ -24,7 +25,9 @@
validators: objectSchemaToSuperformsValidators(schema),
onUpdate: async ({ form }) => {
try {
await unlockApp();
await onSubmit(form.data);
} catch (e) {
setError(form, parseFormException(e));
}
Expand All @@ -40,7 +43,7 @@
}
</script>

<Form {form}>
<Form {form} {id}>
<div class="space-y-4">
{#each Object.entries(schema.properties) as [fieldName, field]}
{@const required = schema.required?.includes(fieldName)}
Expand All @@ -50,9 +53,5 @@
<FormError {form} let:errorMessage>
<ErrorDisplay name="Form Error" message={errorMessage} />
</FormError>

<div class="flex justify-end">
<d-button type="submit">{m.Submit()}</d-button>
</div>
</div>
</Form>
8 changes: 5 additions & 3 deletions src/lib/JSONSchemaForms/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Ajv, { type Options } from 'ajv';
import { type Options } from 'ajv';
import addFormats from 'ajv-formats';
import type { SuperValidated, ZodValidation } from 'sveltekit-superforms';
import type z from 'zod';
import Ajv2020 from 'ajv/dist/2020';


//

export function createAjv(options: Options = {}): Ajv {
const ajv = new Ajv(options);
export function createAjv(options: Options = {}): Ajv2020 {
const ajv = new Ajv2020(options);
addFormats(ajv);
return ajv;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/forms/form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@
<script lang="ts">
type SchemaGeneric = $$Generic<AnyZodObject>;
export let form: SuperForm<SchemaGeneric>;
export let id: string | undefined = undefined
const { enhance, delayed } = form;
</script>

<form method="post" use:enhance>
<form {id} method="post" use:enhance>
<slot delayed={$delayed} />
</form>

Expand Down
24 changes: 15 additions & 9 deletions src/routes/[[lang]]/(protected)/[id]/credential-offer/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
}, 3000);
}, 5000);
};
const cleanSchema = () => {
const schema = credential.expand.templates[0].schema;
schema;
};
</script>

<Header>{m.Credential_offer()}</Header>
Expand All @@ -69,15 +74,16 @@
</div>
</div>

<JSONSchemaParser schema={credential.expand.templates[0].schema} let:schema>
<JSONSchemaForm {schema} onSubmit={() => {}} />
<svelte:fragment slot="error" let:error>
<ErrorDisplay name={error.name} message={error.message} />
</svelte:fragment>
</JSONSchemaParser>
<pre>{credential.expand.templates[0].schema}</pre>
<div class="rounded-md bg-primary p-4">
<JSONSchemaParser schema={credential.expand.templates[0].schema} let:schema>
<JSONSchemaForm {schema} onSubmit={getCredential} id="schemaForm" />
<svelte:fragment slot="error" let:error>
<ErrorDisplay name={error.name} message={error.message} />
</svelte:fragment>
</JSONSchemaParser>
</div>
<div class="w-full">
<d-button expand on:click={getCredential} on:keydown={getCredential} aria-hidden
<d-button expand type="submit" form="schemaForm" aria-hidden
>{m.Get_this_credential()}</d-button
>
<d-button expand href="/home">{m.Decline()}</d-button>
Expand All @@ -88,7 +94,7 @@
<div class="flex h-full flex-col justify-around">
<div>
{#if !isCredentialVerified}
We are generating this credential
{m.We_are_generating_this_credential()}
<d-credential-card
name={credential.name}
issuer={credential.issuer}
Expand Down

0 comments on commit f10902b

Please sign in to comment.