Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for nullable objects. #713

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading