@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 CHANGED
@@ -818,507 +818,100 @@ Array 전체를 교체한다. 기존 item link를 의도적으로 끊기 때문
818
818
 
819
819
  ## Runtime flows
820
820
 
821
- ### Form creation flow
821
+ ### 필드 명령
822
822
 
823
- ```txt
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
- 핵심은 모든 collaborator가 같은 `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`와 `modified`는 의도적으로 분리되어 있다. Programmatic write는 field를 dirty하게 만들 수 있지만 user-modified로 표시하지 않을 수 있다.
851
-
852
- ### Blur flow
823
+ `src/core/form/CreateForm.ts`부터 읽으면 명령의 전체 순서를 볼 수 있다.
853
824
 
854
825
  ```txt
855
- form.blur(path)
856
- -> FormFieldCommands.blur()
857
- -> FormPath.pathInputToKey(path)
858
- -> FormStateStore.touchField(key)
859
- -> if validateOn includes 'blur': ValidationEngine.validateField(key, 'blur')
860
- -> otherwise return true
826
+ setValue -> 경로 정규화 -> store.setValue -> 필요하면 change 검증
827
+ blur -> 경로 정규화 -> unfocus -> touch -> 필요하면 blur 검증
828
+ trigger -> 경로 정규화 -> 지정 필드 또는 전체 검증
829
+ array -> form이 공유하는 키 생성기로 controller 생성
830
+ submit -> 제출 큐 -> 검증 -> callback -> 완료 처리
861
831
  ```
862
832
 
863
- ### Manual validation flow
864
-
865
- ```txt
866
- form.trigger(...paths)
867
- -> no paths: validateRegisteredFields('manual')
868
- -> with paths: validateFields(keys, 'manual')
869
- -> StandardSchemaValidator.validate(values)
870
- -> issue paths become PathKeys
871
- -> selected field errors are replaced
872
- ```
833
+ 상태 변경은 동기적으로 알린다. Blur의 unfocus와 touch 알림은 각각 유지한다.
834
+ `dirty`는 기본값과의 `Object.is` 비교 결과이고, `modified`는 사용자 입력으로
835
+ 변경되었는지를 기록한다. 두 flag의 의미는 다르다.
873
836
 
874
- Field-level validation도 full schema를 실행한 뒤 target field errors만 적용한다. 이렇게 하면 schema adapter가 단순하고 schema-library independent하게 유지된다.
837
+ ### 검증과 제출
875
838
 
876
- ### Submit flow
839
+ 지정 필드 검증은 local schema를 먼저 실행한 뒤 나머지 대상에 form schema를
840
+ 적용한다. 전체 검증은 form schema 이후 등록된 local schema를 실행한다.
841
+ Local 결과는 빈 오류 목록을 포함해 form schema 결과보다 우선한다. Local schema가
842
+ 없는 지정 필드 검증은 form 전체 schema를 실행하지만 지정 필드 오류만 기록한다.
877
843
 
878
- ```txt
879
- form.submit(onValid, onInvalid)
880
- -> increment submitCount
881
- -> validateRegisteredFields('submit')
882
- -> invalid: onInvalid(fields), return undefined
883
- -> valid: onValid(getValues())
884
- ```
844
+ 비동기 단계마다 검증 revision과 캡처한 값이 유효한지 확인한다. 서로 다른 필드의
845
+ 검증은 순서가 바뀌어 완료되어도 반영되지만, 겹치는 검증은 최신 결과를 덮어쓰면
846
+ 안 된다. 오류는 기존처럼 필드별로 기록하며 새로운 일괄 알림을 도입하지 않는다.
885
847
 
886
- `submitCount`는 successful submissions가 아니라 submit attempts를 기록한다.
848
+ `FormSubmitter`는 동시 제출을 큐에 넣는다. 시도 횟수는 즉시 증가시키고,
849
+ stale 검증은 다시 실행한다. Invalid이면 `onInvalid`, valid이면 현재 값으로
850
+ `onValid`를 호출하고, 대기 중인 제출이 모두 끝나면 submit 상태를 완료한다.
887
851
 
888
- ### Array mutation flow
852
+ ### 배열 변경
889
853
 
890
854
  ```txt
