add require non-empty array

This commit is contained in:
Ruben Fiszel
2025-11-10 09:51:30 +00:00
parent 4be973f7e0
commit 246f105e93
4 changed files with 27 additions and 1 deletions
@@ -400,6 +400,7 @@
const UUID_PATTERN = '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
const IPV6_PATTERN =
'^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$'
function validateInput(pattern: string | undefined, v: any, required: boolean): void {
if (nullable && emptyString(v)) {
error = ''
@@ -407,6 +408,18 @@
} else if (required && (v == undefined || v == null || v === '') && inputCat != 'object') {
error = 'Required'
valid && (valid = false)
} else if (
required &&
inputCat == 'list' &&
extra?.['nonEmpty'] == true &&
Array.isArray(v) &&
v.length === 0
) {
error = 'Required'
valid && (valid = false)
} else if (inputCat == 'list' && !Array.isArray(v)) {
error = 'Expected an array, got ' + typeof v + ' instead'
valid && (valid = false)
} else {
if (inputCat == 'number' && typeof v === 'number') {
let min = extra['min']
@@ -523,6 +536,7 @@
})
$effect(() => {
extra?.['nonEmpty']
let args = [pattern, value, required] as const
untrack(() => validateInput(...args))
})
@@ -24,12 +24,14 @@
properties?: { [name: string]: SchemaProperty }
}
| undefined
nonEmpty?: boolean | undefined
}
let {
canEditResourceType = false,
originalType = undefined,
itemsType = $bindable()
itemsType = $bindable(),
nonEmpty = $bindable()
}: Props = $props()
let selected:
@@ -143,6 +145,12 @@
</div>
{/each}
</div>
<Toggle
size="sm"
class="mt-8"
bind:checked={nonEmpty}
options={{ right: 'Require non-empty array' }}
/>
{#if canEditResourceType || originalType == 'string[]' || originalType == 'object[]'}
<div class="flex flex-row mb-1 mt-2">
<Button
@@ -668,6 +668,7 @@
bind:customErrorMessage={
schema.properties[argName].customErrorMessage
}
bind:nonEmpty={schema.properties[argName].nonEmpty}
bind:itemsType={schema.properties[argName].items}
bind:extra={schema.properties[argName]}
bind:title={schema.properties[argName].title}
@@ -32,6 +32,7 @@
title?: string | undefined
placeholder?: string | undefined
properties?: Record<string, any> | undefined
nonEmpty?: boolean | undefined
isFlowInput?: boolean
isAppInput?: boolean
order?: string[] | undefined
@@ -57,6 +58,7 @@
pattern = $bindable(undefined),
enum_ = $bindable(undefined),
extra = $bindable({}),
nonEmpty = $bindable(undefined),
minW = true,
customErrorMessage = $bindable(undefined),
title = $bindable(undefined),
@@ -219,6 +221,7 @@
originalType={extra['originalType']}
bind:itemsType
canEditResourceType={isFlowInput || isAppInput}
bind:nonEmpty
/>
{:else if type == 'string' || ['number', 'integer'].includes(type ?? '')}
<div>