@composable-os/cli 0.1.0-beta.11 → 0.1.0-beta.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{accounts-UHYLCLEY.js → accounts-GOCNY6WI.js} +17 -13
- package/dist/app-RPRZMLU5.js +43 -0
- package/dist/app-upload-YJVEKZC6.js +198 -0
- package/dist/{blocks-5IFU25J6.js → blocks-DE4RRWPJ.js} +207 -25
- package/dist/{chunk-XIDLW3EP.js → chunk-2B6YUYZV.js} +23499 -11766
- package/dist/{run-7P4RQALH.js → chunk-2CCHB37H.js} +9 -146
- package/dist/{chunk-DU6OIFOU.js → chunk-3OGDWKJ3.js} +530 -434
- package/dist/{chunk-DHS2CV2M.js → chunk-5D7BDF4Y.js} +15 -13
- package/dist/{chunk-ZAWES2UD.js → chunk-6CM3WZG5.js} +13 -2483
- package/dist/{chunk-7D4GKZOX.js → chunk-CIBJGB5H.js} +422 -75
- package/dist/chunk-EPDTPGZJ.js +330 -0
- package/dist/{chunk-LAAL4YGR.js → chunk-HAYF4G5V.js} +66 -23
- package/dist/chunk-HCG6T53D.js +61904 -0
- package/dist/{chunk-K34UKWEI.js → chunk-JX4IFWNT.js} +64 -32
- package/dist/{chunk-6XTU2MTP.js → chunk-KFHKNXGH.js} +632 -418
- package/dist/chunk-KMFMD52Z.js +13276 -0
- package/dist/{chunk-E4WCYTCT.js → chunk-LX2TTLVY.js} +174 -55
- package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
- package/dist/{chunk-E64SG24J.js → chunk-Q6Y45DY6.js} +25 -2
- package/dist/{chunk-MBIKVRAC.js → chunk-QJZGYJVV.js} +823 -44
- package/dist/{chunk-PDD2ETVF.js → chunk-QQ3YSL5F.js} +172 -16
- package/dist/chunk-RGKJKC5T.js +876 -0
- package/dist/{chunk-YFTLIEWU.js → chunk-RSEWEFAV.js} +12 -2
- package/dist/{chunk-2ABSVMGB.js → chunk-SMJZIK2O.js} +1 -1
- package/dist/{chunk-LO6WGU5Y.js → chunk-U72NEGCT.js} +8089 -3464
- package/dist/{chunk-P6UWZ6QQ.js → chunk-Z4IKF23A.js} +11 -7
- package/dist/{chunk-2IVEVPMY.js → chunk-Z7OVQ6BC.js} +105 -61
- package/dist/{chunk-6UWRT4VM.js → chunk-ZJSFX4RD.js} +175 -3
- package/dist/{cli-5UNY5G5L.js → cli-OKQHHSOE.js} +17 -25
- package/dist/{cli-UYZMVQMR.js → cli-VYSD2S4Z.js} +227 -81
- package/dist/cli.js +5 -5
- package/dist/{client-repo-SE4EKG6W.js → client-repo-NNUCHQ2Y.js} +7 -5
- package/dist/create.js +5 -5
- package/dist/{fields-FTWWMD7N.js → fields-GTHE6TTS.js} +27 -3
- package/dist/gateway-DPPEQZT4.js +25 -0
- package/dist/grants-6JY4XZO7.js +27 -0
- package/dist/invoke-MRONRZ5Q.js +33 -0
- package/dist/moves-UAUCMWL5.js +36 -0
- package/dist/{order-U4KZLLSJ.js → order-RKLGT74L.js} +2 -2
- package/dist/{person-UHLVBVLR.js → person-YOZTFV4Q.js} +11 -10
- package/dist/plan-on-sale-H4F26YIN.js +26 -0
- package/dist/platform/migrations/0730_practitioners.sql +79 -0
- package/dist/platform/migrations/0750_routine_mail.sql +98 -0
- package/dist/platform/migrations/0770_clear_fixed_role_grants.sql +64 -0
- package/dist/platform/migrations/0810_recurrence_exceptions.sql +66 -0
- package/dist/platform/migrations/0811_staff_time.sql +109 -0
- package/dist/platform/migrations/0812_staff_offers.sql +112 -0
- package/dist/platform/migrations/0813_pay_periods.sql +241 -0
- package/dist/platform/migrations/0814_staff_feed.sql +47 -0
- package/dist/platform/migrations/0815_checklists.sql +237 -0
- package/dist/platform/migrations/0816_checklist_schedules.sql +121 -0
- package/dist/platform/migrations/0817_checklist_day_window.sql +35 -0
- package/dist/platform/migrations/0850_agreements.sql +271 -0
- package/dist/platform/migrations/0851_agreement_acceptances.sql +387 -0
- package/dist/platform/migrations/0853_agreement_enforcement.sql +58 -0
- package/dist/platform/migrations/0854_agreement_copy.sql +149 -0
- package/dist/platform/migrations/0855_agreement_retention.sql +279 -0
- package/dist/platform/migrations/0856_subscription_cooling_off.sql +159 -0
- package/dist/platform/migrations/0857_agreement_copy_guardian.sql +337 -0
- package/dist/platform/migrations/0858_agreement_desk.sql +175 -0
- package/dist/platform/migrations/0910_routine_messages.sql +99 -0
- package/dist/platform/migrations/0911_texts.sql +60 -0
- package/dist/platform/migrations/0913_contact_consent.sql +93 -0
- package/dist/platform/migrations/0916_contact_mailbox_index.sql +29 -0
- package/dist/platform/migrations/0930_stock_movements.sql +95 -0
- package/dist/platform/migrations/0931_stock_low.sql +25 -0
- package/dist/platform/migrations/0932_order_delivery.sql +78 -0
- package/dist/platform/migrations/0950_order_adjustments.sql +111 -0
- package/dist/platform/migrations/0951_order_price_guards.sql +33 -0
- package/dist/platform/migrations/0952_order_paid_another_way.sql +194 -0
- package/dist/platform/migrations/0953_discount_uses.sql +183 -0
- package/dist/platform/migrations/0955_folded_addresses.sql +92 -0
- package/dist/platform/migrations/0970_published_text.sql +59 -0
- package/dist/platform/migrations/0971_publish_acks.sql +68 -0
- package/dist/platform/migrations/0990_customer_email_change.sql +65 -0
- package/dist/platform/migrations/1010_intake_links.sql +148 -0
- package/dist/platform/migrations/1030_invoices.sql +747 -0
- package/dist/platform/migrations/1031_invoice_orders.sql +327 -0
- package/dist/platform/migrations/1050_inbox_conversations.sql +83 -0
- package/dist/platform/migrations/1090_staff_hours.sql +43 -0
- package/dist/platform/migrations/1091_staff_time_margins.sql +12 -0
- package/dist/platform/migrations/1092_client_guard.sql +26 -0
- package/dist/platform/migrations/1095_thing_time.sql +70 -0
- package/dist/platform/migrations/1130_devices.sql +162 -0
- package/dist/platform/migrations/1135_person_codes.sql +88 -0
- package/dist/platform/migrations/1150_finance_periods.sql +316 -0
- package/dist/platform/migrations/1151_payouts.sql +202 -0
- package/dist/platform/migrations/1153_finance_money_dates.sql +18 -0
- package/dist/platform/migrations/1170_journeys.sql +318 -0
- package/dist/platform/migrations/1171_person_birthday.sql +52 -0
- package/dist/platform/migrations/1175_person_signals.sql +29 -0
- package/dist/platform/migrations/1190_restricted_values.sql +25 -0
- package/dist/platform/migrations/1230_note_levels.sql +113 -0
- package/dist/platform/migrations/1231_person_carers.sql +33 -0
- package/dist/platform/migrations/1232_note_kinds.sql +122 -0
- package/dist/platform/migrations/1270_time_entries.sql +145 -0
- package/dist/platform/migrations/1271_leave.sql +249 -0
- package/dist/platform/migrations/1273_time_entry_reopened.sql +21 -0
- package/dist/platform/migrations/1330_accounting.sql +260 -0
- package/dist/platform/migrations/1350_customer_feed.sql +24 -0
- package/dist/platform/migrations/1390_locations.sql +77 -0
- package/dist/platform/migrations/1410_outside_copy.sql +213 -0
- package/dist/platform/migrations/1430_inbox_activities.sql +231 -0
- package/dist/platform/migrations/1450_wrong_guesses.sql +23 -0
- package/dist/platform/sdk/acknowledge.ts +100 -0
- package/dist/platform/sdk/agreements.ts +185 -0
- package/dist/platform/sdk/allowance.ts +98 -8
- package/dist/platform/sdk/automation-check.ts +120 -9
- package/dist/platform/sdk/automation.ts +78 -10
- package/dist/platform/sdk/behaviour.ts +26 -6
- package/dist/platform/sdk/calendar-entry.ts +258 -0
- package/dist/platform/sdk/capacity.ts +1 -2
- package/dist/platform/sdk/checklists.ts +118 -0
- package/dist/platform/sdk/contract.ts +489 -27
- package/dist/platform/sdk/discount.ts +408 -0
- package/dist/platform/sdk/document.ts +4 -2
- package/dist/platform/sdk/field-types.ts +5 -0
- package/dist/platform/sdk/formula/compile.ts +10 -0
- package/dist/platform/sdk/index.ts +17 -0
- package/dist/platform/sdk/intake.ts +380 -0
- package/dist/platform/sdk/journey-signals.ts +215 -0
- package/dist/platform/sdk/manifest.ts +1236 -65
- package/dist/platform/sdk/mine.ts +161 -0
- package/dist/platform/sdk/occupies.ts +192 -0
- package/dist/platform/sdk/questions.ts +681 -0
- package/dist/platform/sdk/recurring.ts +90 -9
- package/dist/platform/sdk/restricted-fields.ts +129 -0
- package/dist/platform/sdk/rich-text.ts +112 -0
- package/dist/platform/sdk/routine-mail.ts +1272 -0
- package/dist/platform/sdk/runtime.ts +25 -12
- package/dist/platform/sdk/screens.ts +91 -0
- package/dist/platform/sdk/sellable.ts +344 -0
- package/dist/platform/sdk/slug.ts +7 -2
- package/dist/platform/sdk/staffed.ts +474 -0
- package/dist/platform/sdk/states.ts +68 -26
- package/dist/platform/sdk/stock.ts +332 -0
- package/dist/platform/sdk/team.ts +83 -0
- package/dist/platform/sdk/testing.ts +70 -6
- package/dist/platform/sdk/worked-on.ts +134 -0
- package/dist/{products-2H5DR232.js → products-GCQLSZ3U.js} +2 -2
- package/dist/run-7ID64RA6.js +65 -0
- package/dist/run-FH7OGGTV.js +131 -0
- package/dist/run-OYZIAP6M.js +3363 -0
- package/dist/{run-ZCGFMEZF.js → run-QOFRRSZV.js} +41 -60
- package/dist/{run-PO3W5BFA.js → run-TJAEWR5U.js} +21 -51
- package/dist/{src-I3IJ4NKC.js → src-52VERUPK.js} +306 -4
- package/dist/src-PHRELWN5.js +40 -0
- package/dist/{visibility-TVXZ67FD.js → visibility-SZCINPP4.js} +21 -4
- package/package.json +2 -2
- package/dist/chunk-2TWV3SVB.js +0 -349
- package/dist/chunk-2XC5JLBO.js +0 -1931
- package/dist/chunk-324WJXNP.js +0 -320
- package/dist/chunk-6ELZRNUU.js +0 -116
- package/dist/chunk-6WGFA2IV.js +0 -643
- package/dist/chunk-7CQGMVDO.js +0 -63
- package/dist/chunk-7OGCMQXG.js +0 -284
- package/dist/chunk-FJIZGS5Q.js +0 -698
- package/dist/chunk-UB4I2QKR.js +0 -31575
- package/dist/chunk-WHKJGF36.js +0 -2418
- package/dist/chunk-YNUZRTPH.js +0 -182
- package/dist/chunk-ZZFWMVJO.js +0 -704
- package/dist/grants-LOW6TSWD.js +0 -32
- package/dist/invoke-6KVT6WJI.js +0 -41
- package/dist/moves-E2HKYCTK.js +0 -41
- package/dist/plan-on-sale-TJPFCNL7.js +0 -26
- package/dist/src-CRO35DYY.js +0 -46
|
@@ -0,0 +1,681 @@
|
|
|
1
|
+
// The question schema (stage 29, step 1): what a form asks, as a document field (ADR 0445) with
|
|
2
|
+
// one fixed schema the platform owns, as rich text is. A form's questions field is one:
|
|
3
|
+
//
|
|
4
|
+
// questions: { type: "document", label: "Questions", schema: QUESTIONS_SCHEMA,
|
|
5
|
+
// maxBytes: QUESTIONS_MAX_BYTES }
|
|
6
|
+
//
|
|
7
|
+
// The value is `{ items: [...] }`, at most 60 items told apart by `kind`, each with a stable `key`
|
|
8
|
+
// (camelCase, unique in the form). The questions: `text` (short, or `long` for several lines, with
|
|
9
|
+
// a maximum length), `email`, `phone`, `number` (min, max, whole), `choice` (single or `multiple`,
|
|
10
|
+
// up to 50 options), `date`, `rating` (`stars`, 1 to 5, or `ten`, 0 to 10), `yesNo`, and `consent`
|
|
11
|
+
// (a tick box: its exact `words`, its `purpose`: contact, marketing or other). The non-questions:
|
|
12
|
+
// `heading` and `paragraph` (rich text's inline runs). There is no health kind, no signature and no
|
|
13
|
+
// payment: those are Agreements' (stage 44) and orders'. A `file` kind is added with the trades work.
|
|
14
|
+
//
|
|
15
|
+
// Any item may be shown only if one earlier `choice` or `yesNo` has a value (`showIf`), one level
|
|
16
|
+
// deep: the item it names has no `showIf` of its own. A question may `map` to the submitter's Person
|
|
17
|
+
// (name from a short text, email from an email, phone from a phone) or to a field of the form's
|
|
18
|
+
// destination object; each target is filled from one question at most. A marketing consent line is
|
|
19
|
+
// never required (consent is not a condition of being heard).
|
|
20
|
+
//
|
|
21
|
+
// Two checks. `checkQuestions` is the document check plus what a schema cannot say (keys, earlier
|
|
22
|
+
// items, maps, options). `checkAnswers` checks what a person sent against one version's questions
|
|
23
|
+
// and gives the answers document a submission stores (`ANSWERS_SCHEMA`), in the questions' order.
|
|
24
|
+
// Its faults name the question's key and never the value, so they can go in an error or an audit
|
|
25
|
+
// row. It never repairs a value: a client trims and converts before it sends.
|
|
26
|
+
import { z } from "zod";
|
|
27
|
+
import {
|
|
28
|
+
DOCUMENT_MAX_ISSUES,
|
|
29
|
+
type DocumentIssue,
|
|
30
|
+
type DocumentNode,
|
|
31
|
+
type DocumentObjectNode,
|
|
32
|
+
type DocumentSchema,
|
|
33
|
+
documentBytes,
|
|
34
|
+
validateDocument,
|
|
35
|
+
} from "./document.ts";
|
|
36
|
+
import { RICH_TEXT_SCHEMA } from "./rich-text.ts";
|
|
37
|
+
|
|
38
|
+
/** The kinds of item, in the order a builder lists them. */
|
|
39
|
+
export const QUESTION_KINDS = [
|
|
40
|
+
"text",
|
|
41
|
+
"email",
|
|
42
|
+
"phone",
|
|
43
|
+
"number",
|
|
44
|
+
"choice",
|
|
45
|
+
"date",
|
|
46
|
+
"rating",
|
|
47
|
+
"yesNo",
|
|
48
|
+
"consent",
|
|
49
|
+
"heading",
|
|
50
|
+
"paragraph",
|
|
51
|
+
] as const;
|
|
52
|
+
export type QuestionKind = (typeof QUESTION_KINDS)[number];
|
|
53
|
+
|
|
54
|
+
/** The kinds that ask nothing: no answer is given to them. */
|
|
55
|
+
export const QUESTION_DISPLAY_KINDS = ["heading", "paragraph"] as const;
|
|
56
|
+
|
|
57
|
+
/** What a consent line is for. Only `marketing` lines are "keep me updated". */
|
|
58
|
+
export const CONSENT_PURPOSES = ["contact", "marketing", "other"] as const;
|
|
59
|
+
export type ConsentPurpose = (typeof CONSENT_PURPOSES)[number];
|
|
60
|
+
|
|
61
|
+
/** The Person's fields a question may fill (only where empty: the host's rule, ADR 0721). */
|
|
62
|
+
export const QUESTION_PERSON_FIELDS = ["name", "email", "phone"] as const;
|
|
63
|
+
export type QuestionPersonField = (typeof QUESTION_PERSON_FIELDS)[number];
|
|
64
|
+
|
|
65
|
+
/** Items in a form, at most. */
|
|
66
|
+
export const QUESTIONS_MAX_ITEMS = 60;
|
|
67
|
+
/** Options of a choice, at most. */
|
|
68
|
+
export const QUESTION_MAX_OPTIONS = 50;
|
|
69
|
+
/** The most a questions value may be, in bytes of its JSON. */
|
|
70
|
+
export const QUESTIONS_MAX_BYTES = 262_144;
|
|
71
|
+
/** A short text's longest answer, and its default. */
|
|
72
|
+
export const SHORT_TEXT_MAX = 500;
|
|
73
|
+
export const SHORT_TEXT_DEFAULT = 200;
|
|
74
|
+
/** A long text's longest answer, and its default. */
|
|
75
|
+
export const LONG_TEXT_MAX = 5000;
|
|
76
|
+
/** Links in any one text answer, at most. */
|
|
77
|
+
export const ANSWER_MAX_LINKS = 3;
|
|
78
|
+
/** The most a submission's answers may be, in bytes of their JSON. */
|
|
79
|
+
export const ANSWERS_MAX_BYTES = 20_480;
|
|
80
|
+
|
|
81
|
+
const KEY_RE = /^[a-z][a-zA-Z0-9]{0,39}$/;
|
|
82
|
+
// Control characters other than tab and newline (and a lone carriage return) are refused.
|
|
83
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what it finds.
|
|
84
|
+
const CONTROL_RE = /[\u0000-\u0008\u000b-\u001f\u007f]/;
|
|
85
|
+
// What counts as a link in an answer: an address with a scheme, or one starting `www.`.
|
|
86
|
+
const LINK_RE = /\b(?:[a-z][a-z0-9+.-]*:\/\/|www\.)/gi;
|
|
87
|
+
const PHONE_RE = /^\+?[0-9 ().-]{3,40}$/;
|
|
88
|
+
const DATE_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
89
|
+
const emailShape = z.email().max(320);
|
|
90
|
+
|
|
91
|
+
// --- The schema ----------------------------------------------------------------------------------
|
|
92
|
+
|
|
93
|
+
const KEY_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
94
|
+
const keyNode: DocumentNode = {
|
|
95
|
+
type: "text",
|
|
96
|
+
min: 1,
|
|
97
|
+
max: 40,
|
|
98
|
+
chars: KEY_CHARS,
|
|
99
|
+
description: "The item's stable key in the form (camelCase): answers are kept by it",
|
|
100
|
+
};
|
|
101
|
+
const prose = (max: number, extra: Partial<DocumentNode> = {}): DocumentNode =>
|
|
102
|
+
({ type: "text", max, noMarkup: true, ...extra }) as DocumentNode;
|
|
103
|
+
const optional = (node: DocumentNode): DocumentNode => ({ ...node, optional: true });
|
|
104
|
+
const flag = (description: string): DocumentNode => ({
|
|
105
|
+
type: "boolean",
|
|
106
|
+
optional: true,
|
|
107
|
+
description,
|
|
108
|
+
});
|
|
109
|
+
const object = (fields: Record<string, DocumentNode>): DocumentObjectNode => ({
|
|
110
|
+
type: "object",
|
|
111
|
+
fields,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/** What every item has: its key and the one condition that shows it. */
|
|
115
|
+
const shown = { key: keyNode, showIf: optional({ type: "use", def: "showIf" }) };
|
|
116
|
+
/** What every question has besides: its label, help, whether it is required and where it lands. */
|
|
117
|
+
const asked = (mapped: boolean) => ({
|
|
118
|
+
...shown,
|
|
119
|
+
label: prose(300, { min: 1, description: "The question, as the person reads it" }),
|
|
120
|
+
help: optional(prose(500, { multiline: true, description: "A line under the question" })),
|
|
121
|
+
required: flag("The form is not sent without an answer (when the question is shown)"),
|
|
122
|
+
...(mapped ? { map: optional({ type: "use", def: "map" }) } : {}),
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const items: Record<QuestionKind, DocumentObjectNode> = {
|
|
126
|
+
text: object({
|
|
127
|
+
...asked(true),
|
|
128
|
+
long: flag("Several lines (up to 5,000 characters) rather than one (up to 500)"),
|
|
129
|
+
max: optional({ type: "number", integer: true, min: 1, max: LONG_TEXT_MAX }),
|
|
130
|
+
}),
|
|
131
|
+
email: object(asked(true)),
|
|
132
|
+
phone: object(asked(true)),
|
|
133
|
+
number: object({
|
|
134
|
+
...asked(true),
|
|
135
|
+
min: optional({ type: "number" }),
|
|
136
|
+
max: optional({ type: "number" }),
|
|
137
|
+
integer: flag("Whole numbers only"),
|
|
138
|
+
}),
|
|
139
|
+
choice: object({
|
|
140
|
+
...asked(true),
|
|
141
|
+
options: {
|
|
142
|
+
type: "array",
|
|
143
|
+
min: 1,
|
|
144
|
+
max: QUESTION_MAX_OPTIONS,
|
|
145
|
+
of: prose(200, { min: 1 }),
|
|
146
|
+
description: "What the person chooses from; an answer is one of these words",
|
|
147
|
+
},
|
|
148
|
+
multiple: flag("Several options may be chosen"),
|
|
149
|
+
}),
|
|
150
|
+
date: object(asked(true)),
|
|
151
|
+
rating: object({
|
|
152
|
+
...asked(true),
|
|
153
|
+
scale: {
|
|
154
|
+
type: "choice",
|
|
155
|
+
options: ["stars", "ten"],
|
|
156
|
+
description: "stars: 1 to 5; ten: 0 to 10",
|
|
157
|
+
},
|
|
158
|
+
}),
|
|
159
|
+
yesNo: object(asked(true)),
|
|
160
|
+
consent: object({
|
|
161
|
+
...shown,
|
|
162
|
+
words: prose(1000, {
|
|
163
|
+
min: 1,
|
|
164
|
+
multiline: true,
|
|
165
|
+
description: "The exact words beside the tick box, kept with every answer to this version",
|
|
166
|
+
}),
|
|
167
|
+
purpose: { type: "choice", options: CONSENT_PURPOSES },
|
|
168
|
+
required: flag("The form is not sent unless it is ticked (never for marketing)"),
|
|
169
|
+
}),
|
|
170
|
+
heading: object({ ...shown, text: prose(200, { min: 1 }) }),
|
|
171
|
+
paragraph: object({ ...shown, content: { type: "use", def: "inlines" } }),
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/** The one schema of every form's questions. */
|
|
175
|
+
export const QUESTIONS_SCHEMA: DocumentSchema = {
|
|
176
|
+
root: object({
|
|
177
|
+
items: {
|
|
178
|
+
type: "array",
|
|
179
|
+
max: QUESTIONS_MAX_ITEMS,
|
|
180
|
+
unique: "key",
|
|
181
|
+
of: { type: "union", tag: "kind", variants: items },
|
|
182
|
+
},
|
|
183
|
+
}),
|
|
184
|
+
defs: {
|
|
185
|
+
// Rich text's inline runs, so a paragraph is drawn as any rich text is.
|
|
186
|
+
inlines: (RICH_TEXT_SCHEMA.defs as Record<string, DocumentNode>).inlines as DocumentNode,
|
|
187
|
+
showIf: object({
|
|
188
|
+
question: { ...keyNode, description: "The key of an earlier choice or yes/no" },
|
|
189
|
+
is: prose(200, { min: 1, description: "One of its options, or yes or no" }),
|
|
190
|
+
}),
|
|
191
|
+
map: {
|
|
192
|
+
type: "union",
|
|
193
|
+
tag: "to",
|
|
194
|
+
variants: {
|
|
195
|
+
person: object({ field: { type: "choice", options: QUESTION_PERSON_FIELDS } }),
|
|
196
|
+
destination: object({
|
|
197
|
+
field: { ...keyNode, description: "A field the destination's intake lists" },
|
|
198
|
+
}),
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* What a submission stores: the answers, in the questions' order, each with its key and kind.
|
|
206
|
+
* Private: a customer or the public never reads it, not even their own.
|
|
207
|
+
*/
|
|
208
|
+
export const ANSWERS_SCHEMA: DocumentSchema = {
|
|
209
|
+
root: {
|
|
210
|
+
type: "object",
|
|
211
|
+
private: true,
|
|
212
|
+
fields: {
|
|
213
|
+
answers: {
|
|
214
|
+
type: "array",
|
|
215
|
+
max: QUESTIONS_MAX_ITEMS,
|
|
216
|
+
unique: "key",
|
|
217
|
+
of: {
|
|
218
|
+
type: "union",
|
|
219
|
+
tag: "kind",
|
|
220
|
+
variants: {
|
|
221
|
+
text: object({
|
|
222
|
+
key: keyNode,
|
|
223
|
+
value: { type: "text", max: LONG_TEXT_MAX, multiline: true },
|
|
224
|
+
}),
|
|
225
|
+
email: object({ key: keyNode, value: { type: "text", max: 320 } }),
|
|
226
|
+
phone: object({
|
|
227
|
+
key: keyNode,
|
|
228
|
+
value: { type: "text", max: 40, chars: "+0123456789 ().-" },
|
|
229
|
+
}),
|
|
230
|
+
number: object({ key: keyNode, value: { type: "number" } }),
|
|
231
|
+
choice: object({
|
|
232
|
+
key: keyNode,
|
|
233
|
+
values: {
|
|
234
|
+
type: "array",
|
|
235
|
+
min: 1,
|
|
236
|
+
max: QUESTION_MAX_OPTIONS,
|
|
237
|
+
of: { type: "text", max: 200 },
|
|
238
|
+
},
|
|
239
|
+
}),
|
|
240
|
+
date: object({
|
|
241
|
+
key: keyNode,
|
|
242
|
+
value: { type: "text", min: 10, max: 10, chars: "0123456789-" },
|
|
243
|
+
}),
|
|
244
|
+
rating: object({
|
|
245
|
+
key: keyNode,
|
|
246
|
+
value: { type: "number", integer: true, min: 0, max: 10 },
|
|
247
|
+
}),
|
|
248
|
+
yesNo: object({ key: keyNode, value: { type: "boolean" } }),
|
|
249
|
+
consent: object({ key: keyNode, value: { type: "boolean" } }),
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
// --- The types -----------------------------------------------------------------------------------
|
|
258
|
+
|
|
259
|
+
export interface QuestionShowIf {
|
|
260
|
+
question: string;
|
|
261
|
+
is: string;
|
|
262
|
+
}
|
|
263
|
+
export type QuestionMap =
|
|
264
|
+
| { to: "person"; field: QuestionPersonField }
|
|
265
|
+
| { to: "destination"; field: string };
|
|
266
|
+
|
|
267
|
+
interface ItemBase {
|
|
268
|
+
key: string;
|
|
269
|
+
showIf?: QuestionShowIf;
|
|
270
|
+
}
|
|
271
|
+
interface AskedBase extends ItemBase {
|
|
272
|
+
label: string;
|
|
273
|
+
help?: string;
|
|
274
|
+
required?: boolean;
|
|
275
|
+
map?: QuestionMap;
|
|
276
|
+
}
|
|
277
|
+
export interface TextQuestion extends AskedBase {
|
|
278
|
+
kind: "text";
|
|
279
|
+
long?: boolean;
|
|
280
|
+
max?: number;
|
|
281
|
+
}
|
|
282
|
+
export interface EmailQuestion extends AskedBase {
|
|
283
|
+
kind: "email";
|
|
284
|
+
}
|
|
285
|
+
export interface PhoneQuestion extends AskedBase {
|
|
286
|
+
kind: "phone";
|
|
287
|
+
}
|
|
288
|
+
export interface NumberQuestion extends AskedBase {
|
|
289
|
+
kind: "number";
|
|
290
|
+
min?: number;
|
|
291
|
+
max?: number;
|
|
292
|
+
integer?: boolean;
|
|
293
|
+
}
|
|
294
|
+
export interface ChoiceQuestion extends AskedBase {
|
|
295
|
+
kind: "choice";
|
|
296
|
+
options: string[];
|
|
297
|
+
multiple?: boolean;
|
|
298
|
+
}
|
|
299
|
+
export interface DateQuestion extends AskedBase {
|
|
300
|
+
kind: "date";
|
|
301
|
+
}
|
|
302
|
+
export interface RatingQuestion extends AskedBase {
|
|
303
|
+
kind: "rating";
|
|
304
|
+
scale: "stars" | "ten";
|
|
305
|
+
}
|
|
306
|
+
export interface YesNoQuestion extends AskedBase {
|
|
307
|
+
kind: "yesNo";
|
|
308
|
+
}
|
|
309
|
+
export interface ConsentQuestion extends ItemBase {
|
|
310
|
+
kind: "consent";
|
|
311
|
+
words: string;
|
|
312
|
+
purpose: ConsentPurpose;
|
|
313
|
+
required?: boolean;
|
|
314
|
+
}
|
|
315
|
+
export interface HeadingItem extends ItemBase {
|
|
316
|
+
kind: "heading";
|
|
317
|
+
text: string;
|
|
318
|
+
}
|
|
319
|
+
export interface ParagraphItem extends ItemBase {
|
|
320
|
+
kind: "paragraph";
|
|
321
|
+
/** Rich text's inline runs (ADR 0440). */
|
|
322
|
+
content: unknown[];
|
|
323
|
+
}
|
|
324
|
+
export type QuestionItem =
|
|
325
|
+
| TextQuestion
|
|
326
|
+
| EmailQuestion
|
|
327
|
+
| PhoneQuestion
|
|
328
|
+
| NumberQuestion
|
|
329
|
+
| ChoiceQuestion
|
|
330
|
+
| DateQuestion
|
|
331
|
+
| RatingQuestion
|
|
332
|
+
| YesNoQuestion
|
|
333
|
+
| ConsentQuestion
|
|
334
|
+
| HeadingItem
|
|
335
|
+
| ParagraphItem;
|
|
336
|
+
|
|
337
|
+
/** A form's questions field. */
|
|
338
|
+
export interface Questions {
|
|
339
|
+
items: QuestionItem[];
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export type Answer =
|
|
343
|
+
| { kind: "text" | "email" | "phone" | "date"; key: string; value: string }
|
|
344
|
+
| { kind: "number" | "rating"; key: string; value: number }
|
|
345
|
+
| { kind: "yesNo" | "consent"; key: string; value: boolean }
|
|
346
|
+
| { kind: "choice"; key: string; values: string[] };
|
|
347
|
+
|
|
348
|
+
/** A submission's answers field. */
|
|
349
|
+
export interface Answers {
|
|
350
|
+
answers: Answer[];
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// --- Checking the questions ----------------------------------------------------------------------
|
|
354
|
+
|
|
355
|
+
export interface CheckQuestionsOptions {
|
|
356
|
+
/** An anonymous form (ADR 0725): nothing maps to the Person and there is no destination. */
|
|
357
|
+
anonymous?: boolean;
|
|
358
|
+
/** The fields the form's destination object's intake lists; left out, the form has none. */
|
|
359
|
+
destinationFields?: readonly string[];
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export type QuestionsResult =
|
|
363
|
+
| { ok: true; questions: Questions; bytes: number }
|
|
364
|
+
| { ok: false; issues: DocumentIssue[] };
|
|
365
|
+
|
|
366
|
+
/** The text answers a person map takes from each kind. */
|
|
367
|
+
const PERSON_MAP_KINDS: Record<QuestionPersonField, QuestionKind> = {
|
|
368
|
+
name: "text",
|
|
369
|
+
email: "email",
|
|
370
|
+
phone: "phone",
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Checks a form's questions: the document schema, then what it cannot say. Every fault (up to the
|
|
375
|
+
* document check's limit) with its path from the root (`items.3.showIf.question`).
|
|
376
|
+
*/
|
|
377
|
+
export function checkQuestions(
|
|
378
|
+
value: unknown,
|
|
379
|
+
options: CheckQuestionsOptions = {},
|
|
380
|
+
): QuestionsResult {
|
|
381
|
+
const checked = validateDocument(QUESTIONS_SCHEMA, value, { maxBytes: QUESTIONS_MAX_BYTES });
|
|
382
|
+
if (!checked.ok) return checked;
|
|
383
|
+
const questions = value as Questions;
|
|
384
|
+
const issues: DocumentIssue[] = [];
|
|
385
|
+
const fault = (path: (string | number)[], message: string) => {
|
|
386
|
+
if (issues.length < DOCUMENT_MAX_ISSUES) issues.push({ path, message });
|
|
387
|
+
};
|
|
388
|
+
const before = new Map<string, QuestionItem>();
|
|
389
|
+
const mapped = new Map<string, string>();
|
|
390
|
+
questions.items.forEach((item, i) => {
|
|
391
|
+
const at = ["items", i];
|
|
392
|
+
if (!KEY_RE.test(item.key))
|
|
393
|
+
fault([...at, "key"], "A key is camelCase: a lowercase letter first");
|
|
394
|
+
if (item.showIf) {
|
|
395
|
+
const target = before.get(item.showIf.question);
|
|
396
|
+
const why = target ? showIfFault(target, item.showIf.is) : "names no earlier item";
|
|
397
|
+
if (why) fault([...at, "showIf"], `"Show only if" ${why}`);
|
|
398
|
+
}
|
|
399
|
+
switch (item.kind) {
|
|
400
|
+
case "text":
|
|
401
|
+
if (item.max !== undefined && !item.long && item.max > SHORT_TEXT_MAX) {
|
|
402
|
+
fault([...at, "max"], `A short text takes at most ${SHORT_TEXT_MAX} characters`);
|
|
403
|
+
}
|
|
404
|
+
break;
|
|
405
|
+
case "number":
|
|
406
|
+
if (item.min !== undefined && item.max !== undefined && item.min > item.max) {
|
|
407
|
+
fault([...at, "min"], "min is above max");
|
|
408
|
+
}
|
|
409
|
+
break;
|
|
410
|
+
case "choice":
|
|
411
|
+
if (new Set(item.options).size !== item.options.length) {
|
|
412
|
+
fault([...at, "options"], "Each option is listed once");
|
|
413
|
+
}
|
|
414
|
+
break;
|
|
415
|
+
case "consent":
|
|
416
|
+
if (item.purpose === "marketing" && item.required) {
|
|
417
|
+
fault([...at, "required"], "A marketing consent line is never required");
|
|
418
|
+
}
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
const map = "map" in item ? item.map : undefined;
|
|
422
|
+
if (map) {
|
|
423
|
+
const why = mapFault(item, map, options);
|
|
424
|
+
const target = `${map.to}.${map.field}`;
|
|
425
|
+
if (why) fault([...at, "map"], why);
|
|
426
|
+
else if (mapped.has(target))
|
|
427
|
+
fault([...at, "map"], `${target} is filled by ${mapped.get(target)} already`);
|
|
428
|
+
else mapped.set(target, item.key);
|
|
429
|
+
}
|
|
430
|
+
before.set(item.key, item);
|
|
431
|
+
});
|
|
432
|
+
if (issues.length) return { ok: false, issues };
|
|
433
|
+
return { ok: true, questions, bytes: checked.bytes };
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function showIfFault(target: QuestionItem, is: string): string | null {
|
|
437
|
+
if (target.showIf) return "names an item that is itself shown only if (one level only)";
|
|
438
|
+
if (target.kind === "yesNo")
|
|
439
|
+
return is === "yes" || is === "no" ? null : "of a yes/no is yes or no";
|
|
440
|
+
if (target.kind === "choice") {
|
|
441
|
+
return target.options.includes(is) ? null : `names no option of ${target.key}`;
|
|
442
|
+
}
|
|
443
|
+
return "names a choice or a yes/no";
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function mapFault(
|
|
447
|
+
item: QuestionItem,
|
|
448
|
+
map: QuestionMap,
|
|
449
|
+
options: CheckQuestionsOptions,
|
|
450
|
+
): string | null {
|
|
451
|
+
if (map.to === "person") {
|
|
452
|
+
if (options.anonymous) return "An anonymous form keeps no Person, so nothing maps to one";
|
|
453
|
+
const kind = PERSON_MAP_KINDS[map.field];
|
|
454
|
+
if (item.kind !== kind || (item.kind === "text" && item.long)) {
|
|
455
|
+
return `The Person's ${map.field} is filled from ${kind === "text" ? "a short text" : `an ${kind}`} question`;
|
|
456
|
+
}
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
if (options.anonymous) return "An anonymous form has no destination";
|
|
460
|
+
if (!options.destinationFields) return "The form has no destination";
|
|
461
|
+
if (!options.destinationFields.includes(map.field)) {
|
|
462
|
+
return `The destination's intake does not list "${map.field}"`;
|
|
463
|
+
}
|
|
464
|
+
if (item.kind === "choice" && item.multiple)
|
|
465
|
+
return "A choice of several fills no destination field";
|
|
466
|
+
return null;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// --- Checking the answers ------------------------------------------------------------------------
|
|
470
|
+
|
|
471
|
+
/** What is wrong with an answer, for a client to show beside its question. */
|
|
472
|
+
export type AnswerFault =
|
|
473
|
+
| "shape" // the answers are not an object of key to value
|
|
474
|
+
| "unknown" // the key is no question of this version
|
|
475
|
+
| "hidden" // the question was not shown, by its "show only if"
|
|
476
|
+
| "required"
|
|
477
|
+
| "type"
|
|
478
|
+
| "length"
|
|
479
|
+
| "links"
|
|
480
|
+
| "size"; // the answers together are too big
|
|
481
|
+
|
|
482
|
+
/** A fault in the answers: the question's key (null for the whole) and never the value. */
|
|
483
|
+
export interface AnswerIssue {
|
|
484
|
+
key: string | null;
|
|
485
|
+
fault: AnswerFault;
|
|
486
|
+
message: string;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
export type AnswersResult =
|
|
490
|
+
| { ok: true; answers: Answers; bytes: number }
|
|
491
|
+
| { ok: false; issues: AnswerIssue[] };
|
|
492
|
+
|
|
493
|
+
/** Whether a value counts as no answer: left out, null, empty text or no option chosen. */
|
|
494
|
+
function unanswered(value: unknown): boolean {
|
|
495
|
+
return (
|
|
496
|
+
value === undefined || value === null || value === "" || (Array.isArray(value) && !value.length)
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Whether an item is shown, given the answers so far: it has no "show only if", or the earlier
|
|
502
|
+
* item it names has that value (a choice of several: among those chosen). The headless entry and
|
|
503
|
+
* `checkAnswers` both read it.
|
|
504
|
+
*/
|
|
505
|
+
export function isQuestionShown(
|
|
506
|
+
item: QuestionItem,
|
|
507
|
+
questions: Questions,
|
|
508
|
+
input: Readonly<Record<string, unknown>>,
|
|
509
|
+
): boolean {
|
|
510
|
+
if (!item.showIf) return true;
|
|
511
|
+
const { question, is } = item.showIf;
|
|
512
|
+
const target = questions.items.find((q) => q.key === question);
|
|
513
|
+
const value = Object.hasOwn(input, question) ? input[question] : undefined;
|
|
514
|
+
if (target?.kind === "yesNo") return value === (is === "yes");
|
|
515
|
+
if (target?.kind === "choice") return Array.isArray(value) ? value.includes(is) : value === is;
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** Links in a text answer. */
|
|
520
|
+
export function countLinks(text: string): number {
|
|
521
|
+
return text.match(LINK_RE)?.length ?? 0;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Checks what a person sent (`{ [key]: value }`) against one version's questions, already checked
|
|
526
|
+
* by `checkQuestions`: every fault, or the answers document to store. An item that is not shown
|
|
527
|
+
* takes no answer; a question left unanswered is not stored; a consent line left unticked may be
|
|
528
|
+
* sent as false.
|
|
529
|
+
*/
|
|
530
|
+
export function checkAnswers(questions: Questions, input: unknown): AnswersResult {
|
|
531
|
+
const isRecord = typeof input === "object" && input !== null && !Array.isArray(input);
|
|
532
|
+
if (!isRecord) {
|
|
533
|
+
return {
|
|
534
|
+
ok: false,
|
|
535
|
+
issues: [{ key: null, fault: "shape", message: "Answers are an object of question keys" }],
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
const given = input as Record<string, unknown>;
|
|
539
|
+
const issues: AnswerIssue[] = [];
|
|
540
|
+
const fault = (key: string | null, kind: AnswerFault, message: string) => {
|
|
541
|
+
if (issues.length < DOCUMENT_MAX_ISSUES) issues.push({ key, fault: kind, message });
|
|
542
|
+
};
|
|
543
|
+
const byKey = new Map(questions.items.map((item) => [item.key, item]));
|
|
544
|
+
for (const key of Object.keys(given)) {
|
|
545
|
+
const item = byKey.get(key);
|
|
546
|
+
if (!item || item.kind === "heading" || item.kind === "paragraph") {
|
|
547
|
+
fault(null, "unknown", "An answer names no question of this form");
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
const answers: Answer[] = [];
|
|
551
|
+
for (const item of questions.items) {
|
|
552
|
+
if (item.kind === "heading" || item.kind === "paragraph") continue;
|
|
553
|
+
const value = Object.hasOwn(given, item.key) ? given[item.key] : undefined;
|
|
554
|
+
if (!isQuestionShown(item, questions, given)) {
|
|
555
|
+
if (!unanswered(value)) fault(item.key, "hidden", "This question was not shown");
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
if (unanswered(value)) {
|
|
559
|
+
if (item.required) fault(item.key, "required", "An answer is required");
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
const answer = checkAnswer(item, value, (kind, message) => {
|
|
563
|
+
fault(item.key, kind, message);
|
|
564
|
+
return null;
|
|
565
|
+
});
|
|
566
|
+
if (answer) answers.push(answer);
|
|
567
|
+
}
|
|
568
|
+
if (issues.length) return { ok: false, issues };
|
|
569
|
+
const document: Answers = { answers };
|
|
570
|
+
const bytes = documentBytes(document);
|
|
571
|
+
if (bytes > ANSWERS_MAX_BYTES) {
|
|
572
|
+
return {
|
|
573
|
+
ok: false,
|
|
574
|
+
issues: [
|
|
575
|
+
{
|
|
576
|
+
key: null,
|
|
577
|
+
fault: "size",
|
|
578
|
+
message: `The answers are more than ${ANSWERS_MAX_BYTES} bytes`,
|
|
579
|
+
},
|
|
580
|
+
],
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
// The stored shape is the platform's: a fault here is a fault in this file.
|
|
584
|
+
const stored = validateDocument(ANSWERS_SCHEMA, document, { maxBytes: ANSWERS_MAX_BYTES });
|
|
585
|
+
if (!stored.ok) throw new Error("checkAnswers built answers its own schema refuses");
|
|
586
|
+
return { ok: true, answers: document, bytes };
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
function checkAnswer(
|
|
590
|
+
item: Exclude<QuestionItem, HeadingItem | ParagraphItem>,
|
|
591
|
+
value: unknown,
|
|
592
|
+
fault: (kind: AnswerFault, message: string) => null,
|
|
593
|
+
): Answer | null {
|
|
594
|
+
const { key } = item;
|
|
595
|
+
const text = (max: number, multiline: boolean): string | null => {
|
|
596
|
+
if (typeof value !== "string") return fault("type", "Expected text");
|
|
597
|
+
if (value.length > max) return fault("length", `At most ${max} characters`);
|
|
598
|
+
if (CONTROL_RE.test(value)) return fault("type", "Control characters are not allowed");
|
|
599
|
+
if (!multiline && /[\r\n]/.test(value)) return fault("type", "One line only");
|
|
600
|
+
if (countLinks(value) > ANSWER_MAX_LINKS) {
|
|
601
|
+
return fault("links", `At most ${ANSWER_MAX_LINKS} links`);
|
|
602
|
+
}
|
|
603
|
+
return value;
|
|
604
|
+
};
|
|
605
|
+
switch (item.kind) {
|
|
606
|
+
case "text": {
|
|
607
|
+
const max = Math.min(
|
|
608
|
+
item.max ?? (item.long ? LONG_TEXT_MAX : SHORT_TEXT_DEFAULT),
|
|
609
|
+
item.long ? LONG_TEXT_MAX : SHORT_TEXT_MAX,
|
|
610
|
+
);
|
|
611
|
+
const v = text(max, item.long === true);
|
|
612
|
+
return v === null ? null : { kind: "text", key, value: v };
|
|
613
|
+
}
|
|
614
|
+
case "email": {
|
|
615
|
+
if (typeof value !== "string" || !emailShape.safeParse(value).success) {
|
|
616
|
+
return fault("type", "Expected an email address");
|
|
617
|
+
}
|
|
618
|
+
return { kind: "email", key, value };
|
|
619
|
+
}
|
|
620
|
+
case "phone": {
|
|
621
|
+
if (
|
|
622
|
+
typeof value !== "string" ||
|
|
623
|
+
!PHONE_RE.test(value) ||
|
|
624
|
+
(value.match(/[0-9]/g)?.length ?? 0) < 5
|
|
625
|
+
) {
|
|
626
|
+
return fault("type", "Expected a phone number");
|
|
627
|
+
}
|
|
628
|
+
return { kind: "phone", key, value };
|
|
629
|
+
}
|
|
630
|
+
case "number": {
|
|
631
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
632
|
+
return fault("type", "Expected a number");
|
|
633
|
+
if (item.integer && !Number.isInteger(value)) return fault("type", "Expected a whole number");
|
|
634
|
+
if (item.min !== undefined && value < item.min) return fault("type", `At least ${item.min}`);
|
|
635
|
+
if (item.max !== undefined && value > item.max) return fault("type", `At most ${item.max}`);
|
|
636
|
+
return { kind: "number", key, value };
|
|
637
|
+
}
|
|
638
|
+
case "choice": {
|
|
639
|
+
const values = item.multiple ? value : [value];
|
|
640
|
+
if (
|
|
641
|
+
!Array.isArray(values) ||
|
|
642
|
+
values.some((v) => typeof v !== "string" || !item.options.includes(v)) ||
|
|
643
|
+
new Set(values).size !== values.length
|
|
644
|
+
) {
|
|
645
|
+
return fault(
|
|
646
|
+
"type",
|
|
647
|
+
item.multiple ? "Expected some of the options, each once" : "Expected one of the options",
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
return { kind: "choice", key, values: values as string[] };
|
|
651
|
+
}
|
|
652
|
+
case "date": {
|
|
653
|
+
const match = typeof value === "string" ? DATE_RE.exec(value) : null;
|
|
654
|
+
const [y, m, d] = match ? [Number(match[1]), Number(match[2]), Number(match[3])] : [0, 0, 0];
|
|
655
|
+
const day = new Date(Date.UTC(y, m - 1, d));
|
|
656
|
+
if (
|
|
657
|
+
!match ||
|
|
658
|
+
day.getUTCFullYear() !== y ||
|
|
659
|
+
day.getUTCMonth() !== m - 1 ||
|
|
660
|
+
day.getUTCDate() !== d
|
|
661
|
+
) {
|
|
662
|
+
return fault("type", "Expected a date (YYYY-MM-DD)");
|
|
663
|
+
}
|
|
664
|
+
return { kind: "date", key, value: value as string };
|
|
665
|
+
}
|
|
666
|
+
case "rating": {
|
|
667
|
+
const [min, max] = item.scale === "stars" ? [1, 5] : [0, 10];
|
|
668
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < min || value > max) {
|
|
669
|
+
return fault("type", `Expected a whole number from ${min} to ${max}`);
|
|
670
|
+
}
|
|
671
|
+
return { kind: "rating", key, value };
|
|
672
|
+
}
|
|
673
|
+
case "yesNo":
|
|
674
|
+
case "consent": {
|
|
675
|
+
if (typeof value !== "boolean") return fault("type", "Expected true or false");
|
|
676
|
+
if (item.kind === "consent" && item.required && !value)
|
|
677
|
+
return fault("required", "This must be ticked");
|
|
678
|
+
return { kind: item.kind, key, value };
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|