Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
fix(api): document and constrain always-required field types in API guide and OpenAPI (#205)
Kevin Rosed041d076f0ad
diff --git a/docs/api.md b/docs/api.mdindex e5d7e93..12c3f3b 100644--- a/docs/api.md+++ b/docs/api.md@@ -157,8 +157,11 @@ Things to know:its top left corner: `"x": 0.1, "y": 0.8` is 10% in from the left and 80%down. Types: `signature`, `initials`, `date`, `name` (filled in with thesigner's name when they sign), `text`, `checkbox`, `number`, `dropdown`- (with `options`), `radio` (buttons with one `groupId` are one question). `required` is true unless set to false. Every signer needs at- least one `signature` field.+ (with `options`), `radio` (buttons with one `groupId` are one question).+ `required` defaults to `true`. `signature`, `initials`, `date` and `name`+ fields are always required (`"required": false` is refused with 400); only+ `text`, `checkbox`, `number`, `dropdown` and `radio` fields may be set to+ `"required": false`. Every signer needs at least one `signature` field.- **Sending** needs `"permittedUseConfirmed": true`: your app confirms, as theweb app's send button asks a person to, that the envelope is an ordinarybusiness agreement and not one of the kinds of document that the law doesdiff --git a/src/lib/api/openapi.test.ts b/src/lib/api/openapi.test.tsindex 9d1caca..c7e5d6c 100644--- a/src/lib/api/openapi.test.ts+++ b/src/lib/api/openapi.test.ts@@ -3,8 +3,13 @@ import { readdir } from "node:fs/promises";import path from "node:path";import { test } from "node:test";-import { fieldTypeSchema } from "../schemas/field.ts";-import { signatureMeanings } from "../types/domain.ts";+import { fieldPlacementSchema, fieldTypeSchema } from "../schemas/field.ts";+import {+ alwaysRequiredFieldTypes,+ fieldTypes,+ signatureMeanings,+ type FieldType,+} from "../types/domain.ts";import { buildOpenApiDocument } from "./openapi.ts";// Every route file under src/app/api/v1 has a path in the OpenAPI@@ -69,6 +74,22 @@ test("the OpenAPI description allows the values the API accepts", () => {].schema.properties.signers.items.properties;assert.deepEqual(Field.properties.type.enum, fieldTypeSchema.options);+ assert.deepEqual(Field.if.properties.type.enum, [+ ...alwaysRequiredFieldTypes,+ ]);+ assert.deepEqual(Field.if.required, ["type"]);+ assert.equal(Field.then.properties.required.const, true);+ assert.equal(Field.properties.required.default, true);+ assert.equal(Field.properties.required.type, "boolean");++ for (const type of alwaysRequiredFieldTypes) {+ assert.match(+ Field.properties.required.description,+ new RegExp(`\\b${type}\\b`),+ `Field.properties.required.description should mention ${type}`,+ );+ }+assert.deepEqual(CreateEnvelope.properties.signers.items.properties.signatureMeaning.enum,[...signatureMeanings],@@ -76,6 +97,60 @@ test("the OpenAPI description allows the values the API accepts", () => {assert.deepEqual(fromTemplate.signatureMeaning.enum, [...signatureMeanings]);});+test("the OpenAPI Field schema matches server validation for required by field type", () => {+ const document = buildOpenApiDocument("https://sign.example.com/api/v1");+ const { Field } = document.components.schemas;+ const alwaysRequired = new Set<string>(Field.if.properties.type.enum);++ function openApiAllowsRequired(+ type: FieldType,+ required: boolean | undefined,+ ): boolean {+ if (required === undefined) {+ return true;+ }++ if (alwaysRequired.has(type)) {+ return required === Field.then.properties.required.const;+ }++ return typeof required === "boolean";+ }++ const basePlacement = {+ height: 0.05,+ page: 1,+ signerId: "signer-1",+ width: 0.25,+ x: 0.1,+ y: 0.5,+ };++ for (const type of fieldTypes) {+ const extra =+ type === "dropdown"+ ? { options: ["Yes", "No"] }+ : type === "radio"+ ? { groupId: "choice" }+ : {};++ for (const required of [undefined, true, false] as const) {+ const candidate =+ required === undefined+ ? { ...basePlacement, ...extra, type }+ : { ...basePlacement, ...extra, required, type };+ const serverAllowed = fieldPlacementSchema.safeParse(candidate).success;+ const schemaAllowed = openApiAllowsRequired(type, required);++ assert.equal(+ schemaAllowed,+ serverAllowed,+ `mismatch for type=${type}, required=${String(required)}`,+ );+ }+ }+});+test("the OpenAPI description gives this install's signer limit", () => {const document = buildOpenApiDocument("https://sign.example.com/api/v1", 40);const fromTemplate =diff --git a/src/lib/api/openapi.ts b/src/lib/api/openapi.tsindex e3a1302..ee2cb11 100644--- a/src/lib/api/openapi.ts+++ b/src/lib/api/openapi.ts@@ -5,6 +5,7 @@import { getMaxSignersPerEnvelope } from "../signer-limit.ts";import {+ alwaysRequiredFieldTypes,fieldTypes,MAX_SIGNERS_CEILING,signatureMeanings,@@ -249,6 +250,12 @@ export function buildOpenApiDocument(Field: {description:"Position and size are fractions of the page (0 to 1), measured from its top left corner.",+ if: {+ properties: {+ type: { enum: [...alwaysRequiredFieldTypes], type: "string" },+ },+ required: ["type"],+ },properties: {groupId: {description: "Radio buttons in one group.",@@ -265,7 +272,12 @@ export function buildOpenApiDocument(type: "array",},page: { minimum: 1, type: "integer" },- required: { default: true, type: "boolean" },+ required: {+ default: true,+ description:+ "Whether the signer must complete this field. Always required for signature, initials, date and name fields (false is refused with 400); text, checkbox, number, dropdown and radio fields may set false.",+ type: "boolean",+ },signer: { description: "The signer's ref.", type: "string" },type: { enum: [...fieldTypes], type: "string" },width: { exclusiveMinimum: 0, maximum: 1, type: "number" },@@ -273,6 +285,11 @@ export function buildOpenApiDocument(y: { maximum: 1, minimum: 0, type: "number" },},required: ["signer", "type", "page", "x", "y", "width", "height"],+ then: {+ properties: {+ required: { const: true, type: "boolean" },+ },+ },type: "object",},SendOptions: {diff --git a/src/lib/schemas/field.test.ts b/src/lib/schemas/field.test.tsindex 95bbdc0..c649299 100644--- a/src/lib/schemas/field.test.ts+++ b/src/lib/schemas/field.test.ts@@ -27,20 +27,41 @@ test("fields that fill themselves in or are the signature are always required",assert.equal(fieldPlacementSchema.safeParse({ ...placement, type }).success,true,- `a ${type} field was refused`,+ `a ${type} field with omitted required was refused`,+ );+ assert.equal(+ fieldPlacementSchema.safeParse({ ...placement, required: true, type })+ .success,+ true,+ `a ${type} field with required: true was refused`,);}});-test("a text field may be optional", () => {- assert.equal(- fieldPlacementSchema.safeParse({+test("interactive non-signature fields may be optional", () => {+ for (const candidate of [+ { ...placement, required: false, type: "text" as const },+ { ...placement, required: false, type: "checkbox" as const },+ { ...placement, required: false, type: "number" as const },+ {...placement,+ options: ["Yes", "No"],required: false,- type: "text",- }).success,- true,- );+ type: "dropdown" as const,+ },+ {+ ...placement,+ groupId: "choice",+ required: false,+ type: "radio" as const,+ },+ ]) {+ assert.equal(+ fieldPlacementSchema.safeParse(candidate).success,+ true,+ `an optional ${candidate.type} field was refused`,+ );+ }});test("dropdown choices accept 80-code-point emoji strings and refuse lone surrogates", () => {diff --git a/src/lib/schemas/field.ts b/src/lib/schemas/field.tsindex 7c19e9f..3531431 100644--- a/src/lib/schemas/field.ts+++ b/src/lib/schemas/field.ts@@ -6,7 +6,12 @@ import {MAX_NUMBER_ENTRY_LENGTH,MAX_RADIO_GROUP_BUTTONS,} from "../field-choices.ts";-import { fieldInputMethods, fieldTypes } from "../types/domain.ts";+import {+ alwaysRequiredFieldTypes,+ fieldInputMethods,+ fieldTypes,+ type FieldType,+} from "../types/domain.ts";import { toWellFormedText } from "../whole-characters.ts";import {idSchema,@@ -16,6 +21,10 @@ import {unixTimestampSchema,} from "./common.ts";+const alwaysRequiredFieldTypeSet: ReadonlySet<FieldType> = new Set(+ alwaysRequiredFieldTypes,+);+export const fieldTypeSchema = z.enum(fieldTypes);export const fieldInputMethodSchema = z.enum(fieldInputMethods);@@ -111,13 +120,7 @@ export const fieldPlacementSchema = z});}- if (- (field.type === "signature" ||- field.type === "initials" ||- field.type === "date" ||- field.type === "name") &&- !field.required- ) {+ if (alwaysRequiredFieldTypeSet.has(field.type) && !field.required) {ctx.addIssue({code: z.ZodIssueCode.custom,message: `${field.type} fields are always required`,diff --git a/src/lib/types/domain.ts b/src/lib/types/domain.tsindex ba1984d..7f4de86 100644--- a/src/lib/types/domain.ts+++ b/src/lib/types/domain.ts@@ -64,6 +64,17 @@ export const fieldTypes = ["radio",] as const;+/**+ * Field types that cannot be marked optional (`required: false`): signatures+ * and initials must be completed, and date and name fill themselves in.+ */+export const alwaysRequiredFieldTypes = [+ "signature",+ "initials",+ "date",+ "name",+] as const satisfies readonly (typeof fieldTypes)[number][];+export const fieldInputMethods = ["typed", "drawn", "auto"] as const;export const auditEventTypes = [