@jini-ai/cms-forms 0.0.0-stage → 0.2.1
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/CHANGELOG.md +40 -0
- package/LICENSE +201 -0
- package/README.md +43 -2
- package/dist/errors.d.ts +53 -0
- package/dist/errors.js +25 -0
- package/dist/forms.d.ts +59 -0
- package/dist/forms.js +203 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/notify-subscriber.d.ts +15 -0
- package/dist/notify-subscriber.js +60 -0
- package/dist/ports.d.ts +111 -0
- package/dist/ports.js +2 -0
- package/dist/rate-limit-key.d.ts +8 -0
- package/dist/rate-limit-key.js +7 -0
- package/dist/submit-service.d.ts +28 -0
- package/dist/submit-service.js +62 -0
- package/dist/types.d.ts +46 -0
- package/dist/types.js +2 -0
- package/dist/write-service.d.ts +78 -0
- package/dist/write-service.js +213 -0
- package/package.json +54 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.1 — 2026-10-04
|
|
4
|
+
|
|
5
|
+
- `@jini-ai/cms` and `@jini-ai/core` are caret ranges (`^0.4.1`, `^0.4.0`) instead of exact pins, so a host on a newer patch of either resolves a single copy without an override.
|
|
6
|
+
|
|
7
|
+
## 0.2.0 — 2026-10-02
|
|
8
|
+
|
|
9
|
+
### BREAKING
|
|
10
|
+
|
|
11
|
+
- Logging uses the canonical core Logger; public calls retain separate required and optional argument objects. The package remains private.
|
|
12
|
+
- Distribution includes runtime output, release documentation and required assets only. Process records and per-job neutrality checks are no longer part of the package surface.
|
|
13
|
+
|
|
14
|
+
## Unreleased
|
|
15
|
+
|
|
16
|
+
### Compilation
|
|
17
|
+
|
|
18
|
+
- Align the in-memory event-bus fixture and subscriber test calls with the current CMS argument objects.
|
|
19
|
+
- Preserve the rejecting command-gateway mock's `Promise<never>` result so it satisfies the generic executor contract.
|
|
20
|
+
- Validate record payloads before catch-all fixture delivery instead of casting generic events; add batch/unsubscribe parity and invalid-payload regression coverage (not run).
|
|
21
|
+
|
|
22
|
+
### Kernel and bus contracts
|
|
23
|
+
|
|
24
|
+
- Use core runtime/JSON/ID/time types and `Pick<Logger, 'warn'>`; structured warning error data is passed in argument two.
|
|
25
|
+
- Pass notification subscriptions as `{ eventName, handler }` and change-set insertion as `{ record, items }, { event }`.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
- Restore independent empty `fieldErrors` defaults for message-only field and
|
|
29
|
+
submission validation errors.
|
|
30
|
+
- Validate name updates against the host's reserved-slug policy using the stored,
|
|
31
|
+
immutable slug. Reject before persistence when a policy now reserves that slug.
|
|
32
|
+
- Add error-default and reserved-slug update regressions; execution is deferred by owner directive.
|
|
33
|
+
|
|
34
|
+
## 0.1.0 — 2026-10-01
|
|
35
|
+
|
|
36
|
+
- Move the unpublished CMS forms extraction and all of its tests into the independent `@jini-ai/cms-forms` package.
|
|
37
|
+
- Export form contracts, validation, definition writes, submissions, notification and rate-limit keys from the package root.
|
|
38
|
+
- Inject audited command execution through `FormCommandExecutorPort`; retain type-only CMS contract imports.
|
|
39
|
+
- Remove the CMS forms export and metadata entry without retaining an alias.
|
|
40
|
+
- Add standalone package and product-reference guard tests. Verification is deferred by owner directive.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing the Work or
|
|
166
|
+
Derivative Works thereof, You may choose to offer, and charge a
|
|
167
|
+
fee for, acceptance of support, warranty, indemnity, or other
|
|
168
|
+
liability obligations and/or rights consistent with this License.
|
|
169
|
+
However, in accepting such obligations, You may act only on Your
|
|
170
|
+
own behalf and on Your sole responsibility, not on behalf of any
|
|
171
|
+
other Contributor, and only if You agree to indemnify, defend,
|
|
172
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
173
|
+
or claims asserted against, such Contributor by reason of your
|
|
174
|
+
accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Open Design contributors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @jini-ai/cms-forms
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Configurable forms in the CMS family, kept as a private workspace package at `packages/cms/forms`. Do not publish this package to npm. The root export includes field and submission validation, honeypot handling, domain errors and records, audited definition writes, anonymous submissions, rate-limit key construction, and notification subscribers. Version 0.1.0 uses Apache-2.0 licensing.
|
|
4
|
+
|
|
5
|
+
Runtime code has no framework, Node, database, mail-provider, or CMS implementation imports. CMS contracts are type-only dependencies from `@jini-ai/cms/core`. There is no `@jini-ai/cms/forms` compatibility alias.
|
|
6
|
+
|
|
7
|
+
Every public function accepts a required object and, where applicable, a second optional object. Consumers bind repositories, clock, IDs, authorization, reserved slugs, permission names, rate limits, outbox, background dispatcher, logger, mailer, sender and audited command execution.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { executeCommand } from "@jini-ai/cms/core";
|
|
11
|
+
import { createFormDefinition } from "@jini-ai/cms-forms";
|
|
12
|
+
|
|
13
|
+
const { definition } = await createFormDefinition(
|
|
14
|
+
{
|
|
15
|
+
deps: {
|
|
16
|
+
repo, clock, idGen, changeSets, authorize, executeCommand,
|
|
17
|
+
permission: formWritePermission,
|
|
18
|
+
reservedSlugs,
|
|
19
|
+
},
|
|
20
|
+
input: { workspaceId, actor, name, slug, fields },
|
|
21
|
+
},
|
|
22
|
+
{ notify, idempotencyKey, outbox },
|
|
23
|
+
);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The command executor port preserves the CMS gateway's generic result and audit contract; hosts may bind their own compatible gateway. Optional notification configuration, idempotency keys and definition-write outbox wiring belong in the second argument. Anonymous submission dependencies and input are passed as `submitForm({ deps, input })`. Subscriber dependencies are passed as `registerFormNotifySubscriber({ bus, sender, logger, mailer, formDefinitionRepo, formSubmissionRepo })`.
|
|
27
|
+
|
|
28
|
+
Repositories, HTTP routes, tool registration, concrete rate-limit profiles, pagination adapters, mail transports and composition remain consumer responsibilities. Source validation, event names, error messages, notification deduplication and effect ordering are preserved by the moved tests. Submission persistence and outbox enqueue remain separate awaited effects; background dispatch does not delay acceptance. Atomicity depends on the injected host adapters.
|
|
29
|
+
|
|
30
|
+
See [API.md](./API.md) for the source mapping, caller inventory, changed files and exact verification commands. Tests, typechecks, builds and packing were not run under the owner's directive; dependencies and generated output must be prepared later by the coordinator.
|
|
31
|
+
|
|
32
|
+
## Design decisions
|
|
33
|
+
|
|
34
|
+
- [Anonymous submissions isolate spam and notification failures](docs/decisions/DR-001-anonymous-submission-containment.md).
|
|
35
|
+
|
|
36
|
+
Runtime contracts use `Clock`, `IdGenerator` and JSON/ID/time types from
|
|
37
|
+
`@jini-ai/core/primitives`. The logger dependency is `Pick<Logger, 'warn'>`: call
|
|
38
|
+
`warn({ message }, { error })`. Notification subscriptions use
|
|
39
|
+
`bus.subscribe({ eventName, handler })`; change-set repositories use
|
|
40
|
+
`insert({ record, items }, { event })`.
|
|
41
|
+
|
|
42
|
+
The in-memory event-bus test fixture implements the CMS `EventBusPort`, including
|
|
43
|
+
`publishBatch({ events })` and `subscribeAll({ handler })`. A command gateway that
|
|
44
|
+
always rejects returns `Promise<never>`, preserving the executor's generic result contract.
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** Stable domain error types; callers map them to their own transport errors. */
|
|
2
|
+
export declare class FormFieldValidationError extends Error {
|
|
3
|
+
readonly fieldErrors: Array<{
|
|
4
|
+
field: string;
|
|
5
|
+
reason: string;
|
|
6
|
+
}>;
|
|
7
|
+
constructor({ message, fieldErrors }: {
|
|
8
|
+
message: string;
|
|
9
|
+
fieldErrors?: Array<{
|
|
10
|
+
field: string;
|
|
11
|
+
reason: string;
|
|
12
|
+
}> | undefined;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export declare class FormSubmissionValidationError extends Error {
|
|
16
|
+
readonly fieldErrors: Array<{
|
|
17
|
+
field: string;
|
|
18
|
+
reason: string;
|
|
19
|
+
}>;
|
|
20
|
+
constructor({ message, fieldErrors }: {
|
|
21
|
+
message: string;
|
|
22
|
+
fieldErrors?: Array<{
|
|
23
|
+
field: string;
|
|
24
|
+
reason: string;
|
|
25
|
+
}> | undefined;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
export declare class FormSlugConflictError extends Error {
|
|
29
|
+
readonly slug: string;
|
|
30
|
+
constructor({ message, slug }: {
|
|
31
|
+
message: string;
|
|
32
|
+
slug: string;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** Disabled definitions deliberately use the same not-found error as missing definitions. */
|
|
36
|
+
export declare class FormDefinitionNotFoundError extends Error {
|
|
37
|
+
constructor({ message }: {
|
|
38
|
+
message: string;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export declare class FormSubmissionNotFoundError extends Error {
|
|
42
|
+
constructor({ message }: {
|
|
43
|
+
message: string;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
export declare class FormRateLimitExceededError extends Error {
|
|
47
|
+
readonly retryAfterSeconds: number;
|
|
48
|
+
constructor({ message, retryAfterSeconds }: {
|
|
49
|
+
message: string;
|
|
50
|
+
retryAfterSeconds: number;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=errors.d.ts.map
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Stable domain error types; callers map them to their own transport errors. */
|
|
2
|
+
export class FormFieldValidationError extends Error {
|
|
3
|
+
fieldErrors;
|
|
4
|
+
constructor({ message, fieldErrors = [] }) { super(message); this.fieldErrors = fieldErrors; }
|
|
5
|
+
}
|
|
6
|
+
export class FormSubmissionValidationError extends Error {
|
|
7
|
+
fieldErrors;
|
|
8
|
+
constructor({ message, fieldErrors = [] }) { super(message); this.fieldErrors = fieldErrors; }
|
|
9
|
+
}
|
|
10
|
+
export class FormSlugConflictError extends Error {
|
|
11
|
+
slug;
|
|
12
|
+
constructor({ message, slug }) { super(message); this.slug = slug; }
|
|
13
|
+
}
|
|
14
|
+
/** Disabled definitions deliberately use the same not-found error as missing definitions. */
|
|
15
|
+
export class FormDefinitionNotFoundError extends Error {
|
|
16
|
+
constructor({ message }) { super(message); }
|
|
17
|
+
}
|
|
18
|
+
export class FormSubmissionNotFoundError extends Error {
|
|
19
|
+
constructor({ message }) { super(message); }
|
|
20
|
+
}
|
|
21
|
+
export class FormRateLimitExceededError extends Error {
|
|
22
|
+
retryAfterSeconds;
|
|
23
|
+
constructor({ message, retryAfterSeconds }) { super(message); this.retryAfterSeconds = retryAfterSeconds; }
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=errors.js.map
|
package/dist/forms.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** Pure field/submission validation and honeypot decisions. Error order and stored value normalization are stable. */
|
|
2
|
+
import type { FieldDescriptor, FormDefinitionRecord } from "./types.js";
|
|
3
|
+
export declare const MAX_FIELDS = 20;
|
|
4
|
+
export declare const MAX_LABEL_LENGTH = 200;
|
|
5
|
+
export declare const MIN_MAX_LENGTH = 1;
|
|
6
|
+
export declare const MAX_MAX_LENGTH = 5000;
|
|
7
|
+
export declare const FIELD_ID_PATTERN: RegExp;
|
|
8
|
+
export declare const FIELD_TYPES: Set<string>;
|
|
9
|
+
export declare const MAX_CLASS_NAME_LENGTH = 300;
|
|
10
|
+
export declare const MAX_ATTRIBUTES_PER_FIELD = 12;
|
|
11
|
+
export declare const MAX_ATTRIBUTE_VALUE_LENGTH = 300;
|
|
12
|
+
/**
|
|
13
|
+
* Escaping makes an attribute value inert, but cannot make a name such as onclick safe. Names
|
|
14
|
+
* are deny-by-default: only aria-* and data-* namespaces and a fixed closed list are accepted.
|
|
15
|
+
* Event handlers, style/formaction/href/src/srcdoc can inject behavior; id/name/type overrides
|
|
16
|
+
* can break label association, submission keys or the renderer's declared field vocabulary.
|
|
17
|
+
*/
|
|
18
|
+
export declare const ATTRIBUTE_NAME_PATTERN: RegExp;
|
|
19
|
+
export declare const HONEYPOT_KEY = "_hp";
|
|
20
|
+
export interface FieldError {
|
|
21
|
+
field: string;
|
|
22
|
+
reason: string;
|
|
23
|
+
}
|
|
24
|
+
export type FieldValidationResult = {
|
|
25
|
+
valid: true;
|
|
26
|
+
} | {
|
|
27
|
+
valid: false;
|
|
28
|
+
fieldErrors: FieldError[];
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Collect every violation, not just the first: arity errors, then descriptors in array order,
|
|
32
|
+
* then each rule in the fixed order below. Callers display this list as-is, so order is observable.
|
|
33
|
+
* Empty helper results mean a satisfied rule. Bounds are 1-20 fields, labels 1-200 characters,
|
|
34
|
+
* and maxLength 1-5000 (never on checkboxes), with unique lowercase field IDs.
|
|
35
|
+
*/
|
|
36
|
+
export declare function validateFieldDescriptors({ fields }: {
|
|
37
|
+
fields: FieldDescriptor[];
|
|
38
|
+
}): FieldValidationResult;
|
|
39
|
+
export interface ValidateSubmissionPayloadInput {
|
|
40
|
+
definition: FormDefinitionRecord;
|
|
41
|
+
body: Record<string, unknown>;
|
|
42
|
+
}
|
|
43
|
+
export type SubmissionValidationResult = {
|
|
44
|
+
valid: true;
|
|
45
|
+
data: Record<string, string | boolean>;
|
|
46
|
+
} | {
|
|
47
|
+
valid: false;
|
|
48
|
+
fieldErrors: FieldError[];
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Only declared, accepted fields reach data; the reserved honeypot never does. Report unknown
|
|
52
|
+
* body keys first, then declared-field errors in definition order, independent of body key order.
|
|
53
|
+
* This keeps the declared-field portion of the error list stable across equivalent submissions.
|
|
54
|
+
*/
|
|
55
|
+
export declare function validateSubmissionPayload(input: ValidateSubmissionPayloadInput): SubmissionValidationResult;
|
|
56
|
+
export declare function isHoneypotTripped(input: {
|
|
57
|
+
hp: unknown;
|
|
58
|
+
}): boolean;
|
|
59
|
+
//# sourceMappingURL=forms.d.ts.map
|
package/dist/forms.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// Domain validation stays independent of feature manifests, and returns decisions without I/O
|
|
2
|
+
// or exceptions so write and submission services can choose their own error presentation.
|
|
3
|
+
// Published tool schemas and render-time defensive checks use these exported bounds/patterns
|
|
4
|
+
// instead of restating them; validation owns the enforcement rule.
|
|
5
|
+
const MIN_FIELDS = 1;
|
|
6
|
+
export const MAX_FIELDS = 20;
|
|
7
|
+
const MIN_LABEL_LENGTH = 1;
|
|
8
|
+
export const MAX_LABEL_LENGTH = 200;
|
|
9
|
+
export const MIN_MAX_LENGTH = 1;
|
|
10
|
+
export const MAX_MAX_LENGTH = 5000;
|
|
11
|
+
export const FIELD_ID_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
12
|
+
export const FIELD_TYPES = new Set(["text", "email", "textarea", "checkbox"]);
|
|
13
|
+
export const MAX_CLASS_NAME_LENGTH = 300;
|
|
14
|
+
export const MAX_ATTRIBUTES_PER_FIELD = 12;
|
|
15
|
+
export const MAX_ATTRIBUTE_VALUE_LENGTH = 300;
|
|
16
|
+
/**
|
|
17
|
+
* Escaping makes an attribute value inert, but cannot make a name such as onclick safe. Names
|
|
18
|
+
* are deny-by-default: only aria-* and data-* namespaces and a fixed closed list are accepted.
|
|
19
|
+
* Event handlers, style/formaction/href/src/srcdoc can inject behavior; id/name/type overrides
|
|
20
|
+
* can break label association, submission keys or the renderer's declared field vocabulary.
|
|
21
|
+
*/
|
|
22
|
+
export const ATTRIBUTE_NAME_PATTERN = /^(aria-[a-z0-9-]+|data-[a-z0-9-]+|placeholder|autocomplete|inputmode|pattern|title|min|max|step|minlength|spellcheck|readonly)$/;
|
|
23
|
+
// Accepted as submission metadata, never as a declared field or stored answer.
|
|
24
|
+
export const HONEYPOT_KEY = "_hp";
|
|
25
|
+
// Malformed IDs are never claimed: repeated malformed IDs must each report their shape error
|
|
26
|
+
// rather than misleading the second field's author with a duplicate-ID diagnosis.
|
|
27
|
+
function fieldIdErrors(descriptor, label, claimedIds) {
|
|
28
|
+
if (!descriptor.id || !FIELD_ID_PATTERN.test(descriptor.id)) {
|
|
29
|
+
return [{ field: label, reason: "id must match ^[a-z][a-z0-9_]*$" }];
|
|
30
|
+
}
|
|
31
|
+
if (claimedIds.has(descriptor.id)) {
|
|
32
|
+
return [{ field: descriptor.id, reason: "duplicate field id" }];
|
|
33
|
+
}
|
|
34
|
+
claimedIds.add(descriptor.id);
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
function fieldTypeErrors(descriptor, label) {
|
|
38
|
+
if (!FIELD_TYPES.has(descriptor.type)) {
|
|
39
|
+
return [{ field: label, reason: `type '${descriptor.type}' is not in the registered vocabulary` }];
|
|
40
|
+
}
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
function labelLengthErrors(descriptor, label) {
|
|
44
|
+
if (descriptor.label.length < MIN_LABEL_LENGTH || descriptor.label.length > MAX_LABEL_LENGTH) {
|
|
45
|
+
return [{ field: label, reason: `label must be ${MIN_LABEL_LENGTH}-${MAX_LABEL_LENGTH} characters` }];
|
|
46
|
+
}
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
// A checkbox carries a boolean, so no character cap can be honored. Report that prohibition
|
|
50
|
+
// before any numeric range error, rather than asking the author to fix a number they must remove.
|
|
51
|
+
function maxLengthErrors(descriptor, label) {
|
|
52
|
+
if (descriptor.maxLength == null)
|
|
53
|
+
return [];
|
|
54
|
+
if (descriptor.type === "checkbox") {
|
|
55
|
+
return [{ field: label, reason: "maxLength is forbidden for checkbox fields" }];
|
|
56
|
+
}
|
|
57
|
+
if (descriptor.maxLength < MIN_MAX_LENGTH || descriptor.maxLength > MAX_MAX_LENGTH) {
|
|
58
|
+
return [{ field: label, reason: `maxLength must be ${MIN_MAX_LENGTH}-${MAX_MAX_LENGTH}` }];
|
|
59
|
+
}
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
// HTML-escaped class names are inert; only their length is bounded. Check type defensively
|
|
63
|
+
// because real inputs include parsed request bodies, regardless of the descriptor's static type.
|
|
64
|
+
function classNameErrors(descriptor, label) {
|
|
65
|
+
const { className } = descriptor;
|
|
66
|
+
if (className == null)
|
|
67
|
+
return [];
|
|
68
|
+
if (typeof className !== "string") {
|
|
69
|
+
return [{ field: label, reason: "className must be a string" }];
|
|
70
|
+
}
|
|
71
|
+
if (className.length > MAX_CLASS_NAME_LENGTH) {
|
|
72
|
+
return [{ field: label, reason: `className must be at most ${MAX_CLASS_NAME_LENGTH} characters` }];
|
|
73
|
+
}
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
// A refused name will never render, so its value is moot. Reporting value errors as well would
|
|
77
|
+
// imply that correcting a value could make a structurally unsafe attribute acceptable.
|
|
78
|
+
function attributeEntryErrors(label, attrName, attrValue) {
|
|
79
|
+
if (!ATTRIBUTE_NAME_PATTERN.test(attrName)) {
|
|
80
|
+
return [{ field: label, reason: `attribute '${attrName}' is not allowed` }];
|
|
81
|
+
}
|
|
82
|
+
if (typeof attrValue !== "string") {
|
|
83
|
+
return [{ field: label, reason: `attribute '${attrName}' value must be a string` }];
|
|
84
|
+
}
|
|
85
|
+
if (attrValue.length > MAX_ATTRIBUTE_VALUE_LENGTH) {
|
|
86
|
+
return [{ field: label, reason: `attribute '${attrName}' value must be at most ${MAX_ATTRIBUTE_VALUE_LENGTH} characters` }];
|
|
87
|
+
}
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
// Exceeding the count cap still checks every attribute so the author sees unsafe names in one
|
|
91
|
+
// round trip. This visits O(a) caller-supplied attributes; the reported cap does not truncate work.
|
|
92
|
+
function attributeErrors(descriptor, label) {
|
|
93
|
+
const { attributes } = descriptor;
|
|
94
|
+
if (attributes == null)
|
|
95
|
+
return [];
|
|
96
|
+
const entries = Object.entries(attributes);
|
|
97
|
+
const errors = [];
|
|
98
|
+
if (entries.length > MAX_ATTRIBUTES_PER_FIELD) {
|
|
99
|
+
errors.push({ field: label, reason: `at most ${MAX_ATTRIBUTES_PER_FIELD} attributes are allowed` });
|
|
100
|
+
}
|
|
101
|
+
for (const [attrName, attrValue] of entries) {
|
|
102
|
+
errors.push(...attributeEntryErrors(label, attrName, attrValue));
|
|
103
|
+
}
|
|
104
|
+
return errors;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Collect every violation, not just the first: arity errors, then descriptors in array order,
|
|
108
|
+
* then each rule in the fixed order below. Callers display this list as-is, so order is observable.
|
|
109
|
+
* Empty helper results mean a satisfied rule. Bounds are 1-20 fields, labels 1-200 characters,
|
|
110
|
+
* and maxLength 1-5000 (never on checkboxes), with unique lowercase field IDs.
|
|
111
|
+
*/
|
|
112
|
+
export function validateFieldDescriptors({ fields }) {
|
|
113
|
+
const fieldErrors = [];
|
|
114
|
+
if (fields.length < MIN_FIELDS) {
|
|
115
|
+
fieldErrors.push({ field: "fields", reason: `at least ${MIN_FIELDS} field is required` });
|
|
116
|
+
}
|
|
117
|
+
if (fields.length > MAX_FIELDS) {
|
|
118
|
+
fieldErrors.push({ field: "fields", reason: `at most ${MAX_FIELDS} fields are allowed` });
|
|
119
|
+
}
|
|
120
|
+
const claimedIds = new Set();
|
|
121
|
+
for (const descriptor of fields) {
|
|
122
|
+
const label = descriptor.id || "(unknown)";
|
|
123
|
+
fieldErrors.push(...fieldIdErrors(descriptor, label, claimedIds), ...fieldTypeErrors(descriptor, label), ...labelLengthErrors(descriptor, label), ...maxLengthErrors(descriptor, label), ...classNameErrors(descriptor, label), ...attributeErrors(descriptor, label));
|
|
124
|
+
}
|
|
125
|
+
if (fieldErrors.length > 0)
|
|
126
|
+
return { valid: false, fieldErrors };
|
|
127
|
+
return { valid: true };
|
|
128
|
+
}
|
|
129
|
+
// The honeypot is tolerated in the raw body, but is excluded from stored answers.
|
|
130
|
+
function unregisteredKeyErrors(body, fieldsById) {
|
|
131
|
+
const errors = [];
|
|
132
|
+
for (const key of Object.keys(body)) {
|
|
133
|
+
if (key === HONEYPOT_KEY)
|
|
134
|
+
continue;
|
|
135
|
+
if (!fieldsById.has(key)) {
|
|
136
|
+
errors.push({ field: key, reason: "unregistered_key" });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return errors;
|
|
140
|
+
}
|
|
141
|
+
// Whitespace is rejected only for required text; optional whitespace is an answer the definition
|
|
142
|
+
// permits. Preserve the submitted string instead of silently trimming its stored value.
|
|
143
|
+
function resolveTextValue(field, value) {
|
|
144
|
+
if (typeof value !== "string")
|
|
145
|
+
return { kind: "rejected", reason: "must be a string" };
|
|
146
|
+
if (field.required && value.trim() === "")
|
|
147
|
+
return { kind: "rejected", reason: "required" };
|
|
148
|
+
if (field.maxLength != null && value.length > field.maxLength)
|
|
149
|
+
return { kind: "rejected", reason: "too_long" };
|
|
150
|
+
return { kind: "accepted", value };
|
|
151
|
+
}
|
|
152
|
+
// Native URL-encoded forms submit "on" for checked boxes without a value attribute; accepting
|
|
153
|
+
// only JSON booleans would reject every checked checkbox when client JavaScript is disabled.
|
|
154
|
+
// An unticked native checkbox omits its key; submission resolution treats that as absence.
|
|
155
|
+
const HTML_CHECKBOX_ON_VALUE = "on";
|
|
156
|
+
function resolveCheckboxValue(value) {
|
|
157
|
+
if (typeof value === "boolean")
|
|
158
|
+
return { kind: "accepted", value };
|
|
159
|
+
if (value === HTML_CHECKBOX_ON_VALUE)
|
|
160
|
+
return { kind: "accepted", value: true };
|
|
161
|
+
return { kind: "rejected", reason: "must be a boolean" };
|
|
162
|
+
}
|
|
163
|
+
// JSON and native forms differ in how they represent missing values, so null/undefined both
|
|
164
|
+
// mean absence. Boolean false is a real checkbox answer, including for a required checkbox.
|
|
165
|
+
function resolveSubmittedValue(field, value) {
|
|
166
|
+
if (value === undefined || value === null) {
|
|
167
|
+
return field.required ? { kind: "rejected", reason: "required" } : { kind: "absent" };
|
|
168
|
+
}
|
|
169
|
+
if (field.type === "checkbox")
|
|
170
|
+
return resolveCheckboxValue(value);
|
|
171
|
+
return resolveTextValue(field, value);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Only declared, accepted fields reach data; the reserved honeypot never does. Report unknown
|
|
175
|
+
* body keys first, then declared-field errors in definition order, independent of body key order.
|
|
176
|
+
* This keeps the declared-field portion of the error list stable across equivalent submissions.
|
|
177
|
+
*/
|
|
178
|
+
export function validateSubmissionPayload(input) {
|
|
179
|
+
const { definition, body } = input;
|
|
180
|
+
const fieldsById = new Map(definition.fields.map((f) => [f.id, f]));
|
|
181
|
+
const fieldErrors = unregisteredKeyErrors(body, fieldsById);
|
|
182
|
+
const data = {};
|
|
183
|
+
for (const field of definition.fields) {
|
|
184
|
+
const resolved = resolveSubmittedValue(field, body[field.id]);
|
|
185
|
+
if (resolved.kind === "rejected") {
|
|
186
|
+
fieldErrors.push({ field: field.id, reason: resolved.reason });
|
|
187
|
+
}
|
|
188
|
+
else if (resolved.kind === "accepted") {
|
|
189
|
+
data[field.id] = resolved.value;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (fieldErrors.length > 0)
|
|
193
|
+
return { valid: false, fieldErrors };
|
|
194
|
+
return { valid: true, data };
|
|
195
|
+
}
|
|
196
|
+
// Only a non-empty trimmed string trips the trap; absent, non-string or whitespace-only values
|
|
197
|
+
// do not reject an otherwise valid submission.
|
|
198
|
+
export function isHoneypotTripped(input) {
|
|
199
|
+
if (typeof input.hp !== "string")
|
|
200
|
+
return false;
|
|
201
|
+
return input.hp.trim().length > 0;
|
|
202
|
+
}
|
|
203
|
+
//# sourceMappingURL=forms.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./types.js";
|
|
2
|
+
export * from "./ports.js";
|
|
3
|
+
export * from "./errors.js";
|
|
4
|
+
export * from "./forms.js";
|
|
5
|
+
export * from "./rate-limit-key.js";
|
|
6
|
+
export * from "./write-service.js";
|
|
7
|
+
export * from "./submit-service.js";
|
|
8
|
+
export * from "./notify-subscriber.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./types.js";
|
|
2
|
+
export * from "./ports.js";
|
|
3
|
+
export * from "./errors.js";
|
|
4
|
+
export * from "./forms.js";
|
|
5
|
+
export * from "./rate-limit-key.js";
|
|
6
|
+
export * from "./write-service.js";
|
|
7
|
+
export * from "./submit-service.js";
|
|
8
|
+
export * from "./notify-subscriber.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|