@orkestrel/form 0.0.5 → 0.0.7

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.
@@ -1,4 +1,4 @@
1
- import { arrayOf, attempt, cloneJSONRecord, isArray, isBoolean, isBoundedJSONRecord, isContractError, isFiniteNumber, isFunction, isInteger, isRecord, isString, keyOf, parseNumber, readArrayEntries, recordOf, unionOf } from "@orkestrel/contract";
1
+ import { arrayOf, attempt, cloneJSONRecord, isArray, isBoolean, isBoundedJSONRecord, isContractError, isFiniteNumber, isFunction, isInstance, isInteger, isRecord, isString, keyOf, parseNumber, readArrayEntries, recordOf, unionOf } from "@orkestrel/contract";
2
2
  import { Emitter } from "@orkestrel/emitter";
3
3
  //#region src/core/constants.ts
4
4
  /** Lists every field control, in the order declared by the public contract. */
@@ -106,23 +106,23 @@ var DATE_PATTERN = Object.freeze(/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01
106
106
  var TIME_PATTERN = Object.freeze(/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/);
107
107
  /** Matches an ISO local date and time string with optional seconds. */
108
108
  var DATETIME_PATTERN = Object.freeze(/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/);
109
- /** Caps the accepted source length for an authored regular expression. */
109
+ /** Caps the accepted source length for an authored regular expression, at 256. */
110
110
  var PATTERN_LIMIT = 256;
111
- /** Caps the number of fields one schema may declare. */
111
+ /** Caps the number of fields one schema may declare, at 512. */
112
112
  var FIELD_LIMIT = 512;
113
- /** Caps the number of groups one schema may declare. */
113
+ /** Caps the number of groups one schema may declare, at 64. */
114
114
  var GROUP_LIMIT = 64;
115
- /** Caps the number of choices one `select` or `checkbox` field may offer. */
115
+ /** Caps the number of choices one `select` or `checkbox` field may offer, at 1024. */
116
116
  var CHOICE_LIMIT = 1024;
117
- /** Caps the number of entries one list-valued answer may hold. */
117
+ /** Caps the number of entries one list-valued answer may hold, at 1024. */
118
118
  var LIST_LIMIT = 1024;
119
- /** Caps the length, in UTF-16 code units, of a schema, group, or field name. */
119
+ /** Caps the length, in UTF-16 code units, of a schema, group, or field name, at 128. */
120
120
  var NAME_LIMIT = 128;
121
- /** Caps the length, in UTF-16 code units, of any single retained string. */
121
+ /** Caps the length, in UTF-16 code units, of any single retained string, at 65536. */
122
122
  var STRING_LIMIT = 65536;
123
- /** Caps the total length, in UTF-16 code units, of every string one schema retains. */
123
+ /** Caps the total length, in UTF-16 code units, of every string one schema retains, at 1048576. */
124
124
  var TEXT_LIMIT = 1048576;
125
- /** Caps the total number of records, arrays, and leaves one schema retains. */
125
+ /** Caps the total number of records, arrays, and leaves one schema retains, at 16384. */
126
126
  var NODE_LIMIT = 16384;
127
127
  //#endregion
128
128
  //#region src/core/errors.ts
@@ -165,7 +165,7 @@ var FormError = class extends Error {
165
165
  * @returns True if the value is a {@link FormError} instance; false otherwise.
166
166
  */
167
167
  function isFormError(input) {
168
- return input instanceof FormError;
168
+ return isInstance(input, FormError);
169
169
  }
170
170
  //#endregion
171
171
  //#region src/core/validators.ts
@@ -190,6 +190,10 @@ function isFormStatus(input) {
190
190
  /**
191
191
  * Determines whether an unknown value has a form field value shape.
192
192
  *
193
+ * @remarks
194
+ * A string, a finite number, a boolean, and a list of strings each qualify. A number that is not
195
+ * finite does not, so `NaN` and `Infinity` are refused.
196
+ *
193
197
  * @param input - The value to inspect.
194
198
  * @returns True if the value is a field value; false otherwise.
195
199
  */
@@ -349,6 +353,10 @@ function isFormGroup(input) {
349
353
  /**
350
354
  * Determines whether an unknown value is one exact structural form schema.
351
355
  *
356
+ * @remarks
357
+ * Structure alone is read. Domain soundness — a duplicate name, a bound no answer satisfies, a
358
+ * breached budget — is {@link auditSchema}'s question.
359
+ *
352
360
  * @param input - The value to inspect.
353
361
  * @returns True if the value is a structurally valid form schema; false otherwise.
354
362
  */
@@ -513,7 +521,7 @@ function defineEntry(target, name, value) {
513
521
  *
514
522
  * @remarks
515
523
  * The prototype-safe write of {@link defineEntry}, frozen: the entry is neither writable nor
516
- * configurable, so the record a parser hands back cannot be edited through the key it just filled.
524
+ * configurable, so the record a parser hands back cannot be edited through the key it filled.
517
525
  *
518
526
  * @example
519
527
  * ```ts
@@ -533,6 +541,10 @@ function freezeEntry(target, name, value) {
533
541
  /**
534
542
  * Checks whether a value has the shape required by one field control.
535
543
  *
544
+ * @remarks
545
+ * Every write and every seeded value passes through this gate, and it reads `STRING_LIMIT` and
546
+ * `LIST_LIMIT` before it consults the control, so no regular expression sees an over-long value.
547
+ *
536
548
  * @param field - The field that owns the value.
537
549
  * @param value - The unknown value to inspect.
538
550
  * @returns True if the control can hold the value; false otherwise.
@@ -713,6 +725,10 @@ function evaluateForm(schema, values, options) {
713
725
  /**
714
726
  * Computes the values explicitly seeded by a schema.
715
727
  *
728
+ * @remarks
729
+ * `password` and `file` declare no default, so a field of either control never appears in the
730
+ * result.
731
+ *
716
732
  * @param schema - The schema whose defaults to collect.
717
733
  * @returns A value record containing only fields with defaults.
718
734
  */
@@ -786,6 +802,10 @@ function matchesValues(a, b) {
786
802
  /**
787
803
  * Resolves and interpolates one rule message.
788
804
  *
805
+ * @remarks
806
+ * A replacement in `messages` is read first and {@link RULE_MESSAGES} supplies the copy otherwise,
807
+ * and `{limit}` in whichever text wins is replaced with the rule's operand.
808
+ *
789
809
  * @param rule - The rule whose message to resolve.
790
810
  * @param limit - The optional operand substituted for `{limit}`.
791
811
  * @param messages - Optional rule-specific message replacements.
@@ -1165,6 +1185,10 @@ function parseForm(input) {
1165
1185
  /**
1166
1186
  * Parses one answer against its field control.
1167
1187
  *
1188
+ * @remarks
1189
+ * A numeric string coerces to a number for a `number` field, and `'true'` and `'false'` coerce to
1190
+ * a boolean for a `confirm` field. Every other value must already have its control's shape.
1191
+ *
1168
1192
  * @param field - The field that defines the accepted value.
1169
1193
  * @param input - The unknown value to parse.
1170
1194
  * @returns The typed or lexically coerced field value, or `undefined` on refusal.
@@ -1210,7 +1234,8 @@ function parseValues(schema, input) {
1210
1234
  //#endregion
1211
1235
  //#region src/core/Form.ts
1212
1236
  /**
1213
- * Represents a form: a schema, the answers given against it, and the errors they carry.
1237
+ * Implements `FormInterface` exactly, over an owned schema, the answers given against it, and the
1238
+ * errors they carry.
1214
1239
  *
1215
1240
  * @remarks
1216
1241
  * The form owns its schema, so a later edit to the schema the caller passed changes nothing here.
@@ -1622,8 +1647,10 @@ var Form = class {
1622
1647
  * @throws A {@link FormError} coded `SCHEMA` when the schema is malformed, `FIELD` when
1623
1648
  * `options.values` names a field the schema does not declare, and `CONTROL` when a seeded value
1624
1649
  * is one its field's control cannot hold.
1625
- * @example
1650
+ * @example Open a form, answer it, and settle it
1626
1651
  * ```ts
1652
+ * import { createForm } from '@orkestrel/form'
1653
+ *
1627
1654
  * const form = createForm({
1628
1655
  * label: 'Sign up',
1629
1656
  * fields: [
@@ -1633,7 +1660,8 @@ var Form = class {
1633
1660
  * })
1634
1661
  *
1635
1662
  * form.fill({ email: 'ada@example.com', terms: true })
1636
- * form.submit() // { success: true, value: { email: 'ada@example.com', terms: true } }
1663
+ * const result = form.submit() // { success: true, value: { email: 'ada@example.com', terms: true } }
1664
+ * const answers = await form.answer // { email: 'ada@example.com', terms: true }
1637
1665
  * ```
1638
1666
  */
1639
1667
  function createForm(schema, options) {