import { FormFieldRefSchema } from '../identity/FormFieldRef.schema'; import { PageRefSchema } from '../identity/PageRef.schema'; import { AnnotationRefSchema } from '../annotation/base.schema'; import * as 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 { isDatePattern, isTimePattern } from './datePattern'; import type { FormFieldDraft, FormFieldOptionInput, WidgetPlacement } from './draft'; import { withAssignment, type FieldAssignment } from './assignment'; import { withButtonImage } from './buttonImage'; import { withLockedBy } from './lockedBy'; import type { FieldSignatureLock } from '../signature/types'; import { withPurposeAndFormat, type FormFieldFormat, type FormFieldFormatWritable } from './format'; import type { FormFieldPatch } from './patch'; import type { FormField, FormFieldOption, FormFieldWidget, ToggleFieldWidget } from './field'; import { FIELD_PLACEHOLDER_MAX_LENGTH, isFieldPlaceholder, withPlaceholder } from './placeholder'; import { FIELD_PURPOSE_MAX_LENGTH, isFieldPurpose } from './purpose'; 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`.", ); /** * A text field's or a dropdown's format, as a response carries it. A kind * this client doesn't know reads as `other`. */ export const FormFieldFormatSchema: z.ZodType = withCatchAll( z.discriminatedUnion('kind', [ z.object({ kind: z.literal('date'), pattern: z.string() }), z.object({ kind: z.literal('time'), pattern: z.string() }), z.object({ kind: z.literal('other') }), ]), 'other', ).describe( "The shape a text field's or a dropdown's value has, by its `kind`: a date or a time with " + 'the pattern it is shown in, or `other` for a format script the engine does not recognise. ' + "A newer server can add kinds: read one your client doesn't know as `other`.", ); /** A pattern a date format is written with: see `isDatePattern`. */ const DatePatternSchema = z .string() .min(1) .max(64) .refine(isDatePattern, { message: 'a date pattern is made of the groups yyyy yy mmmm mmm mm m dd d, at least one of them ' + 'and the year, the month and the day each once at most, with spaces and / - . , between ' + 'them, and at most one time such as HH:MM or h:MM:ss tt', }); /** A pattern a time format is written with: see `isTimePattern`. */ const TimePatternSchema = z .string() .min(1) .max(64) .refine(isTimePattern, { message: 'a time pattern is a time and nothing else: HH, H, hh or h, then :MM, then :ss when it ' + 'has seconds, then tt when it has one', }); /** The format a create or an update writes: a date or a time. `other` is read, never written. */ export const FormFieldFormatWritableSchema: z.ZodType = z.discriminatedUnion('kind', [ z.object({ kind: z.literal('date'), pattern: DatePatternSchema }).strict(), z.object({ kind: z.literal('time'), pattern: TimePatternSchema }).strict(), ]); /** A field's purpose as a create or an update names it: see `isFieldPurpose`. */ const FieldPurposeSchema = z.string().min(1).max(FIELD_PURPOSE_MAX_LENGTH).refine(isFieldPurpose, { message: 'a purpose has no space at its start or its end, and no NUL character', }); /** A field's placeholder as a create or an update names it: see `isFieldPlaceholder`. */ const FieldPlaceholderSchema = z .string() .min(1) .max(FIELD_PLACEHOLDER_MAX_LENGTH) .refine(isFieldPlaceholder, { message: 'a placeholder has no space at its start or its end, and no NUL character', }); /** * Who fills a field in, by its `kind`. Closed: a client that doesn't know * an assignment couldn't tell who may fill the field in. No `.describe()`: * a draft and a patch take it as an optional member, which would carry the * description beside the component's reference, and each SDK would then * make a type of its own for every kind of draft. */ export const FieldAssignmentSchema: z.ZodType = closed( z.discriminatedUnion('kind', [ z.object({ kind: z.literal('group'), groupId: z.string() }), z.object({ kind: z.literal('anyone') }), z.object({ kind: z.literal('unassigned') }), ]), ); /** * Which signed signature locked a field, and by what. No `.describe()`: a * nullable member written with a description beside the reference would * give each SDK a type of its own for every kind of field. */ export const FieldSignatureLockSchema: z.ZodType = z.object({ signatureIndex: z.number().int().nonnegative(), source: closed(z.enum(['fieldmdp', 'lock'])), }); 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(), assignment: FieldAssignmentSchema, lockedBy: FieldSignatureLockSchema.nullable(), purpose: z.string().nullable(), placeholder: 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(), format: FormFieldFormatSchema.nullable(), }), 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), format: FormFieldFormatSchema.nullable(), }), 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'), hasImage: z.boolean(), takesImage: z .boolean() .describe( 'Whether the push button is an image field, which a person fills with a picture: it ' + "has a widget, and every widget's press runs Acrobat's import-icon script " + '(`event.target.buttonImportIcon()`) and nothing else.', ), }), 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 : '' }), (field) => withLockedBy(withPlaceholder(withAssignment(withButtonImage(withPurposeAndFormat(field))))), ).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).nullable().optional(), assignment: FieldAssignmentSchema.optional(), purpose: FieldPurposeSchema.nullable().optional(), placeholder: FieldPlaceholderSchema.nullable().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(), format: FormFieldFormatWritableSchema.nullable().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(), format: FormFieldFormatWritableSchema.nullable().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'), takesImage: z .boolean() .optional() .describe( "Make the push button an image field: the engine writes Acrobat's import-icon script " + "as each widget's activation action. It needs a widget, and can't go beside a " + "widget's `actions.activate` or `actions.mouseUp`.", ), }) .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).nullable().optional(), assignment: FieldAssignmentSchema.optional(), purpose: FieldPurposeSchema.nullable().optional(), placeholder: FieldPlaceholderSchema.nullable().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(), // A text field's or a dropdown's. format: FormFieldFormatWritableSchema.nullable().optional(), takesImage: z .boolean() .optional() .describe( "A push button's: `true` makes it an image field, writing Acrobat's import-icon script " + 'as the activation action of each widget whose press does nothing, and is refused ' + "while a widget's press runs another action. `false` takes the script off each widget " + 'whose press is exactly it.', ), }) .strict() as unknown as z.ZodType;