WIP: form attachments (e-mail only) — core, endpoint, Graph large upload
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
0ba0014ed5
commit
6003b3d7d7
@@ -1,5 +1,6 @@
|
||||
import type { BuilderField, BuilderFormTexts, BuilderStep, FieldCondition } from './builderTypes.js'
|
||||
import type { BuilderField, BuilderFileLimits, BuilderFormTexts, BuilderStep, FieldCondition } from './builderTypes.js'
|
||||
|
||||
import { describeFileTypes, fileExtensions, MB } from './attachments/fileTypes.js'
|
||||
import { NON_INPUT_TYPES } from './builderTypes.js'
|
||||
import { isEmail } from './validators.js'
|
||||
|
||||
@@ -102,12 +103,61 @@ export function visibleFields<F extends ConditionalField>(fields: F[] | null | u
|
||||
return list.filter((field) => visible(field))
|
||||
}
|
||||
|
||||
/** Starting values: checkbox → boolean, checkboxGroup → string[], the rest → string. */
|
||||
/** A selected file as the logic needs it — a browser `File` fits. */
|
||||
export type BuilderFile = { name: string; size: number }
|
||||
|
||||
/**
|
||||
* The limits of a File field in the browser: what the server resolved
|
||||
* (`field.resolved`), else the field's own values with the built-in types.
|
||||
*/
|
||||
export function fileFieldRules(field: BuilderField): Required<BuilderFileLimits> {
|
||||
const own = describeFileTypes(field.allowedTypes)
|
||||
const r = field.resolved ?? {}
|
||||
const maxSizeMB = r.maxSizeMB ?? field.maxSizeMB ?? 10
|
||||
return {
|
||||
accept: r.accept ?? own.accept,
|
||||
extensions: r.extensions ?? own.extensions,
|
||||
maxFiles: r.maxFiles ?? field.maxFiles ?? 1,
|
||||
maxSizeMB,
|
||||
maxTotalMB: r.maxTotalMB ?? maxSizeMB,
|
||||
typeLabels: r.typeLabels ?? own.labels,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The first problem with the files picked for a File field, as the visitor's
|
||||
* message — the same rules the server enforces (it checks the content too).
|
||||
*/
|
||||
export function fileFieldError(
|
||||
field: BuilderField,
|
||||
files: readonly BuilderFile[],
|
||||
texts: Pick<BuilderFormTexts, 'fileCount' | 'fileTooLarge' | 'fileType' | 'validation'>,
|
||||
): string | undefined {
|
||||
const rules = fileFieldRules(field)
|
||||
const label = field.label || field.name || ''
|
||||
const generic = texts.validation.replace('{field}', label)
|
||||
if (files.length > rules.maxFiles) {return (texts.fileCount ?? generic).replace('{field}', label).replace('{max}', String(rules.maxFiles))}
|
||||
for (const file of files) {
|
||||
if (rules.extensions.length > 0 && !rules.extensions.includes(fileExtensions(file.name).at(-1) ?? '')) {
|
||||
return (texts.fileType ?? generic).replace('{file}', file.name).replace('{types}', rules.typeLabels)
|
||||
}
|
||||
if (file.size > rules.maxSizeMB * MB || file.size === 0) {
|
||||
return (texts.fileTooLarge ?? generic).replace('{file}', file.name).replace('{max}', String(rules.maxSizeMB))
|
||||
}
|
||||
}
|
||||
if (files.reduce((sum, f) => sum + f.size, 0) > rules.maxTotalMB * MB) {
|
||||
return (texts.fileTooLarge ?? generic).replace('{file}', files.at(-1)?.name ?? '').replace('{max}', String(rules.maxTotalMB))
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** Starting values: checkbox → boolean, checkboxGroup → string[], file → File[], the rest → string. */
|
||||
export function initialBuilderValues(fields: BuilderField[] | null | undefined): Record<string, unknown> {
|
||||
return Object.fromEntries(
|
||||
inputFields(fields).map((f) => {
|
||||
if (f.blockType === 'checkbox') {return [f.name, f.defaultValue === true]}
|
||||
if (f.blockType === 'checkboxGroup') {return [f.name, Array.isArray(f.defaultValue) ? f.defaultValue : []]}
|
||||
if (f.blockType === 'file') {return [f.name, []]}
|
||||
const v = f.defaultValue
|
||||
return [f.name, typeof v === 'string' || typeof v === 'number' ? String(v) : '']
|
||||
}),
|
||||
@@ -120,7 +170,7 @@ const isBlank = isBlankValue
|
||||
export function validateBuilderFields(
|
||||
fields: BuilderField[],
|
||||
values: Record<string, unknown>,
|
||||
texts: Pick<BuilderFormTexts, 'validation'>,
|
||||
texts: Pick<BuilderFormTexts, 'fileCount' | 'fileTooLarge' | 'fileType' | 'validation'>,
|
||||
): Record<string, string> {
|
||||
const errors: Record<string, string> = {}
|
||||
for (const f of inputFields(fields)) {
|
||||
@@ -130,7 +180,11 @@ export function validateBuilderFields(
|
||||
if (f.required) {errors[f.name] = msg}
|
||||
continue
|
||||
}
|
||||
if (f.blockType === 'email' && !isEmail(v)) {errors[f.name] = msg}
|
||||
if (f.blockType === 'file') {
|
||||
const problem = Array.isArray(v) ? fileFieldError(f, v as BuilderFile[], texts) : msg
|
||||
if (problem) {errors[f.name] = problem}
|
||||
}
|
||||
else if (f.blockType === 'email' && !isEmail(v)) {errors[f.name] = msg}
|
||||
else if (f.blockType === 'number' && !Number.isFinite(Number(v))) {errors[f.name] = msg}
|
||||
else if (f.blockType === 'checkboxGroup' && Array.isArray(v)) {
|
||||
if ((f.minSelected && v.length < f.minSelected) || (f.maxSelected && v.length > f.maxSelected)) {errors[f.name] = msg}
|
||||
|
||||
Reference in New Issue
Block a user