import { FormFieldRefSchema } from '../identity/FormFieldRef.schema'; import { PageRefSchema } from '../identity/PageRef.schema'; import { AnnotationRefSchema } from '../annotation/base.schema'; import { z } from 'zod'; import { WidgetDTOSchema } from '../annotation/kinds/widget'; import { WIDGET_STYLE_SHAPE, WidgetAppearanceSchema } from '../annotation/kinds/widget.shared'; import { PageBoxSchema, PdfRotationSchema } from '../geometry/schemas'; import type { FormWidget } from '../identity/FormFieldRef'; import type { FormFieldDraft, FormFieldOptionInput, WidgetPlacement } from './draft'; import type { FormFieldPatch } from './patch'; import type { FormField, FormFieldOption, FormFieldWidget, ToggleFieldWidget } from './field'; import type { FormKind, FormSnapshot } from './snapshot'; import type { FormFieldValue } from './value'; import type { FormValueEntry } from './value-entry'; import { IsoDateTimeSchema } from '../dto/IsoDateTime.schema'; import { FieldActionsPatchSchema, PdfFieldActionsSchema, WidgetActionsPatchSchema, } from '../dto/PdfAction.schema'; import { closed, withCatchAll } from '../wire/reading'; export { FormFieldRefSchema }; const FormWidgetShape = { // The annotation address, present exactly when the widget is indirect and placed. ref: AnnotationRefSchema.nullable(), // 0 = direct (unaddressable) widget; null page = unplaced widget. objectNumber: z.number().int().nonnegative(), page: PageRefSchema.nullable(), }; export const FormWidgetSchema: z.ZodType = z.object(FormWidgetShape); export const FormFieldWidgetSchema: z.ZodType = FormWidgetSchema; export const ToggleFieldWidgetSchema: z.ZodType = z.object({ ...FormWidgetShape, onState: z.string(), exportValue: z.string(), checked: z.boolean(), }); export const FormFieldOptionSchema: z.ZodType = z.object({ label: z.string(), value: z.string(), selected: z.boolean(), }); /** A `/V` of a kind this client doesn't know reads as `unsupported`. */ export const FormValueEntrySchema: z.ZodType = withCatchAll( z.discriminatedUnion('kind', [ z.object({ kind: z.literal('none') }), z.object({ kind: z.literal('scalar'), value: z.string() }), z.object({ kind: z.literal('array'), values: z.array(z.string()) }), z.object({ kind: z.literal('unsupported') }), ]), 'unsupported', ).describe( "A field's stored value, by its `kind`. A newer server can add kinds: read one your client " + "doesn't know as `unsupported`.", ); const FormFieldBaseShape = { ref: FormFieldRefSchema, name: z.string(), origin: closed(z.enum(['acroform', 'recovered'])), readOnly: z.boolean(), required: z.boolean(), noExport: z.boolean(), alternateName: z.string().nullable(), mappingName: z.string().nullable(), valueEntry: FormValueEntrySchema, defaultValueEntry: FormValueEntrySchema, actions: PdfFieldActionsSchema.optional(), groupId: z.string().nullable(), createdBy: z.string().nullable(), createdAt: IsoDateTimeSchema.nullable(), filledBy: z.string().nullable(), filledByName: z.string().nullable(), filledAt: IsoDateTimeSchema.nullable(), importedBy: z.string().nullable(), widgets: z.array(FormFieldWidgetSchema), }; /** * A field of a kind this client doesn't know reads as `unknown`, with the * body's `rawValue` when it carries one and `''` when it doesn't. */ export const FormFieldDTOSchema: z.ZodType = withCatchAll( z.discriminatedUnion('kind', [ z.object({ ...FormFieldBaseShape, kind: z.literal('text'), value: z.string(), defaultValue: z.string(), maxLength: z.number().int().positive().nullable(), multiline: z.boolean(), password: z.boolean(), comb: z.boolean(), }), z.object({ ...FormFieldBaseShape, kind: z.literal('checkbox'), checked: z.boolean(), exportValue: z.string(), widgets: z.array(ToggleFieldWidgetSchema), }), z.object({ ...FormFieldBaseShape, kind: z.literal('radio'), value: z.string(), radiosInUnison: z.boolean(), noToggleToOff: z.boolean(), widgets: z.array(ToggleFieldWidgetSchema), }), z.object({ ...FormFieldBaseShape, kind: z.literal('combobox'), value: z.string(), defaultValue: z.string(), edit: z.boolean(), options: z.array(FormFieldOptionSchema), }), z.object({ ...FormFieldBaseShape, kind: z.literal('listbox'), selectedValues: z.array(z.string()), defaultValue: z.array(z.string()), multiSelect: z.boolean(), options: z.array(FormFieldOptionSchema), topIndex: z.number().int().nonnegative(), }), z.object({ ...FormFieldBaseShape, kind: z.literal('pushbutton'), }), z.object({ ...FormFieldBaseShape, kind: z.literal('signature'), }), z.object({ ...FormFieldBaseShape, kind: z.literal('unknown'), rawValue: z.string(), }), ]) as unknown as z.ZodType, 'unknown', (field) => ({ ...field, rawValue: typeof field.rawValue === 'string' ? field.rawValue : '' }), ).describe( 'A form field, by its `family`. A newer server can add families: read one your client ' + "doesn't know as `unknown`, keeping the fields every field has, with `rawValue` an empty " + 'string when the body has none.', ); export const FormKindSchema: z.ZodType = closed(z.enum(['none', 'acroform', 'xfa'])); export const FormSnapshotSchema: z.ZodType = z.object({ formKind: FormKindSchema, needsAppearances: z.boolean(), fields: z.array(FormFieldDTOSchema), widgets: z.array(WidgetDTOSchema), calculationOrder: z.array(FormFieldRefSchema.nullable()), }) as unknown as z.ZodType; /** * `{ value }`, `{ checked }` or `{ selectedValues }`: one object with exactly * one of the three, so the wire has no untagged union (the API reference * labels variants by a discriminating literal, and a value has none to add). */ export const FormFieldValueSchema: z.ZodType = z .object({ value: z.string().nullable().optional(), checked: z.boolean().optional(), selectedValues: z.array(z.string()).optional(), }) .strict() .refine( (value) => [value.value, value.checked, value.selectedValues].filter((set) => set !== undefined) .length === 1, { message: 'exactly one of value, checked or selectedValues' }, ) as unknown as z.ZodType; export { WidgetAppearanceSchema }; export const WidgetPlacementSchema: z.ZodType = z .object({ page: PageRefSchema, rect: PageBoxSchema.optional(), box: PageBoxSchema.optional(), rotation: PdfRotationSchema.optional(), exportValue: z.string().min(1).optional(), actions: WidgetActionsPatchSchema.optional(), ...WIDGET_STYLE_SHAPE, }) .strict() as unknown as z.ZodType; export const FormFieldOptionInputSchema: z.ZodType = z.object({ label: z.string(), value: z.string(), }); const FormFieldDraftBaseShape = { name: z.string().min(1), readOnly: z.boolean().optional(), required: z.boolean().optional(), noExport: z.boolean().optional(), alternateName: z.string().optional(), mappingName: z.string().optional(), groupId: z.string().min(1).max(256).optional(), actions: FieldActionsPatchSchema.optional(), widgets: z.array(WidgetPlacementSchema).optional(), }; /** A draft that names a member its kind doesn't have is refused, not trimmed. */ export const FormFieldDraftSchema: z.ZodType = z.discriminatedUnion('kind', [ z .object({ ...FormFieldDraftBaseShape, kind: z.literal('text'), defaultValue: z.string().optional(), maxLength: z.number().int().positive().optional(), multiline: z.boolean().optional(), password: z.boolean().optional(), comb: z.boolean().optional(), }) .strict(), z.object({ ...FormFieldDraftBaseShape, kind: z.literal('checkbox') }).strict(), z .object({ ...FormFieldDraftBaseShape, kind: z.literal('radio'), radiosInUnison: z.boolean().optional(), noToggleToOff: z.boolean().optional(), }) .strict(), z .object({ ...FormFieldDraftBaseShape, kind: z.literal('combobox'), edit: z.boolean().optional(), options: z.array(FormFieldOptionInputSchema).optional(), defaultValue: z.string().optional(), }) .strict(), z .object({ ...FormFieldDraftBaseShape, kind: z.literal('listbox'), multiSelect: z.boolean().optional(), options: z.array(FormFieldOptionInputSchema).optional(), defaultValue: z.array(z.string()).optional(), }) .strict(), z.object({ ...FormFieldDraftBaseShape, kind: z.literal('pushbutton') }).strict(), z.object({ ...FormFieldDraftBaseShape, kind: z.literal('signature') }).strict(), ]) as unknown as z.ZodType; const FormFieldPatchBaseShape = { name: z.string().min(1).optional(), readOnly: z.boolean().optional(), required: z.boolean().optional(), noExport: z.boolean().optional(), alternateName: z.string().nullable().optional(), mappingName: z.string().nullable().optional(), groupId: z.string().min(1).max(256).optional(), actions: FieldActionsPatchSchema.optional(), }; /** * One object on the wire: the kind is optional (the engine knows it from * the ref), so there is no discriminator, and the engine refuses members * the field's kind doesn't have. */ export const FormFieldPatchSchema: z.ZodType = z .object({ ...FormFieldPatchBaseShape, kind: z .enum(['text', 'checkbox', 'radio', 'combobox', 'listbox', 'pushbutton', 'signature']) .optional(), // A string for text fields and dropdowns, option values for a list. defaultValue: z .union([z.string(), z.array(z.string())]) .nullable() .optional(), maxLength: z.number().int().positive().nullable().optional(), multiline: z.boolean().optional(), password: z.boolean().optional(), comb: z.boolean().optional(), radiosInUnison: z.boolean().optional(), noToggleToOff: z.boolean().optional(), edit: z.boolean().optional(), multiSelect: z.boolean().optional(), options: z.array(FormFieldOptionInputSchema).optional(), }) .strict() as unknown as z.ZodType;