Skip to content

Commit

Permalink
feat: add support for nullable nested fields (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
MAST1999 authored May 22, 2024
1 parent e85600c commit 70bdc7f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
39 changes: 39 additions & 0 deletions packages/form-core/src/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,45 @@ describe('form api', () => {
])
})

it('should validate optional object fields during submit', async () => {
const form = new FormApi({
defaultValues: {
person: null,
} as { person: { firstName: string; lastName: string } | null },
})

const field = new FieldApi({
form,
name: 'person.firstName',
validators: {
onChange: ({ value }) =>
value && value.length > 0 ? undefined : 'first name is required',
},
})

const lastNameField = new FieldApi({
form,
name: 'person.lastName',
validators: {
onChange: ({ value }) =>
value && value.length > 0 ? undefined : 'last name is required',
},
})

field.mount()
lastNameField.mount()

await form.handleSubmit()
expect(form.state.isFieldsValid).toEqual(false)
expect(form.state.canSubmit).toEqual(false)
expect(form.state.fieldMeta['person.firstName'].errors).toEqual([
'first name is required',
])
expect(form.state.fieldMeta['person.lastName'].errors).toEqual([
'last name is required',
])
})

it('should run all types of validation on fields during submit', async () => {
const form = new FormApi({
defaultValues: {
Expand Down
8 changes: 8 additions & 0 deletions packages/form-core/src/tests/util-types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ type NestedKeysExample = DeepValue<
>
assertType<number>(0 as never as NestedKeysExample)

type NestedNullableKeys = DeepValue<
{
meta: { mainUser: 'hello' } | null
},
'meta.mainUser'
>
assertType<'hello' | null>(0 as never as NestedNullableKeys)

type NestedArrayExample = DeepValue<{ users: User[] }, 'users[0].age'>
assertType<number>(0 as never as NestedArrayExample)

Expand Down
8 changes: 7 additions & 1 deletion packages/form-core/src/util-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
type Nullable<T> = T | null
type IsNullable<T> = [null] extends [T] ? true : false

export type RequiredByKey<T, K extends keyof T> = Omit<T, K> &
Required<Pick<T, K>>

Expand Down Expand Up @@ -96,6 +99,7 @@ export type DeepValue<
TValue,
// A string representing the path of the property we're trying to access
TAccessor,
TNullable extends boolean = IsNullable<TValue>,
> =
// If TValue is any it will recurse forever, this terminates the recursion
unknown extends TValue
Expand All @@ -122,7 +126,9 @@ export type DeepValue<
: TAccessor extends `${infer TBefore}.${infer TAfter}`
? DeepValue<DeepValue<TValue, TBefore>, TAfter>
: TAccessor extends string
? TValue[TAccessor]
? TNullable extends true
? Nullable<TValue[TAccessor]>
: TValue[TAccessor]
: never
: // Do not allow `TValue` to be anything else
never
1 change: 1 addition & 0 deletions packages/form-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function functionalUpdate<TInput, TOutput = TInput>(
export function getBy(obj: any, path: any) {
const pathObj = makePathArray(path)
return pathObj.reduce((current: any, pathPart: any) => {
if (current === null) return null
if (typeof current !== 'undefined') {
return current[pathPart]
}
Expand Down

0 comments on commit 70bdc7f

Please sign in to comment.