From f543aa5bf98492bd4fc40a0dff31b80d121f5c12 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Sun, 19 Nov 2023 13:54:32 +0100 Subject: [PATCH] feat: custom error messages for forms --- frontend/src/lib/common.ts | 31 ++++ frontend/src/lib/components/ArgInput.svelte | 33 ++++- .../lib/components/LightweightArgInput.svelte | 135 ++++++++++-------- .../components/LightweightSchemaForm.svelte | 1 + .../src/lib/components/SchemaEditor.svelte | 23 +-- frontend/src/lib/components/SchemaForm.svelte | 2 + .../src/lib/components/SchemaModal.svelte | 32 +++-- .../lib/components/StringTypeNarrowing.svelte | 23 +++ frontend/src/lib/infer.ts | 1 - frontend/src/lib/utils.ts | 3 + 10 files changed, 192 insertions(+), 92 deletions(-) diff --git a/frontend/src/lib/common.ts b/frontend/src/lib/common.ts index 3d829003b2..2a7830eaa4 100644 --- a/frontend/src/lib/common.ts +++ b/frontend/src/lib/common.ts @@ -28,10 +28,41 @@ export interface SchemaProperty { contentEncoding?: 'base64' enum?: string[] } + customErrorMessage?: string properties?: { [name: string]: SchemaProperty } required?: string[] } +export interface ModalSchemaProperty { + selectedType?: string + description: string + name: string + required: boolean + format?: string + pattern?: string + enum_?: string[] + default?: any + items?: { type?: 'string' | 'number' } + contentEncoding?: 'base64' | 'binary' + schema?: Schema + customErrorMessage?: string +} + +export function modalToSchema(schema: ModalSchemaProperty): SchemaProperty { + return { + type: schema.selectedType, + description: schema.description, + pattern: schema.pattern, + default: schema.default, + enum: schema.enum_, + items: schema.items, + contentEncoding: schema.contentEncoding, + format: schema.format, + customErrorMessage: schema.customErrorMessage, + properties: schema.schema?.properties, + required: schema.schema?.required + } +} export type Schema = { $schema: string | undefined type: string diff --git a/frontend/src/lib/components/ArgInput.svelte b/frontend/src/lib/components/ArgInput.svelte index 3ea5436a8e..ff67e061f6 100644 --- a/frontend/src/lib/components/ArgInput.svelte +++ b/frontend/src/lib/components/ArgInput.svelte @@ -1,6 +1,6 @@ @@ -209,47 +214,51 @@ {:else if inputCat == 'list'}
- {#each value ?? [] as v, i} -
- {#if itemsType?.type == 'number'} - - {:else if itemsType?.type == 'string' && itemsType?.contentEncoding == 'base64'} - fileChanged(x, (val) => (value[i] = val))} - multiple={false} - /> - {:else if Array.isArray(itemsType?.enum)} - + {:else if itemsType?.type == 'string' && itemsType?.contentEncoding == 'base64'} + fileChanged(x, (val) => (value[i] = val))} + multiple={false} + /> + {:else if Array.isArray(itemsType?.enum)} + + {:else} + + {/if} + -
- {/each} + + +
+ {/each} + {:else} + List is not an array + {/if}
- {(value ?? []).length} item{(value ?? []).length > 1 ? 's' : ''} + {(value ?? []).length} item{(value ?? []).length != 1 ? 's' : ''} {:else if inputCat == 'resource-object'} @@ -312,12 +321,16 @@ {:else if inputCat == 'date'} {:else if inputCat == 'base64'} - fileChanged(x, (val) => (value = val))} - multiple={false} - /> +
+ fileChanged(x, (val) => (value = val))} + multiple={false} + /> + {#if value?.length} +
File length: {value.length} base64 chars
+ {/if} +
{:else if inputCat == 'resource-string'}
+ {:else if inputCat == 'email'} + {:else if inputCat == 'string'}
diff --git a/frontend/src/lib/components/LightweightSchemaForm.svelte b/frontend/src/lib/components/LightweightSchemaForm.svelte index fdaceff84e..be9ae2ff18 100644 --- a/frontend/src/lib/components/LightweightSchemaForm.svelte +++ b/frontend/src/lib/components/LightweightSchemaForm.svelte @@ -75,6 +75,7 @@ enum_={schema.properties[argName].enum} format={schema.properties[argName].format} contentEncoding={schema.properties[argName].contentEncoding} + customErrorMessage={schema.properties[argName].customErrorMessage} properties={schema.properties[argName].properties} nestedRequired={schema.properties[argName].required} itemsType={schema.properties[argName].items} diff --git a/frontend/src/lib/components/SchemaEditor.svelte b/frontend/src/lib/components/SchemaEditor.svelte index ea2598ae1d..f0c1eca492 100644 --- a/frontend/src/lib/components/SchemaEditor.svelte +++ b/frontend/src/lib/components/SchemaEditor.svelte @@ -1,9 +1,14 @@