891
- form.array('items').move(1, 0)
892
- -> FormArrayController.move()
893
- -> read current array values and keys
894
- -> FormArrayMutationPlanner.move()
895
- -> next values
896
- -> next keys
897
- -> previous-index to next-index mapper
898
- -> FormArrayRebaser.rebase()
899
- -> write next array into values
900
- -> initialize a fresh FormState from next values
901
- -> preserve non-array fields
902
- -> move child metadata through the index mapper
903
- -> write next array keys
855
+ controller가 현재 값과 키를 읽음
856
+ -> 정적 planner가 { values, keys, mapPreviousIndex } 계산
857
+ -> store.replaceState(previousState => rebase(previousState, path, mutation))
858
+ -> 다음 snapshot 한 번 반영
904
859
  ```
905
860
 
906
- 이 흐름이 `items[1].name` metadata를 동일한 logical item에 붙인 채 그 item이 `items[0].name`으로 이동하게 만드는 핵심 기능이다.
861
+ Rebaser는 전달받은 snapshot에서 값을 복원하고 다음 상태를 초기화한 뒤,
862
+ 살아남은 자식 필드 메타데이터와 중첩 배열 키를 새 경로로 옮긴다. Errors, touched,
863
+ dirty, modified, focus는 item을 따라간다. 배열 밖 필드와 submit 상태는 보존한다.
864
+ Replace는 기존 자식 메타데이터를 버리고 새 키를 만든다. 기존 defaultValues
865
+ 동작도 그대로 유지한다.
907
866
 
908
867
  ## Internal architecture
909
868
 
910
869
  ```txt
911
- src/index.ts
912
- -> src/core/index.ts
913
- -> form/CreateForm.ts
914
- -> state/FormStateStore.ts
915
- -> state/FormStateInitializer.ts
916
- -> state/FormStateReader.ts
917
- -> state/FormStateWriter.ts
918
- -> form/FormFieldCommands.ts
919
- -> validation/ValidationEngine.ts
920
- -> validation/StandardSchemaValidator.ts
921
- -> form/FormSubmitter.ts
922
- -> array/FormArrayFactory.ts
923
- -> array/FormArrayController.ts
924
- -> array/FormArrayMutationPlanner.ts
925
- -> array/FormArrayRebaser.ts
926
- -> array/FormArrayPath.ts
927
- -> array/ArrayItemReorder.ts
928
- -> array/ArrayKeyGenerator.ts
929
- -> path/FormPath.ts
930
- -> value/ValueHelper.ts
931
- -> types.ts
932
- -> src/adapters/
933
- -> dom/FieldValue.ts
934
- -> dom/RegisterBinding.ts
935
- -> FormStateSummary.ts
936
- -> src/react/index.ts
937
- -> shared adapter logic 위의 React hook adapter
938
- -> src/vue/index.ts
939
- -> shared adapter logic 위의 Vue composable adapter
940
- -> src/solid/index.ts
941
- -> shared adapter logic 위의 Solid helper adapter
942
- -> src/svelte/index.ts
943
- -> shared adapter logic 위의 Svelte action adapter
944
- ```
945
-
946
- Responsibility summary:
947
-
948
- | Area | Responsibility |
949
- | --- | --- |
950
- | `form/` | Public orchestration: field commands, submit flow, array controller access. |
951
- | `state/` | Normalized state initialization, reading, writing, and store facade. |
952
- | `path/` | Conversion between public path input, tuple paths, and string path keys. |
953
- | `value/` | Immutable nested get/set and reconstruction of values from field states. |
954
- | `validation/` | Standard Schema execution and error normalization. |
955
- | `array/` | Array item key management, mutation planning, and child field metadata rebasing. |
956
- | `adapters/` | 내부 shared adapter logic: DOM value extraction/binding, form-state aggregation. |
957
- | `react/` | Public `Form` interface를 감싸는 React hook adapter. |
958
- | `vue/` | Public `Form` interface를 감싸는 Vue composable adapter. |
959
- | `solid/` | Public `Form` interface를 감싸는 Solid helper adapter. |
960
- | `svelte/` | Public `Form` interface를 감싸는 Svelte action adapter. |
961
- | `types.ts` | Public and internal TypeScript contracts. |
870
+ CreateForm 공개 명령 순서와 공유 배열 키
871
+ FormStateStore 상태 소유와 쓰기
872
+ FormStateInitializer 중첩 기본값 -> 정규화된 snapshot
873
+ FormStateReader snapshot -> 필드와 중첩 값
874
+ ValidationEngine schema 등록과 검증 단계
875
+ StandardSchemaValidator schema 결과 -> 필드 오류
876
+ ValidationSequencer 겹치는 검증의 revision
877
+ ValidationSnapshot 캡처한 값과 배열 키 비교
878
+ FormSubmitter 제출 큐와 callback 수명
879
+ FormArrayController 배열 읽기와 상태 반영
880
+ FormArrayMutationPlanner 순수 배열 변경 계산
881
+ FormArrayRebaser 이전 snapshot + 변경 결과 -> 다음 snapshot
882
+ ```
883
+
884
+ React, Vue, Solid, Svelte adapter는 공개 `Form` 계약에 의존한다.
885
+ `adapters/`는 DOM 값·바인딩과 상태 집계를 공유하고, 구독과 컴포넌트 수명 관리는
886
+ 각 framework 디렉터리에 남긴다.
962
887
 
