@ilokesto/form 2.0.0 → 2.0.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/README.ko.md +74 -481
- package/README.md +76 -482
- package/dist/{chunk-EPQC3LKB.js → chunk-4F4SAGQ5.js} +320 -504
- package/dist/chunk-4F4SAGQ5.js.map +1 -0
- package/dist/{chunk-OBMSHDV2.js → chunk-JK644RR7.js} +3 -3
- package/dist/{chunk-OBMSHDV2.js.map → chunk-JK644RR7.js.map} +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.js +1 -1
- package/dist/react/index.js +2 -2
- package/dist/solid/index.js +2 -2
- package/dist/svelte/index.js +2 -2
- package/dist/vue/index.js +2 -2
- package/package.json +2 -2
- package/dist/chunk-EPQC3LKB.js.map +0 -1
package/README.md
CHANGED
|
@@ -818,508 +818,102 @@ Replaces the whole array. Existing item links are intentionally broken, so child
|
|
|
818
818
|
|
|
819
819
|
## Runtime flows
|
|
820
820
|
|
|
821
|
-
###
|
|
821
|
+
### Field commands
|
|
822
822
|
|
|
823
|
-
|
|
824
|
-
new CreateForm(options)
|
|
825
|
-
-> new FormStateStore(options.defaultValues)
|
|
826
|
-
-> FormStateInitializer.initialize(defaultValues)
|
|
827
|
-
-> fields for leaf values
|
|
828
|
-
-> arrayKeys for array containers
|
|
829
|
-
-> new ValidationEngine(store, options)
|
|
830
|
-
-> new FormFieldCommands(store, validation)
|
|
831
|
-
-> new FormArrayFactory(store)
|
|
832
|
-
-> new FormSubmitter(store, validation)
|
|
833
|
-
```
|
|
834
|
-
|
|
835
|
-
The important point is that all collaborators share the same `FormStateStore`.
|
|
836
|
-
|
|
837
|
-
### Value write flow
|
|
838
|
-
|
|
839
|
-
```txt
|
|
840
|
-
form.setValue(path, value, options)
|
|
841
|
-
-> FormFieldCommands.setValue()
|
|
842
|
-
-> FormPath.toFieldPath(path)
|
|
843
|
-
-> FormStateStore.setValue()
|
|
844
|
-
-> FormStateWriter.setValue()
|
|
845
|
-
-> compute dirty from defaultValues
|
|
846
|
-
-> optionally mark modified
|
|
847
|
-
-> optionally start change validation
|
|
848
|
-
```
|
|
849
|
-
|
|
850
|
-
`dirty` and `modified` are intentionally separate. Programmatic writes can make a field dirty without marking it as user-modified.
|
|
851
|
-
|
|
852
|
-
### Blur flow
|
|
823
|
+
Start with `src/core/form/CreateForm.ts`. It shows the complete orchestration:
|
|
853
824
|
|
|
854
825
|
```txt
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
->
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
-> otherwise return true
|
|
826
|
+
setValue -> normalize path -> store.setValue -> optional change validation
|
|
827
|
+
blur -> normalize path -> unfocus -> touch -> optional blur validation
|
|
828
|
+
trigger -> normalize paths -> selected-field or full validation
|
|
829
|
+
array -> create controller with the form's shared key generator
|
|
830
|
+
submit -> submit queue -> validation -> callback -> completion
|
|
861
831
|
```
|
|
862
832
|
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
form.trigger(...paths)
|
|
867
|
-
-> no paths: validateRegisteredFields('manual')
|
|
868
|
-
-> with paths: validateFields(keys, 'manual')
|
|
869
|
-
-> field-local schema exists: validate that field value and replace that field's errors
|
|
870
|
-
-> otherwise: StandardSchemaValidator.validate(values)
|
|
871
|
-
-> issue paths become PathKeys
|
|
872
|
-
-> selected field errors are replaced
|
|
873
|
-
```
|
|
833
|
+
Writes notify synchronously. Blur retains separate unfocus and touch notifications.
|
|
834
|
+
`dirty` compares against the default value with `Object.is`; `modified` records
|
|
835
|
+
user-originated writes. These flags have different meanings.
|
|
874
836
|
|
|
875
|
-
|
|
837
|
+
### Validation and submission
|
|
876
838
|
|
|
877
|
-
|
|
839
|
+
Selected-field validation runs local schemas first, then the form schema for
|
|
840
|
+
remaining targets. Full validation runs the form schema first, then registered
|
|
841
|
+
local schemas. A local result, including an empty error list, takes precedence.
|
|
842
|
+
Without a local schema, selected-field validation executes the whole form schema
|
|
843
|
+
but writes only the selected errors.
|
|
878
844
|
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
-> invalid: onInvalid(fields), return undefined
|
|
884
|
-
-> valid: onValid(getValues())
|
|
885
|
-
```
|
|
845
|
+
Every asynchronous phase checks both its validation revision and captured values
|
|
846
|
+
before writing errors. Independent field validations may finish in either order;
|
|
847
|
+
overlapping work must not overwrite newer results. Errors are applied through the
|
|
848
|
+
existing per-field write boundaries, not a new batch update.
|
|
886
849
|
|
|
887
|
-
`
|
|
850
|
+
`FormSubmitter` queues concurrent submissions. It increments the attempt count
|
|
851
|
+
immediately, retries stale validation, calls `onInvalid` for invalid results or
|
|
852
|
+
`onValid` with current values, and completes submit state when the queue drains.
|
|
888
853
|
|
|
889
|
-
### Array
|
|
854
|
+
### Array mutations
|
|
890
855
|
|
|
891
856
|
```txt
|
|
892
|
-
|
|
893
|
-
->
|
|
894
|
-
->
|
|
895
|
-
->
|
|
896
|
-
-> next values
|
|
897
|
-
-> next keys
|
|
898
|
-
-> previous-index to next-index mapper
|
|
899
|
-
-> FormArrayRebaser.rebase()
|
|
900
|
-
-> write next array into values
|
|
901
|
-
-> initialize a fresh FormState from next values
|
|
902
|
-
-> preserve non-array fields
|
|
903
|
-
-> move child metadata through the index mapper
|
|
904
|
-
-> write next array keys
|
|
857
|
+
controller reads values and keys
|
|
858
|
+
-> static planner computes { values, keys, mapPreviousIndex }
|
|
859
|
+
-> store.replaceState(previousState => rebase(previousState, path, mutation))
|
|
860
|
+
-> one next snapshot
|
|
905
861
|
```
|
|
906
862
|
|
|
907
|
-
|
|
863
|
+
Rebasing reconstructs values from the supplied snapshot, initializes the next
|
|
864
|
+
state, and moves surviving child metadata and nested array keys to their new
|
|
865
|
+
paths. Errors, touched, dirty, modified and focus follow the item. Unrelated
|
|
866
|
+
fields and submit state are preserved. Replace intentionally drops old child
|
|
867
|
+
metadata and creates fresh keys. The existing defaultValues behavior is retained.
|
|
908
868
|
|
|
909
869
|
## Internal architecture
|
|
910
870
|
|
|
911
871
|
```txt
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
->
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
-> array/ArrayKeyGenerator.ts
|
|
930
|
-
-> path/FormPath.ts
|
|
931
|
-
-> value/ValueHelper.ts
|
|
932
|
-
-> types.ts
|
|
933
|
-
-> src/adapters/
|
|
934
|
-
-> dom/FieldValue.ts
|
|
935
|
-
-> dom/RegisterBinding.ts
|
|
936
|
-
-> FormStateSummary.ts
|
|
937
|
-
-> src/react/index.ts
|
|
938
|
-
-> React hook adapter over shared adapter logic
|
|
939
|
-
-> src/vue/index.ts
|
|
940
|
-
-> Vue composable adapter over shared adapter logic
|
|
941
|
-
-> src/solid/index.ts
|
|
942
|
-
-> Solid helper adapter over shared adapter logic
|
|
943
|
-
-> src/svelte/index.ts
|
|
944
|
-
-> Svelte action adapter over shared adapter logic
|
|
945
|
-
```
|
|
946
|
-
|
|
947
|
-
Responsibility summary:
|
|
948
|
-
|
|
949
|
-
| Area | Responsibility |
|
|
950
|
-
| --- | --- |
|
|
951
|
-
| `form/` | Public orchestration: field commands, submit flow, array controller access. |
|
|
952
|
-
| `state/` | Normalized state initialization, reading, writing, and store facade. |
|
|
953
|
-
| `path/` | Conversion between public path input, tuple paths, and string path keys. |
|
|
954
|
-
| `value/` | Immutable nested get/set and reconstruction of values from field states. |
|
|
955
|
-
| `validation/` | Standard Schema execution and error normalization. |
|
|
956
|
-
| `array/` | Array item key management, mutation planning, and child field metadata rebasing. |
|
|
957
|
-
| `adapters/` | Internal shared adapter logic: DOM value extraction/binding and form-state aggregation. |
|
|
958
|
-
| `react/` | React hook adapter around the public `Form` interface. |
|
|
959
|
-
| `vue/` | Vue composable adapter around the public `Form` interface. |
|
|
960
|
-
| `solid/` | Solid helper adapter around the public `Form` interface. |
|
|
961
|
-
| `svelte/` | Svelte action adapter around the public `Form` interface. |
|
|
962
|
-
| `types.ts` | Public and internal TypeScript contracts. |
|
|
872
|
+
CreateForm public command order and shared array keys
|
|
873
|
+
FormStateStore state ownership and writes
|
|
874
|
+
FormStateInitializer nested defaults -> normalized snapshot
|
|
875
|
+
FormStateReader snapshot -> fields and nested values
|
|
876
|
+
ValidationEngine schema registration and validation phases
|
|
877
|
+
StandardSchemaValidator schema results -> field errors
|
|
878
|
+
ValidationSequencer overlapping validation revisions
|
|
879
|
+
ValidationSnapshot captured values and array-key comparison
|
|
880
|
+
FormSubmitter submit queue and callback lifecycle
|
|
881
|
+
FormArrayController array reads and state application
|
|
882
|
+
FormArrayMutationPlanner pure array mutation calculation
|
|
883
|
+
FormArrayRebaser previous snapshot + mutation -> next snapshot
|
|
884
|
+
```
|
|
885
|
+
|
|
886
|
+
React, Vue, Solid and Svelte adapters depend on the public `Form` contract.
|
|
887
|
+
`adapters/` shares DOM value/binding and aggregate-state logic; subscriptions and
|
|
888
|
+
component lifecycle ownership remain in each framework directory.
|
|
963
889
|
|
|
964
890
|
## Core walkthrough
|
|
965
891
|
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
`types.ts`
|
|
990
|
-
|
|
991
|
-
- public path type: `FieldPathInput`; internal path concepts: `FieldPathSegment`, `FieldPath`, `PathKey`
|
|
992
|
-
- exported validation types: `FormError`, `StandardSchemaV1`; internal validation trigger type: `ValidationTrigger`
|
|
993
|
-
- internal state types: `FieldState`, `ArrayKeys`, `FormState`
|
|
994
|
-
- public API types: `CreateFormOptions`, `Form`; internal command helper types: `FormArray`, `SetValueOptions`
|
|
995
|
-
|
|
996
|
-
The most important design in this file is that public string paths are literal field names. Nested fields require tuple paths.
|
|
997
|
-
|
|
998
|
-
### `src/core/form/CreateForm.ts`
|
|
999
|
-
|
|
1000
|
-
`CreateForm` is the top-level controller and the main public class.
|
|
1001
|
-
|
|
1002
|
-
It owns four collaborators:
|
|
1003
|
-
|
|
1004
|
-
```txt
|
|
1005
|
-
store -> state storage and state operations
|
|
1006
|
-
fields -> setValue, blur, errors, trigger
|
|
1007
|
-
arrays -> array(path) controller creation
|
|
1008
|
-
submitter -> submitCount, validation, callbacks
|
|
1009
|
-
```
|
|
1010
|
-
|
|
1011
|
-
The constructor creates one `FormStateStore`, then passes it to all collaborators. That makes every command operate on the same snapshot.
|
|
1012
|
-
|
|
1013
|
-
Most methods are thin delegations:
|
|
1014
|
-
|
|
1015
|
-
- `getState()`, `getFieldState()`, `getValue()`, `getValues()`, `reset()` delegate to `FormStateStore`.
|
|
1016
|
-
- `setValue()`, `blur()`, `focus()`, `setErrors()`, `clearErrors()`, `trigger()` delegate to `FormFieldCommands`.
|
|
1017
|
-
- `array()` converts the path to a tuple and asks `FormArrayFactory` for a controller.
|
|
1018
|
-
- `submit()` delegates to `FormSubmitter`.
|
|
1019
|
-
|
|
1020
|
-
This keeps the public API stable while implementation responsibilities stay separated.
|
|
1021
|
-
|
|
1022
|
-
### `src/core/form/FormFieldCommands.ts`
|
|
1023
|
-
|
|
1024
|
-
`FormFieldCommands` groups field-level operations.
|
|
1025
|
-
|
|
1026
|
-
`setValue()`:
|
|
1027
|
-
|
|
1028
|
-
1. Converts the path to a tuple through `FormPath.toFieldPath()`.
|
|
1029
|
-
2. Writes the value through `store.setValue()`.
|
|
1030
|
-
3. Receives the resulting `PathKey`.
|
|
1031
|
-
4. Starts change validation if `options.validate` is true or `validateOn` contains `'change'`.
|
|
1032
|
-
|
|
1033
|
-
`blur()`:
|
|
1034
|
-
|
|
1035
|
-
1. Converts the public path to a `PathKey`.
|
|
1036
|
-
2. Marks the field as touched.
|
|
1037
|
-
3. Runs blur validation only when configured.
|
|
1038
|
-
|
|
1039
|
-
`focus()` sets `isFocused: true` on the field. The matching `blur()` command clears `isFocused` (always, regardless of `validateOn`) and marks the field as `touched`. The `isFocused` flag is preserved across array rebasing.
|
|
1040
|
-
|
|
1041
|
-
`setErrors()`, `clearErrors()`, and `trigger()` normalize paths to keys and delegate to the store or validation engine.
|
|
1042
|
-
|
|
1043
|
-
### `src/core/form/FormSubmitter.ts`
|
|
1044
|
-
|
|
1045
|
-
`FormSubmitter` isolates submit-specific sequencing:
|
|
1046
|
-
|
|
1047
|
-
```txt
|
|
1048
|
-
increment submitCount
|
|
1049
|
-
validate all registered fields
|
|
1050
|
-
if invalid -> call onInvalid(fields) and return undefined
|
|
1051
|
-
if valid -> call onValid(getValues())
|
|
1052
|
-
```
|
|
1053
|
-
|
|
1054
|
-
The class exists because submit is a multi-step command and should not make `CreateForm` responsible for validation branching.
|
|
1055
|
-
|
|
1056
|
-
### `src/core/state/FieldStateFactory.ts`
|
|
1057
|
-
|
|
1058
|
-
`FieldStateFactory` centralizes the default field state shape:
|
|
1059
|
-
|
|
1060
|
-
```ts
|
|
1061
|
-
{
|
|
1062
|
-
value: undefined,
|
|
1063
|
-
errors: [],
|
|
1064
|
-
touched: false,
|
|
1065
|
-
dirty: false,
|
|
1066
|
-
modified: false,
|
|
1067
|
-
}
|
|
1068
|
-
```
|
|
1069
|
-
|
|
1070
|
-
It creates a new `errors` array each time to avoid shared mutable references. The same factory is used for initial leaf fields and missing-field fallbacks.
|
|
1071
|
-
|
|
1072
|
-
### `src/core/state/FormStateInitializer.ts`
|
|
1073
|
-
|
|
1074
|
-
`FormStateInitializer.initialize(defaultValues)` converts nested values into normalized `FormState`.
|
|
1075
|
-
|
|
1076
|
-
Rules:
|
|
1077
|
-
|
|
1078
|
-
1. Arrays are containers, not leaf fields.
|
|
1079
|
-
2. Array containers receive `arrayKeys` such as `initial-0`, `initial-1`.
|
|
1080
|
-
3. Array items are visited recursively.
|
|
1081
|
-
4. Plain objects are traversed.
|
|
1082
|
-
5. Empty plain objects are stored as leaf values because there is no deeper field.
|
|
1083
|
-
6. Non-plain objects such as `Date` and class instances are treated as leaf values.
|
|
1084
|
-
7. Primitives and `null` are leaf values.
|
|
1085
|
-
|
|
1086
|
-
This is why `getState().fields` contains leaf paths rather than the original nested object.
|
|
1087
|
-
|
|
1088
|
-
### `src/core/state/FormStateReader.ts`
|
|
1089
|
-
|
|
1090
|
-
`FormStateReader` handles read-only derived operations.
|
|
1091
|
-
|
|
1092
|
-
- `getKnownFieldPaths()` converts every stored `PathKey` back to a tuple path.
|
|
1093
|
-
- `getFieldStateByKey()` returns an existing field or a default `FieldState`.
|
|
1094
|
-
- `getFieldState()` and `getValue()` are path-input convenience methods.
|
|
1095
|
-
- `getValues()` reconstructs the full nested value tree with `ValueHelper.getValuesFromFields()`.
|
|
1096
|
-
- `getValueAtPath()` reconstructs values first, then reads a nested path from them.
|
|
1097
|
-
|
|
1098
|
-
The reader receives a snapshot getter rather than owning the store directly, so it always reads the latest state.
|
|
1099
|
-
|
|
1100
|
-
### `src/core/state/FormStateWriter.ts`
|
|
1101
|
-
|
|
1102
|
-
`FormStateWriter` performs all state mutations through `immer`.
|
|
1103
|
-
|
|
1104
|
-
> **Bundle note:** `immer` adds ~5KB to consumer bundles. Since `FormState` uses a flat `Record<PathKey, FieldState>`, spread-based updates (`{ ...state, [key]: nextField }`) could replace immer with no behavioral change. Benchmark before migrating — immer provides structural sharing and readability benefits that may outweigh the size cost for array rebasing paths.
|
|
1105
|
-
|
|
1106
|
-
`setValue()`:
|
|
1107
|
-
|
|
1108
|
-
- Converts the tuple path to a `PathKey`.
|
|
1109
|
-
- Reads the initial value at that tuple path.
|
|
1110
|
-
- Preserves existing field metadata when possible.
|
|
1111
|
-
- Replaces `value`.
|
|
1112
|
-
- Computes `dirty` with `Object.is`.
|
|
1113
|
-
- Sets `modified` only for `source: 'user'`.
|
|
1114
|
-
- Returns the `PathKey` so validation can reuse it.
|
|
1115
|
-
|
|
1116
|
-
Other methods:
|
|
1117
|
-
|
|
1118
|
-
- `touchField()` sets `touched: true`.
|
|
1119
|
-
- `setErrorsByKey()` replaces the error array.
|
|
1120
|
-
- `clearErrors()` clears target fields or all existing fields.
|
|
1121
|
-
- `reset()` reinitializes state from the current or replacement initial values.
|
|
1122
|
-
- `incrementSubmitCount()` increments attempts.
|
|
1123
|
-
- `replaceState()` is an escape hatch for whole-state operations such as array rebasing.
|
|
1124
|
-
|
|
1125
|
-
### `src/core/state/FormStateStore.ts`
|
|
1126
|
-
|
|
1127
|
-
`FormStateStore` is the facade used by the rest of the core.
|
|
1128
|
-
|
|
1129
|
-
It owns:
|
|
1130
|
-
|
|
1131
|
-
- `Store<FormState<TValues>>` from `@ilokesto/store`
|
|
1132
|
-
- `FormStateReader`
|
|
1133
|
-
- `FormStateWriter`
|
|
1134
|
-
|
|
1135
|
-
Collaborators do not need to know whether an operation is implemented by the reader, writer, or underlying store. They call the facade.
|
|
1136
|
-
|
|
1137
|
-
### `src/core/path/FormPath.ts`
|
|
1138
|
-
|
|
1139
|
-
`FormPath` defines path conversion rules.
|
|
1140
|
-
|
|
1141
|
-
Key methods:
|
|
1142
|
-
|
|
1143
|
-
- `path(...segments)`: helper for tuple paths.
|
|
1144
|
-
- `toFieldPath(input)`: string becomes `[input]`; tuple path passes through.
|
|
1145
|
-
- `pathInputToKey(input)`: public path input to `PathKey`.
|
|
1146
|
-
- `pathToKey(path)`: `[]` becomes `$`, otherwise JSON stringifies the tuple.
|
|
1147
|
-
- `keyToPath(key)`: parses and validates a key back into tuple segments.
|
|
1148
|
-
|
|
1149
|
-
The JSON encoding is what prevents path collisions.
|
|
1150
|
-
|
|
1151
|
-
> **Performance note:** `pathToKey` uses `JSON.stringify` and `keyToPath` uses `JSON.parse`. For large forms (hundreds of fields) with frequent updates, a custom separator-based encoding (e.g. NUL-joined) could reduce hot-path overhead. Benchmark before migrating — the current approach is correct and readable, and real-world impact is typically negligible.
|
|
1152
|
-
|
|
1153
|
-
### `src/core/value/ValueHelper.ts`
|
|
1154
|
-
|
|
1155
|
-
`ValueHelper` provides immutable nested value operations.
|
|
1156
|
-
|
|
1157
|
-
`getValueAtPath(source, path)` walks an object/array safely. If an intermediate value is nullish or primitive, it returns `undefined`.
|
|
1158
|
-
|
|
1159
|
-
`setValueAtPath(source, path, value)` returns a new root value and shallow-clones containers along the path. Missing containers are inferred from the next segment: a number creates an array, a string creates an object.
|
|
1160
|
-
|
|
1161
|
-
`getValuesFromFields(state, fieldPaths)` reconstructs public values in two phases:
|
|
1162
|
-
|
|
1163
|
-
1. Create empty array containers from `arrayKeys`.
|
|
1164
|
-
2. Write every leaf field value into its tuple path.
|
|
1165
|
-
|
|
1166
|
-
This lets normalized `fields` become a nested object again.
|
|
1167
|
-
|
|
1168
|
-
### `src/core/validation/ValidationEngine.ts`
|
|
1169
|
-
|
|
1170
|
-
`ValidationEngine` coordinates validation triggers and store writes.
|
|
1171
|
-
|
|
1172
|
-
- Default `validateOn` is `['submit']`.
|
|
1173
|
-
- If no schema is provided, validation succeeds with no errors.
|
|
1174
|
-
- `validateField(key, trigger)` runs the full schema and applies only that field's errors.
|
|
1175
|
-
- `validateFields(keys, trigger)` runs the full schema and applies only selected fields' errors.
|
|
1176
|
-
- `validateRegisteredFields(trigger)` runs the full schema and updates all current fields plus all schema error keys.
|
|
1177
|
-
|
|
1178
|
-
The engine deliberately ignores the trigger value when calling the schema. The trigger controls when the engine runs, not the schema API.
|
|
1179
|
-
|
|
1180
|
-
`ValidationSequencer` tracks full-form and per-field revisions. Overlapping work becomes stale, independent field work remains valid, and every result is checked against its captured field-value and array-shape snapshot before errors or submit callbacks are applied.
|
|
1181
|
-
|
|
1182
|
-
### `src/core/validation/StandardSchemaValidator.ts`
|
|
1183
|
-
|
|
1184
|
-
`StandardSchemaValidator` adapts Standard Schema results to core errors.
|
|
1185
|
-
|
|
1186
|
-
Success:
|
|
1187
|
-
|
|
1188
|
-
```ts
|
|
1189
|
-
{ valid: true, errorsByKey: {} }
|
|
1190
|
-
```
|
|
1191
|
-
|
|
1192
|
-
Failure:
|
|
1193
|
-
|
|
1194
|
-
```ts
|
|
1195
|
-
{
|
|
1196
|
-
valid: false,
|
|
1197
|
-
errorsByKey: {
|
|
1198
|
-
'["email"]': [{ type: 'standard_schema', message: 'Email is invalid' }],
|
|
1199
|
-
},
|
|
1200
|
-
}
|
|
1201
|
-
```
|
|
1202
|
-
|
|
1203
|
-
Issue path conversion rules:
|
|
1204
|
-
|
|
1205
|
-
- Missing or empty path becomes the root path `[]` and key `$`.
|
|
1206
|
-
- String and number path segments are preserved.
|
|
1207
|
-
- Object path segments use their `.key` property.
|
|
1208
|
-
- Unsupported keys such as symbols fall back to root errors.
|
|
1209
|
-
|
|
1210
|
-
### `src/core/array/ArrayKeyGenerator.ts`
|
|
1211
|
-
|
|
1212
|
-
`ArrayKeyGenerator` creates runtime item keys:
|
|
1213
|
-
|
|
1214
|
-
```txt
|
|
1215
|
-
item-1
|
|
1216
|
-
item-2
|
|
1217
|
-
item-3
|
|
1218
|
-
```
|
|
1219
|
-
|
|
1220
|
-
A single `FormArrayFactory` shares one generator per form instance, reducing accidental key collisions across controllers.
|
|
1221
|
-
|
|
1222
|
-
### `src/core/array/ArrayItemReorder.ts`
|
|
1223
|
-
|
|
1224
|
-
`ArrayItemReorder` is a pure helper for array order operations.
|
|
1225
|
-
|
|
1226
|
-
- `moveItem(items, from, to)` returns a reordered copy.
|
|
1227
|
-
- `swapItems(items, left, right)` returns a swapped copy.
|
|
1228
|
-
- `createIndexMapper(previousLength, nextOrder)` returns a function that maps a previous index to its next index.
|
|
1229
|
-
|
|
1230
|
-
`nextOrder` is expressed as "for each next position, which previous index is here?" New items use `-1`, and removed items simply do not appear.
|
|
1231
|
-
|
|
1232
|
-
### `src/core/array/FormArrayMutationPlanner.ts`
|
|
1233
|
-
|
|
1234
|
-
`FormArrayMutationPlanner` calculates array mutations without knowing about the store.
|
|
1235
|
-
|
|
1236
|
-
Each mutation returns:
|
|
1237
|
-
|
|
1238
|
-
```ts
|
|
1239
|
-
type FormArrayMutation = {
|
|
1240
|
-
values: readonly unknown[];
|
|
1241
|
-
keys: readonly string[];
|
|
1242
|
-
mapPreviousIndex: (index: number) => number | undefined;
|
|
1243
|
-
};
|
|
1244
|
-
```
|
|
1245
|
-
|
|
1246
|
-
Command behavior:
|
|
1247
|
-
|
|
1248
|
-
- `insert()` bounds the target index and inserts a new value/key. The new item maps from `-1`, so no previous metadata is attached to it.
|
|
1249
|
-
- `push()` appends a value/key.
|
|
1250
|
-
- `remove()` drops a value/key and maps removed indexes to `undefined`.
|
|
1251
|
-
- `move()` moves values and keys with the same rule.
|
|
1252
|
-
- `swap()` swaps values and keys with the same rule.
|
|
1253
|
-
- `replace()` returns all new values/keys and maps every previous index to `undefined`.
|
|
1254
|
-
|
|
1255
|
-
Because this planner is pure, it is easy to test independently from the store.
|
|
1256
|
-
|
|
1257
|
-
### `src/core/array/FormArrayPath.ts`
|
|
1258
|
-
|
|
1259
|
-
`FormArrayPath` contains path helpers used during rebasing.
|
|
1260
|
-
|
|
1261
|
-
- `hasPathPrefix(fieldPath, prefix)` checks tuple prefix equality.
|
|
1262
|
-
- `isArrayChildPath(fieldPath, arrayPath)` checks whether a field is below an array item, such as `['items', 0, 'name']` below `['items']`.
|
|
1263
|
-
- `replaceArrayIndex(fieldPath, arrayPath, nextIndex)` creates a new child path with the item index replaced.
|
|
1264
|
-
|
|
1265
|
-
### `src/core/array/FormArrayController.ts`
|
|
1266
|
-
|
|
1267
|
-
`FormArrayController` is the public command object returned by `form.array(path)`.
|
|
1268
|
-
|
|
1269
|
-
It holds:
|
|
1270
|
-
|
|
1271
|
-
- the shared `FormStateStore`
|
|
1272
|
-
- the shared `ArrayKeyGenerator`
|
|
1273
|
-
- the controlled array `FieldPath`
|
|
1274
|
-
- a `FormArrayMutationPlanner`
|
|
1275
|
-
|
|
1276
|
-
Each command follows the same pattern:
|
|
1277
|
-
|
|
1278
|
-
```txt
|
|
1279
|
-
read current array value
|
|
1280
|
-
read current keys
|
|
1281
|
-
ask planner for next values, keys, and index mapper
|
|
1282
|
-
if mutation exists, rebase the whole FormState
|
|
1283
|
-
```
|
|
1284
|
-
|
|
1285
|
-
`getArray()` reads the current value at the array path. If the value is not an array, it uses an empty array.
|
|
1286
|
-
|
|
1287
|
-
`getKeys()` reads stored `arrayKeys`; if none exist, it creates keys for the current array length.
|
|
1288
|
-
|
|
1289
|
-
### `src/core/array/FormArrayFactory.ts`
|
|
1290
|
-
|
|
1291
|
-
`FormArrayFactory` creates `FormArrayController` instances and shares one `ArrayKeyGenerator` for the form instance.
|
|
1292
|
-
|
|
1293
|
-
This allows repeated calls such as `form.array('items')` to create fresh controllers without resetting the key sequence.
|
|
1294
|
-
|
|
1295
|
-
### `src/core/array/FormArrayRebaser.ts`
|
|
1296
|
-
|
|
1297
|
-
`FormArrayRebaser` is the most important array component. It aligns values, field states, array keys, and submit count after an array mutation.
|
|
1298
|
-
|
|
1299
|
-
Detailed sequence:
|
|
1300
|
-
|
|
1301
|
-
1. Read known field paths from the current store.
|
|
1302
|
-
2. Reconstruct current values with `store.getValues()`.
|
|
1303
|
-
3. Write the next array into those values with `ValueHelper.setValueAtPath()`.
|
|
1304
|
-
4. Initialize a fresh `FormState` from the next values.
|
|
1305
|
-
5. Start with the freshly initialized fields.
|
|
1306
|
-
6. Iterate over previous fields.
|
|
1307
|
-
7. Preserve fields outside the changed array.
|
|
1308
|
-
8. For child fields inside the changed array, map the previous item index to the next item index.
|
|
1309
|
-
9. If the item still exists, replace the index in the child path and copy metadata to the new field.
|
|
1310
|
-
10. Write the next `arrayKeys` for the array path.
|
|
1311
|
-
11. Preserve `submitCount`.
|
|
1312
|
-
|
|
1313
|
-
Only metadata is copied for rebased child fields:
|
|
1314
|
-
|
|
1315
|
-
```txt
|
|
1316
|
-
errors
|
|
1317
|
-
touched
|
|
1318
|
-
dirty
|
|
1319
|
-
modified
|
|
1320
|
-
```
|
|
1321
|
-
|
|
1322
|
-
The value itself comes from the freshly initialized state based on the new values. This prevents stale values while preserving user interaction metadata.
|
|
892
|
+
Read in this order rather than following every import:
|
|
893
|
+
|
|
894
|
+
| Question | File under `src/core/` | Responsibility |
|
|
895
|
+
| --- | --- | --- |
|
|
896
|
+
| What happens after a public command? | `form/CreateForm.ts` | Normalize paths, write state, request validation. |
|
|
897
|
+
| Where does state change? | `state/FormStateStore.ts` | Own the store and apply writes with immer, including reset and submit flags. |
|
|
898
|
+
| How are values reconstructed? | `state/FormStateReader.ts` | Static calculations over an explicit snapshot; no store or getter ownership. |
|
|
899
|
+
| How is initial state built? | `state/FormStateInitializer.ts` | Traverse arrays/plain objects; keep empty objects, non-plain objects and primitive values as leaves. |
|
|
900
|
+
| What is an absent field? | `state/FieldStateFactory.ts` | Fresh metadata with undefined value, empty errors and false flags, including isFocused. |
|
|
901
|
+
| How do paths work? | `path/FormPath.ts` | Literal strings become one segment; tuple paths encode as JSON keys, with root key `$`. |
|
|
902
|
+
| How are nested values changed? | `value/ValueHelper.ts` | Immutable nested get/set and reconstruction from fields and array containers. |
|
|
903
|
+
| When may validation apply? | `validation/ValidationEngine.ts` | Local-schema precedence, selected/full execution and stale-result checks. |
|
|
904
|
+
| Which validation supersedes which? | `validation/ValidationSequencer.ts` | Full and per-field revisions, including schema registration invalidation. |
|
|
905
|
+
| Did values change during validation? | `validation/ValidationSnapshot.ts` | Capture and compare field values and array-key identities. |
|
|
906
|
+
| How are schema issues converted? | `validation/StandardSchemaValidator.ts` | Normalize Standard Schema issues; missing/unsupported paths become root errors. |
|
|
907
|
+
| How are submissions serialized? | `form/FormSubmitter.ts` | Queue submissions, retry stale validation and settle lifecycle state. |
|
|
908
|
+
| What is the next array order? | `array/FormArrayMutationPlanner.ts` | Static insert/push/remove/move/swap/replace calculations; no instance or store. |
|
|
909
|
+
| How does metadata follow items? | `array/FormArrayRebaser.ts` | Pure snapshot transformation with no store access or notifications. |
|
|
910
|
+
| Who applies an array mutation? | `array/FormArrayController.ts` | Read current array state and pass the updater snapshot to the rebaser. |
|
|
911
|
+
| Where do array keys come from? | `array/ArrayKeyGenerator.ts` | One generator per CreateForm, shared across newly created controllers. |
|
|
912
|
+
| How are indexes and paths mapped? | `array/ArrayItemReorder.ts`, `array/FormArrayPath.ts` | Pure ordering/index mapping and child-path replacement. |
|
|
913
|
+
|
|
914
|
+
`src/index.ts` and the framework entry points define the published surface.
|
|
915
|
+
`core/types.ts` describes the existing path, state, schema and command contracts.
|
|
916
|
+
Internal modules are implementation details, not additional package exports.
|
|
1323
917
|
|
|
1324
918
|
## Design decisions
|
|
1325
919
|
|
|
@@ -1358,7 +952,7 @@ pnpm typecheck
|
|
|
1358
952
|
pnpm test
|
|
1359
953
|
```
|
|
1360
954
|
|
|
1361
|
-
`pnpm build`
|
|
955
|
+
`pnpm build` uses tsup to bundle ESM JavaScript and declaration files for the core and four framework entry points. `pnpm test:pack` checks the packed package's runtime imports and TypeScript contracts in a separate consumer.
|
|
1362
956
|
|
|
1363
957
|
`pnpm test` runs the Vitest suite.
|
|
1364
958
|
|