963
888
  ## Core walkthrough
964
889
 
965
- ### `src/index.ts`
966
-
967
- Package root는 안정적인 framework-agnostic surface만 export한다.
968
-
969
- ```ts
970
- export { CreateForm } from './core/index';
971
- export type {
972
- CreateFormOptions,
973
- FieldPathInput,
974
- Form,
975
- FormError,
976
- StandardSchemaV1,
977
- } from './core/index';
978
- ```
979
-
980
- Internal state와 command helper 타입은 package root에서 내보내지 않는다. 이 문서에서는 구현 설명을 위해 다루지만, consumer는 `Form`, `CreateForm`, field path, errors, Standard Schema contract를 통해 사용해야 한다.
981
-
982
- ### `src/core/index.ts`
983
-
984
- 이 파일은 `types.ts`의 public types와 `CreateForm`을 re-export한다. Package root와 implementation folders 사이의 boundary다.
985
-
986
- ### `src/core/types.ts`
987
-
988
- `types.ts`는 core vocabulary를 정의한다.
989
-
990
- - public path type: `FieldPathInput`; internal path concepts: `FieldPathSegment`, `FieldPath`, `PathKey`
991
- - exported validation types: `FormError`, `StandardSchemaV1`; internal validation trigger type: `ValidationTrigger`
992
- - internal state types: `FieldState`, `ArrayKeys`, `FormState`
993
- - public API types: `CreateFormOptions`, `Form`; internal command helper types: `FormArray`, `SetValueOptions`
994
-
995
- 이 파일에서 가장 중요한 설계는 public string path가 literal field name이라는 점이다. Nested field에는 tuple path가 필요하다.
996
-
997
- ### `src/core/form/CreateForm.ts`
998
-
999
- `CreateForm`은 top-level controller이자 main public class다.
1000
-
1001
- 네 collaborator를 소유한다.
1002
-
1003
- ```txt
1004
- store -> state storage and state operations
1005
- fields -> setValue, blur, errors, trigger
1006
- arrays -> array(path) controller creation
1007
- submitter -> submitCount, validation, callbacks
1008
- ```
1009
-
1010
- Constructor는 하나의 `FormStateStore`를 만들고 모든 collaborator에 전달한다. 그래서 모든 command는 같은 snapshot 위에서 동작한다.
1011
-
1012
- 대부분의 method는 얇은 delegation이다.
1013
-
1014
- - `getState()`, `getFieldState()`, `getValue()`, `getValues()`, `reset()`은 `FormStateStore`에 위임한다.
1015
- - `setValue()`, `blur()`, `focus()`, `setErrors()`, `clearErrors()`, `trigger()`는 `FormFieldCommands`에 위임한다.
1016
- - `array()`는 path를 tuple로 변환하고 `FormArrayFactory`에 controller를 요청한다.
1017
- - `submit()`은 `FormSubmitter`에 위임한다.
1018
-
1019
- 이 구조는 public API를 stable하게 유지하면서 implementation responsibilities를 분리한다.
1020
-
1021
- ### `src/core/form/FormFieldCommands.ts`
1022
-
1023
- `FormFieldCommands`는 field-level operations를 묶는다.
1024
-
1025
- `setValue()`:
1026
-
1027
- 1. `FormPath.toFieldPath()`로 path를 tuple로 변환한다.
1028
- 2. `store.setValue()`로 value를 쓴다.
1029
- 3. 결과 `PathKey`를 받는다.
1030
- 4. `options.validate`가 true이거나 `validateOn`에 `'change'`가 있으면 change validation을 시작한다.
1031
-
1032
- `blur()`:
1033
-
1034
- 1. Public path를 `PathKey`로 변환한다.
1035
- 2. Field를 touched 처리한다.
1036
- 3. 설정된 경우에만 blur validation을 실행한다.
1037
-
1038
- `focus()`는 field의 `isFocused`를 `true`로 바꾼다. 대응되는 `blur()` command가 `isFocused`를 clearing하고 (`validateOn` 설정과 무관하게 항상), field를 `touched`로 표시한다. `isFocused` flag는 array rebasing 시 보존된다.
1039
-
1040
- `setErrors()`, `clearErrors()`, `trigger()`는 path를 key로 normalize하고 store 또는 validation engine에 위임한다.
1041
-
1042
- ### `src/core/form/FormSubmitter.ts`
1043
-
1044
- `FormSubmitter`는 submit-specific sequencing을 분리한다.
1045
-
1046
- ```txt
1047
- increment submitCount
1048
- validate all registered fields
1049
- if invalid -> call onInvalid(fields) and return undefined
1050
- if valid -> call onValid(getValues())
1051
- ```
1052
-
1053
- Submit은 multi-step command이므로 `CreateForm`이 validation branching까지 책임지지 않게 별도 class로 분리되어 있다.
1054
-
1055
- ### `src/core/state/FieldStateFactory.ts`
1056
-
1057
- `FieldStateFactory`는 default field state shape을 중앙화한다.
1058
-
1059
- ```ts
1060
- {
1061
- value: undefined,
1062
- errors: [],
1063
- touched: false,
1064
- dirty: false,
1065
- modified: false,
1066
- }
1067
- ```
1068
-
1069
- Shared mutable reference를 피하기 위해 매번 새 `errors` array를 만든다. 같은 factory는 initial leaf fields와 missing-field fallback에 모두 사용된다.
1070
-
1071
- ### `src/core/state/FormStateInitializer.ts`
1072
-
1073
- `FormStateInitializer.initialize(defaultValues)`는 nested values를 normalized `FormState`로 변환한다.
1074
-
1075
- Rules:
1076
-
1077
- 1. Arrays는 leaf field가 아니라 containers다.
1078
- 2. Array containers는 `initial-0`, `initial-1` 같은 `arrayKeys`를 받는다.
1079
- 3. Array items는 recursive하게 방문된다.
1080
- 4. Plain objects는 traverse된다.
1081
- 5. Empty plain object는 더 내려갈 field가 없으므로 leaf value로 저장된다.
1082
- 6. `Date`, class instance 같은 non-plain objects는 leaf value로 취급된다.
1083
- 7. Primitive와 `null`은 leaf value다.
1084
-
1085
- 그래서 `getState().fields`는 원래 nested object가 아니라 leaf paths를 가진다.
1086
-
1087
- ### `src/core/state/FormStateReader.ts`
1088
-
1089
- `FormStateReader`는 read-only derived operations를 담당한다.
1090
-
1091
- - `getKnownFieldPaths()`는 저장된 모든 `PathKey`를 tuple path로 되돌린다.
1092
- - `getFieldStateByKey()`는 existing field 또는 default `FieldState`를 반환한다.
1093
- - `getFieldState()`와 `getValue()`는 path-input convenience methods다.
1094
- - `getValues()`는 `ValueHelper.getValuesFromFields()`로 full nested value tree를 복원한다.
1095
- - `getValueAtPath()`는 먼저 values를 복원한 뒤 nested path를 읽는다.
1096
-
1097
- Reader는 store를 직접 소유하지 않고 snapshot getter를 받기 때문에 항상 최신 state를 읽는다.
1098
-
1099
- ### `src/core/state/FormStateWriter.ts`
1100
-
1101
- `FormStateWriter`는 모든 state mutation을 `immer`로 수행한다.
1102
-
1103
- > **번들 참고:** `immer`는 컨슈머 번들에 ~5KB를 추가한다. `FormState`가 flat `Record<PathKey, FieldState>` 구조이므로 spread 기반 업데이트(`{ ...state, [key]: nextField }`)로 immer를 대체해도 동작이 동일하다. 마이그레이션 전에 벤치마크하라 — immer는 구조 공유와 가독성 이점을 제공하며, array rebasing 경로에서는 크기 비용을 상회할 수 있다.
1104
-
1105
- `setValue()`:
1106
-
1107
- - Tuple path를 `PathKey`로 변환한다.
1108
- - 해당 tuple path의 initial value를 읽는다.
1109
- - 가능하면 existing field metadata를 보존한다.
1110
- - `value`를 교체한다.
1111
- - `Object.is`로 `dirty`를 계산한다.
1112
- - `source: 'user'`일 때만 `modified`를 설정한다.
1113
- - Validation이 재사용할 수 있도록 `PathKey`를 반환한다.
1114
-
1115
- Other methods:
1116
-
1117
- - `touchField()`는 `touched: true`를 설정한다.
1118
- - `setErrorsByKey()`는 error array를 교체한다.
1119
- - `clearErrors()`는 target fields 또는 existing fields 전체 errors를 비운다.
1120
- - `reset()`은 현재 또는 replacement initial values로 state를 다시 초기화한다.
1121
- - `incrementSubmitCount()`는 attempts를 증가시킨다.
1122
- - `replaceState()`는 array rebasing 같은 whole-state operation을 위한 escape hatch다.
1123
-
1124
- ### `src/core/state/FormStateStore.ts`
1125
-
1126
- `FormStateStore`는 core의 다른 부분들이 사용하는 facade다.
1127
-
1128
- 소유하는 것:
1129
-
1130
- - `@ilokesto/store`의 `Store<FormState<TValues>>`
1131
- - `FormStateReader`
1132
- - `FormStateWriter`
1133
-
1134
- Collaborator들은 operation이 reader, writer, underlying store 중 어디에서 구현되는지 알 필요가 없다. Facade만 호출하면 된다.
1135
-
1136
- ### `src/core/path/FormPath.ts`
1137
-
1138
- `FormPath`는 path conversion rules를 정의한다.
1139
-
1140
- Key methods:
1141
-
1142
- - `path(...segments)`: tuple path helper.
1143
- - `toFieldPath(input)`: string은 `[input]`이 되고 tuple path는 그대로 통과한다.
1144
- - `pathInputToKey(input)`: public path input을 `PathKey`로 변환한다.
1145
- - `pathToKey(path)`: `[]`는 `$`, 나머지는 tuple을 JSON stringify한다.
1146
- - `keyToPath(key)`: key를 tuple segments로 parse하고 validate한다.
1147
-
1148
- JSON encoding이 path collision을 방지한다.
1149
-
1150
- > **성능 참고:** `pathToKey`는 `JSON.stringify`를, `keyToPath`는 `JSON.parse`를 사용한다. 큰 폼(수백 필드)에서 잦은 업데이트 시 NUL separator 기반 커스텀 인코딩으로 hot path 오버헤드를 줄일 수 있다. 마이그레이션 전에 벤치마크하라 — 현재 접근은 정확하고 읽기 쉬우며 실제 영향은 보통 미미하다.
1151
-
1152
- ### `src/core/value/ValueHelper.ts`
1153
-
1154
- `ValueHelper`는 immutable nested value operations를 제공한다.
1155
-
1156
- `getValueAtPath(source, path)`는 object/array를 안전하게 walk한다. 중간 value가 nullish이거나 primitive이면 `undefined`를 반환한다.
1157
-
1158
- `setValueAtPath(source, path, value)`는 new root value를 반환하고 path를 따라 container를 shallow-clone한다. Missing container는 next segment로 추론한다. Number면 array를 만들고 string이면 object를 만든다.
1159
-
1160
- `getValuesFromFields(state, fieldPaths)`는 public values를 두 단계로 복원한다.
1161
-
1162
- 1. `arrayKeys`에서 empty array containers를 만든다.
1163
- 2. 모든 leaf field value를 tuple path 위치에 쓴다.
1164
-
1165
- 이 과정을 통해 normalized `fields`가 다시 nested object가 된다.
1166
-
1167
- ### `src/core/validation/ValidationEngine.ts`
1168
-
1169
- `ValidationEngine`은 validation triggers와 store writes를 조율한다.
1170
-
1171
- - Default `validateOn`은 `['submit']`이다.
1172
- - Schema가 없으면 validation은 error 없이 성공한다.
1173
- - `validateField(key, trigger)`는 full schema를 실행하고 해당 field errors만 적용한다.
1174
- - `validateFields(keys, trigger)`는 full schema를 실행하고 selected fields errors만 적용한다.
1175
- - `validateRegisteredFields(trigger)`는 full schema를 실행하고 current fields와 schema error keys 전체를 update한다.
1176
-
1177
- Engine은 schema를 호출할 때 trigger value를 전달하지 않는다. Trigger는 engine이 언제 실행될지를 제어하지 schema API를 바꾸지 않는다.
1178
-
1179
- `ValidationSequencer`는 full-form revision과 field별 revision을 추적한다. 겹치는 작업만 stale 처리하고 독립 field 작업은 유지하며, error나 submit callback을 적용하기 전에 captured field-value 및 array-shape snapshot이 최신인지 확인한다.
1180
-
1181
- ### `src/core/validation/StandardSchemaValidator.ts`
1182
-
1183
- `StandardSchemaValidator`는 Standard Schema result를 core errors로 adapt한다.
1184
-
1185
- Success:
1186
-
1187
- ```ts
1188
- { valid: true, errorsByKey: {} }
1189
- ```
1190
-
1191
- Failure:
1192
-
1193
- ```ts
1194
- {
1195
- valid: false,
1196
- errorsByKey: {
1197
- '["email"]': [{ type: 'standard_schema', message: 'Email is invalid' }],
1198
- },
1199
- }
1200
- ```
1201
-
1202
- Issue path conversion rules:
1203
-
1204
- - Missing path 또는 empty path는 root path `[]`와 key `$`가 된다.
1205
- - String과 number path segment는 그대로 보존된다.
1206
- - Object path segment는 `.key` property를 사용한다.
1207
- - Symbol 같은 unsupported key는 root error로 fallback된다.
1208
-
1209
- ### `src/core/array/ArrayKeyGenerator.ts`
1210
-
1211
- `ArrayKeyGenerator`는 runtime item keys를 만든다.
1212
-
1213
- ```txt
1214
- item-1
1215
- item-2
1216
- item-3
1217
- ```
1218
-
1219
- 하나의 `FormArrayFactory`는 form instance마다 하나의 generator를 공유한다. 이렇게 하면 여러 controller 사이에서 accidental key collision 가능성을 줄인다.
1220
-
1221
- ### `src/core/array/ArrayItemReorder.ts`
1222
-
1223
- `ArrayItemReorder`는 array order operations를 위한 pure helper다.
1224
-
1225
- - `moveItem(items, from, to)`는 reordered copy를 반환한다.
1226
- - `swapItems(items, left, right)`는 swapped copy를 반환한다.
1227
- - `createIndexMapper(previousLength, nextOrder)`는 previous index를 next index로 mapping하는 function을 반환한다.
1228
-
1229
- `nextOrder`는 "각 next position에는 어떤 previous index가 있는가?"로 표현된다. New item은 `-1`을 사용하고 removed item은 아예 등장하지 않는다.
1230
-
1231
- ### `src/core/array/FormArrayMutationPlanner.ts`
1232
-
1233
- `FormArrayMutationPlanner`는 store를 몰라도 array mutations를 계산한다.
1234
-
1235
- 각 mutation은 다음 형태를 반환한다.
1236
-
1237
- ```ts
1238
- type FormArrayMutation = {
1239
- values: readonly unknown[];
1240
- keys: readonly string[];
1241
- mapPreviousIndex: (index: number) => number | undefined;
1242
- };
1243
- ```
1244
-
1245
- Command behavior:
1246
-
1247
- - `insert()`는 target index를 보정하고 새 value/key를 삽입한다. New item은 `-1`에서 온 것으로 처리되어 previous metadata가 붙지 않는다.
1248
- - `push()`는 value/key를 끝에 추가한다.
1249
- - `remove()`는 value/key를 제거하고 removed index는 `undefined`로 map한다.
1250
- - `move()`는 같은 rule로 values와 keys를 이동한다.
1251
- - `swap()`은 같은 rule로 values와 keys를 교환한다.
1252
- - `replace()`는 모두 새 values/keys를 반환하고 모든 previous index를 `undefined`로 map한다.
1253
-
1254
- 이 planner는 pure하기 때문에 store와 독립적으로 test하기 쉽다.
1255
-
1256
- ### `src/core/array/FormArrayPath.ts`
1257
-
1258
- `FormArrayPath`는 rebasing 중 필요한 path helpers를 담는다.
1259
-
1260
- - `hasPathPrefix(fieldPath, prefix)`는 tuple prefix equality를 확인한다.
1261
- - `isArrayChildPath(fieldPath, arrayPath)`는 `['items']` 아래의 `['items', 0, 'name']`처럼 field가 array item 아래에 있는지 확인한다.
1262
- - `replaceArrayIndex(fieldPath, arrayPath, nextIndex)`는 item index가 교체된 새 child path를 만든다.
1263
-
1264
- ### `src/core/array/FormArrayController.ts`
1265
-
1266
- `FormArrayController`는 `form.array(path)`가 반환하는 public command object다.
1267
-
1268
- 보유하는 것:
1269
-
1270
- - shared `FormStateStore`
1271
- - shared `ArrayKeyGenerator`
1272
- - controlled array `FieldPath`
1273
- - `FormArrayMutationPlanner`
1274
-
1275
- 각 command는 같은 pattern을 따른다.
1276
-
1277
- ```txt
1278
- read current array value
1279
- read current keys
1280
- ask planner for next values, keys, and index mapper
1281
- if mutation exists, rebase the whole FormState
1282
- ```
1283
-
1284
- `getArray()`는 array path의 현재 value를 읽는다. Value가 array가 아니면 empty array를 사용한다.
1285
-
1286
- `getKeys()`는 stored `arrayKeys`를 읽는다. 없으면 current array length만큼 key를 만든다.
1287
-
1288
- ### `src/core/array/FormArrayFactory.ts`
1289
-
1290
- `FormArrayFactory`는 `FormArrayController` instances를 만들고 form instance를 위한 하나의 `ArrayKeyGenerator`를 공유한다.
1291
-
1292
- 이 덕분에 `form.array('items')`를 반복 호출해서 fresh controller를 만들어도 key sequence가 reset되지 않는다.
1293
-
1294
- ### `src/core/array/FormArrayRebaser.ts`
1295
-
1296
- `FormArrayRebaser`는 가장 중요한 array component다. Array mutation 뒤 values, field states, array keys, submit count를 정렬한다.
1297
-
1298
- Detailed sequence:
1299
-
1300
- 1. Current store에서 known field paths를 읽는다.
1301
- 2. `store.getValues()`로 current values를 복원한다.
1302
- 3. `ValueHelper.setValueAtPath()`로 next array를 values에 쓴다.
1303
- 4. Next values에서 fresh `FormState`를 initialize한다.
1304
- 5. Freshly initialized fields에서 시작한다.
1305
- 6. Previous fields를 순회한다.
1306
- 7. Changed array 밖의 fields는 보존한다.
1307
- 8. Changed array 안의 child fields는 previous item index를 next item index로 mapping한다.
1308
- 9. Item이 아직 존재하면 child path의 index를 교체하고 metadata를 new field로 copy한다.
1309
- 10. Array path에 next `arrayKeys`를 쓴다.
1310
- 11. `submitCount`를 보존한다.
1311
-
1312
- Rebased child field에 copy되는 것은 metadata뿐이다.
1313
-
1314
- ```txt
1315
- errors
1316
- touched
1317
- dirty
1318
- modified
1319
- ```
1320
-
1321
- Value 자체는 next values로부터 freshly initialized state에서 온다. 그래서 stale value를 방지하면서 user interaction metadata는 보존한다.
890
+ 모든 import를 따라가기보다 아래 순서로 읽으면 된다.
891
+
892
+ | 알고 싶은 내용 | `src/core/` 아래 파일 | 책임 |
893
+ | --- | --- | --- |
894
+ | 공개 명령 다음에 무슨 일이 일어나는가? | `form/CreateForm.ts` | 경로 변환, 상태 변경, 검증 요청. |
895
+ | 상태는 어디서 바뀌는가? | `state/FormStateStore.ts` | store 소유와 immer 쓰기, reset·submit flag 반영. |
896
+ | 값을 어떻게 복원하는가? | `state/FormStateReader.ts` | 명시적으로 받은 snapshot의 정적 계산. Store나 getter를 소유하지 않는다. |
897
+ | 초기 상태는 어떻게 만드는가? | `state/FormStateInitializer.ts` | 배열과 일반 객체를 순회하고 빈 객체·특수 객체·원시값은 leaf로 유지. |
898
+ | 없는 필드는 어떻게 읽는가? | `state/FieldStateFactory.ts` | undefined 값, 새 오류 배열, isFocused를 포함한 false flag 생성. |
899
+ | 경로는 어떻게 표현하는가? | `path/FormPath.ts` | 문자열은 단일 segment, tuple은 JSON key, root는 `$`. |
900
+ | 중첩 값을 어떻게 변경하는가? | `value/ValueHelper.ts` | 불변 nested get/set과 fields·배열 container에서 값 복원. |
901
+ | 검증 결과는 언제 적용하는가? | `validation/ValidationEngine.ts` | Local schema 우선순위, 지정·전체 검증 실행, stale 판별. |
902
+ | 어느 검증이 이전 검증을 대체하는가? | `validation/ValidationSequencer.ts` | 전체·필드별 revision과 schema 등록에 따른 무효화. |
903
+ | 검증 중 값이 달라졌는가? | `validation/ValidationSnapshot.ts` | 필드 값과 배열 키 identity 캡처·비교. |
904
+ | Schema issue는 어떻게 변환하는가? | `validation/StandardSchemaValidator.ts` | Standard Schema issue 정규화. 없거나 지원하지 않는 경로는 root 오류. |
905
+ | 제출 순서는 어떻게 보장하는가? | `form/FormSubmitter.ts` | 제출 큐, stale 검증 재시도, 완료 상태 처리. |
906
+ | 다음 배열 순서는 무엇인가? | `array/FormArrayMutationPlanner.ts` | 인스턴스나 store 없이 정적 insert/push/remove/move/swap/replace 계산. |
907
+ | 메타데이터는 어떻게 item을 따라가는가? | `array/FormArrayRebaser.ts` | Store 접근이나 알림 없는 순수 snapshot 변환. |
908
+ | 배열 변경은 누가 반영하는가? | `array/FormArrayController.ts` | 배열 상태를 읽고 updater의 snapshot을 rebaser에 전달. |
909
+ | 배열 키는 어디서 만드는가? | `array/ArrayKeyGenerator.ts` | CreateForm마다 하나를 소유하고 새 controller들과 공유. |
910
+ | Index와 경로는 어떻게 옮기는가? | `array/ArrayItemReorder.ts`, `array/FormArrayPath.ts` | 순수 정렬·index mapping·자식 경로 교체. |
911
+
912
+ 공개 surface는 `src/index.ts`와 framework entry point가 정의한다.
913
+ `core/types.ts`는 기존 경로·상태·schema·명령 계약을 설명한다.
914
+ 내부 모듈은 구현 상세이며 새로운 package export가 아니다.
1322
915
 
1323
916
  ## Design decisions
1324
917
 
@@ -1357,7 +950,7 @@ pnpm typecheck
1357
950
  pnpm test
1358
951
  ```
1359
952
 
1360
- `pnpm build`는 TypeScript로 declaration files를 emit하고, NodeNext 호환성을 위해 declaration-file relative specifier만 보정한 뒤, Vite로 ESM JavaScript를 bundle한다. 그래서 source import는 extensionless로 유지하면서도 `dist/index.js`, `dist/react/index.js`, `dist/vue/index.js`, `dist/solid/index.js`, `dist/svelte/index.js` 같은 adapter subpath는 ESM runtime에서 직접 import할 수 있다.
953
+ `pnpm build`는 tsup으로 core와 네 framework entry point의 ESM JavaScript 및 declaration files를 bundle한다. `pnpm test:pack`은 별도 consumer에서 packed package의 runtime import와 TypeScript 계약을 확인한다.
1361
954
 
1362
955
  `pnpm test`는 Vitest suite를 실행한다.
1363
956