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

[WIP] [FEAT] Allow returning anything in a validator, not just a string #1104

Draft
wants to merge 46 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
44a71cf
chore: WIP adding in inferencing to validator's return type
crutchcorn Jan 9, 2025
d9ef915
chore: more work on fixing types
crutchcorn Jan 9, 2025
8b87404
chore: wip on things more
crutchcorn Jan 9, 2025
d2544c7
chore: fix issues with FormApi and FieldApi
crutchcorn Jan 9, 2025
9c1caef
chore: add more fields to infer
crutchcorn Jan 9, 2025
901efb3
chore: change typed from unknown to undefined
crutchcorn Jan 9, 2025
9b18dbd
chore: more fixes
crutchcorn Jan 9, 2025
97767f4
chore: add some new type tests
crutchcorn Jan 9, 2025
5263c16
chore: add more type tests
crutchcorn Jan 9, 2025
e321a09
ci: apply automated fixes and generate docs
autofix-ci[bot] Jan 9, 2025
05e3e99
chore: migrate React components to use new prop types
crutchcorn Jan 9, 2025
00963bd
chore: move useTransform hook to new types
crutchcorn Jan 9, 2025
be575af
chore: add convinience type for AnyFormAPI and AnyFieldAPI
crutchcorn Jan 9, 2025
f2e0d00
ci: apply automated fixes and generate docs
autofix-ci[bot] Jan 9, 2025
6de25f3
chore: fix NextJS and Remix adapters
crutchcorn Jan 9, 2025
65c31b6
chore: fix build of Start adapter
crutchcorn Jan 9, 2025
e902f69
ci: apply automated fixes and generate docs
autofix-ci[bot] Jan 9, 2025
0035012
ci: apply automated fixes and generate docs
autofix-ci[bot] Jan 9, 2025
64cfbe0
chore: fix issues with Nx on Windows
crutchcorn Jan 10, 2025
0c7cdbe
chore: fix issues with FieldAny in examples
crutchcorn Jan 10, 2025
d3758c0
chore: upgrade React packages to stable
crutchcorn Jan 10, 2025
1a196a5
chore: WIP attempt to fix Start package
crutchcorn Jan 10, 2025
29c25b3
chore: fix mergeForm typing
crutchcorn Jan 10, 2025
b91fc72
ci: apply automated fixes and generate docs
autofix-ci[bot] Jan 10, 2025
5d21496
chore: migrate Angular adapter to use new API
crutchcorn Jan 10, 2025
035ae35
ci: apply automated fixes and generate docs
autofix-ci[bot] Jan 10, 2025
70a644c
chore: migrate Lit adapter to new generics
crutchcorn Jan 11, 2025
eb942ec
ci: apply automated fixes and generate docs
autofix-ci[bot] Jan 11, 2025
98fd587
chore: attempt 1 at fixing Vue's types
crutchcorn Jan 11, 2025
074f9a4
chore: fix Vue types for JSX and Vue SFCs alike
crutchcorn Jan 11, 2025
2e9f16d
chore: migrate Vue type to use new generics
crutchcorn Jan 11, 2025
ae2609e
chore: fix Vue test types
crutchcorn Jan 11, 2025
5600797
chore: fix Vue examples
crutchcorn Jan 11, 2025
af991ec
ci: apply automated fixes and generate docs
autofix-ci[bot] Jan 11, 2025
4604b41
chore: migrate Solid to new generics system
crutchcorn Jan 11, 2025
ac698c8
chore: fix Solid examples
crutchcorn Jan 11, 2025
c9ea6cf
ci: apply automated fixes and generate docs
autofix-ci[bot] Jan 11, 2025
69c1897
ci: apply automated fixes and generate docs (attempt 2/3)
autofix-ci[bot] Jan 11, 2025
1798a88
chore: fix issues with Solid library
crutchcorn Jan 12, 2025
4138729
chore: fix knip
crutchcorn Jan 12, 2025
1b46420
chore: fix issues with error casting
crutchcorn Jan 12, 2025
56f813a
chore: fix issue with ESLint
crutchcorn Jan 12, 2025
a5d41db
docs: show errorMap and errorarray
crutchcorn Jan 12, 2025
57198cb
Merge branch 'main' into return-anything-not-just-string
crutchcorn Jan 15, 2025
dcd8e3b
chore: upgrade all deps
crutchcorn Jan 15, 2025
0d40105
ci: apply automated fixes and generate docs
autofix-ci[bot] Jan 15, 2025
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
36 changes: 36 additions & 0 deletions docs/framework/angular/guides/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,42 @@ export class AppComponent {
}
```

It's worth mentioning that our `errors` array and the `errorMap` matches the types returned by the validators. This means that:

```angular-ts
@Component({
selector: 'app-root',
standalone: true,
imports: [TanStackField],
template: `
<ng-container
[tanstackField]="form"
name="age"
[validators]="{
onChange: ageValidator
}"
#age="field"
>
<!-- ... -->
<!-- errorMap.onChange is type `{isOldEnough: false} | undefined` -->
<!-- meta.errors is type `Array<{isOldEnough: false} | undefined>` -->
@if (!age.api.state.meta.errorMap['onChange']?.isOldEnough) {
<em role="alert">The user is not old enough</em>
}
</ng-container>
`,
})
export class AppComponent {
ageValidator: FieldValidateFn<any, any, any, any, number> = ({ value }) =>
value < 13 ? 'You must be 13 to make an account' : undefined

// ...
}
```




## Validation at field level vs at form level

As shown above, each `[tanstackField]` accepts its own validation rules via the `onChange`, `onBlur` etc... callbacks. It is also possible to define validation rules at the form level (as opposed to field by field) by passing similar callbacks to the `injectForm()` function.
Expand Down
74 changes: 53 additions & 21 deletions docs/framework/angular/reference/classes/tanstackfield.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ id: TanStackField
title: TanStackField
---

# Class: TanStackField\<TParentData, TName, TFieldValidator, TFormValidator, TData\>
<!-- DO NOT EDIT: this page is autogenerated from the type comments -->

# Class: TanStackField\<TParentData, TName, TFieldValidator, TFormValidator, TData, TOnMountReturn, TOnChangeReturn, TOnChangeAsyncReturn, TOnBlurReturn, TOnBlurAsyncReturn, TOnSubmitReturn, TOnSubmitAsyncReturn, TFormOnMountReturn, TFormOnChangeReturn, TFormOnChangeAsyncReturn, TFormOnBlurReturn, TFormOnBlurAsyncReturn, TFormOnSubmitReturn, TFormOnSubmitAsyncReturn, TFormOnServerReturn\>

Defined in: [tanstack-field.directive.ts:25](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L25)

Expand All @@ -21,34 +23,64 @@ Defined in: [tanstack-field.directive.ts:25](https://github.com/TanStack/form/bl

• **TData** *extends* `DeepValue`\<`TParentData`, `TName`\> = `DeepValue`\<`TParentData`, `TName`\>

• **TOnMountReturn** = `undefined`

• **TOnChangeReturn** = `undefined`

• **TOnChangeAsyncReturn** = `undefined`

• **TOnBlurReturn** = `undefined`

• **TOnBlurAsyncReturn** = `undefined`

• **TOnSubmitReturn** = `undefined`

• **TOnSubmitAsyncReturn** = `undefined`

• **TFormOnMountReturn** = `undefined`

• **TFormOnChangeReturn** = `undefined`

• **TFormOnChangeAsyncReturn** = `undefined`

• **TFormOnBlurReturn** = `undefined`

• **TFormOnBlurAsyncReturn** = `undefined`

• **TFormOnSubmitReturn** = `undefined`

• **TFormOnSubmitAsyncReturn** = `undefined`

• **TFormOnServerReturn** = `undefined`

## Implements

- `OnInit`
- `OnChanges`
- `OnDestroy`
- `FieldOptions`\<`TParentData`, `TName`, `TFieldValidator`, `TFormValidator`, `TData`\>
- `FieldOptions`\<`TParentData`, `TName`, `TFieldValidator`, `TFormValidator`, `TData`, `TOnMountReturn`, `TOnChangeReturn`, `TOnChangeAsyncReturn`, `TOnBlurReturn`, `TOnBlurAsyncReturn`, `TOnSubmitReturn`, `TOnSubmitAsyncReturn`\>

## Constructors

### new TanStackField()

```ts
new TanStackField<TParentData, TName, TFieldValidator, TFormValidator, TData>(): TanStackField<TParentData, TName, TFieldValidator, TFormValidator, TData>
new TanStackField<TParentData, TName, TFieldValidator, TFormValidator, TData, TOnMountReturn, TOnChangeReturn, TOnChangeAsyncReturn, TOnBlurReturn, TOnBlurAsyncReturn, TOnSubmitReturn, TOnSubmitAsyncReturn, TFormOnMountReturn, TFormOnChangeReturn, TFormOnChangeAsyncReturn, TFormOnBlurReturn, TFormOnBlurAsyncReturn, TFormOnSubmitReturn, TFormOnSubmitAsyncReturn, TFormOnServerReturn>(): TanStackField<TParentData, TName, TFieldValidator, TFormValidator, TData, TOnMountReturn, TOnChangeReturn, TOnChangeAsyncReturn, TOnBlurReturn, TOnBlurAsyncReturn, TOnSubmitReturn, TOnSubmitAsyncReturn, TFormOnMountReturn, TFormOnChangeReturn, TFormOnChangeAsyncReturn, TFormOnBlurReturn, TFormOnBlurAsyncReturn, TFormOnSubmitReturn, TFormOnSubmitAsyncReturn, TFormOnServerReturn>
```

#### Returns

[`TanStackField`](tanstackfield.md)\<`TParentData`, `TName`, `TFieldValidator`, `TFormValidator`, `TData`\>
[`TanStackField`](tanstackfield.md)\<`TParentData`, `TName`, `TFieldValidator`, `TFormValidator`, `TData`, `TOnMountReturn`, `TOnChangeReturn`, `TOnChangeAsyncReturn`, `TOnBlurReturn`, `TOnBlurAsyncReturn`, `TOnSubmitReturn`, `TOnSubmitAsyncReturn`, `TFormOnMountReturn`, `TFormOnChangeReturn`, `TFormOnChangeAsyncReturn`, `TFormOnBlurReturn`, `TFormOnBlurAsyncReturn`, `TFormOnSubmitReturn`, `TFormOnSubmitAsyncReturn`, `TFormOnServerReturn`\>

## Properties

### api

```ts
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData, TOnMountReturn, TOnChangeReturn, TOnChangeAsyncReturn, TOnBlurReturn, TOnBlurAsyncReturn, TOnSubmitReturn, TOnSubmitAsyncReturn, TFormOnMountReturn, TFormOnChangeReturn, TFormOnChangeAsyncReturn, TFormOnBlurReturn, TFormOnBlurAsyncReturn, TFormOnSubmitReturn, TFormOnSubmitAsyncReturn, TFormOnServerReturn>;
```

Defined in: [tanstack-field.directive.ts:62](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L62)
Defined in: [tanstack-field.directive.ts:111](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L111)

***

Expand All @@ -58,7 +90,7 @@ Defined in: [tanstack-field.directive.ts:62](https://github.com/TanStack/form/bl
optional asyncAlways: boolean;
```

Defined in: [tanstack-field.directive.ts:48](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L48)
Defined in: [tanstack-field.directive.ts:76](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L76)

If `true`, always run async validation, even if there are errors emitted during synchronous validation.

Expand All @@ -76,7 +108,7 @@ FieldOptions.asyncAlways
optional asyncDebounceMs: number;
```

Defined in: [tanstack-field.directive.ts:47](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L47)
Defined in: [tanstack-field.directive.ts:75](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L75)

The default time to debounce async validation if there is not a more specific debounce time passed.

Expand All @@ -94,7 +126,7 @@ FieldOptions.asyncDebounceMs
optional defaultMeta: Partial<FieldMeta>;
```

Defined in: [tanstack-field.directive.ts:60](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L60)
Defined in: [tanstack-field.directive.ts:109](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L109)

An optional object with default metadata for the field.

Expand All @@ -112,7 +144,7 @@ FieldOptions.defaultMeta
optional defaultValue: NoInfer<TData>;
```

Defined in: [tanstack-field.directive.ts:46](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L46)
Defined in: [tanstack-field.directive.ts:74](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L74)

An optional default value for the field.

Expand All @@ -130,7 +162,7 @@ FieldOptions.defaultValue
optional listeners: NoInfer<FieldListeners<TParentData, TName, TFieldValidator, TFormValidator, TData>>;
```

Defined in: [tanstack-field.directive.ts:57](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L57)
Defined in: [tanstack-field.directive.ts:106](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L106)

A list of listeners which attach to the corresponding events

Expand All @@ -148,7 +180,7 @@ FieldOptions.listeners
name: TName;
```

Defined in: [tanstack-field.directive.ts:42](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L42)
Defined in: [tanstack-field.directive.ts:70](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L70)

The field name. The type will be `DeepKeys<TParentData>` to ensure your name is a deep key of the parent dataset.

Expand All @@ -163,10 +195,10 @@ FieldOptions.name
### tanstackField

```ts
tanstackField: FormApi<TParentData, TFormValidator>;
tanstackField: FormApi<TParentData, TFormValidator, TFormOnMountReturn, TFormOnChangeReturn, TFormOnChangeAsyncReturn, TFormOnBlurReturn, TFormOnBlurAsyncReturn, TFormOnSubmitReturn, TFormOnSubmitAsyncReturn, TFormOnServerReturn>;
```

Defined in: [tanstack-field.directive.ts:50](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L50)
Defined in: [tanstack-field.directive.ts:78](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L78)

***

Expand All @@ -176,7 +208,7 @@ Defined in: [tanstack-field.directive.ts:50](https://github.com/TanStack/form/bl
optional unmount: () => void;
```

Defined in: [tanstack-field.directive.ts:78](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L78)
Defined in: [tanstack-field.directive.ts:169](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L169)

#### Returns

Expand All @@ -190,7 +222,7 @@ Defined in: [tanstack-field.directive.ts:78](https://github.com/TanStack/form/bl
optional validatorAdapter: TFieldValidator;
```

Defined in: [tanstack-field.directive.ts:49](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L49)
Defined in: [tanstack-field.directive.ts:77](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L77)

A validator provided by an extension, like `yupValidator` from `@tanstack/yup-form-adapter`

Expand All @@ -205,10 +237,10 @@ FieldOptions.validatorAdapter
### validators?

```ts
optional validators: NoInfer<FieldValidators<TParentData, TName, TFieldValidator, TFormValidator, TData>>;
optional validators: NoInfer<FieldValidators<TParentData, TName, TFieldValidator, TFormValidator, TData, TOnMountReturn, TOnChangeReturn, TOnChangeAsyncReturn, TOnBlurReturn, TOnBlurAsyncReturn, TOnSubmitReturn, TOnSubmitAsyncReturn>>;
```

Defined in: [tanstack-field.directive.ts:54](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L54)
Defined in: [tanstack-field.directive.ts:90](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L90)

A list of validators to pass to the field

Expand All @@ -226,7 +258,7 @@ FieldOptions.validators
ngOnChanges(): void
```

Defined in: [tanstack-field.directive.ts:90](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L90)
Defined in: [tanstack-field.directive.ts:181](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L181)

A callback method that is invoked immediately after the
default change detector has checked data-bound properties
Expand All @@ -251,7 +283,7 @@ OnChanges.ngOnChanges
ngOnDestroy(): void
```

Defined in: [tanstack-field.directive.ts:86](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L86)
Defined in: [tanstack-field.directive.ts:177](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L177)

A callback method that performs custom clean-up, invoked immediately
before a directive, pipe, or service instance is destroyed.
Expand All @@ -274,7 +306,7 @@ OnDestroy.ngOnDestroy
ngOnInit(): void
```

Defined in: [tanstack-field.directive.ts:80](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L80)
Defined in: [tanstack-field.directive.ts:171](https://github.com/TanStack/form/blob/main/packages/angular-form/src/tanstack-field.directive.ts#L171)

A callback method that is invoked immediately after the
default change detector has checked the directive's
Expand Down
24 changes: 21 additions & 3 deletions docs/framework/angular/reference/functions/injectform.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ id: injectForm
title: injectForm
---

<!-- DO NOT EDIT: this page is autogenerated from the type comments -->

# Function: injectForm()

```ts
function injectForm<TFormData, TFormValidator>(opts?): FormApi<TFormData, TFormValidator>
function injectForm<TFormData, TFormValidator, TOnMountReturn, TOnChangeReturn, TOnChangeAsyncReturn, TOnBlurReturn, TOnBlurAsyncReturn, TOnSubmitReturn, TOnSubmitAsyncReturn, TOnServerReturn>(opts?): FormApi<TFormData, TFormValidator, TOnMountReturn, TOnChangeReturn, TOnChangeAsyncReturn, TOnBlurReturn, TOnBlurAsyncReturn, TOnSubmitReturn, TOnSubmitAsyncReturn, TOnServerReturn>
```

Defined in: [inject-form.ts:5](https://github.com/TanStack/form/blob/main/packages/angular-form/src/inject-form.ts#L5)
Expand All @@ -17,12 +19,28 @@ Defined in: [inject-form.ts:5](https://github.com/TanStack/form/blob/main/packag

• **TFormValidator** *extends* `undefined` \| `Validator`\<`TFormData`, `unknown`\> = `undefined`

• **TOnMountReturn** = `undefined`

• **TOnChangeReturn** = `undefined`

• **TOnChangeAsyncReturn** = `undefined`

• **TOnBlurReturn** = `undefined`

• **TOnBlurAsyncReturn** = `undefined`

• **TOnSubmitReturn** = `undefined`

• **TOnSubmitAsyncReturn** = `undefined`

• **TOnServerReturn** = `undefined`

## Parameters

### opts?

`FormOptions`\<`TFormData`, `TFormValidator`\>
`FormOptions`\<`TFormData`, `TFormValidator`, `TOnMountReturn`, `TOnChangeReturn`, `TOnChangeAsyncReturn`, `TOnBlurReturn`, `TOnBlurAsyncReturn`, `TOnSubmitReturn`, `TOnSubmitAsyncReturn`, `TOnServerReturn`\>

## Returns

`FormApi`\<`TFormData`, `TFormValidator`\>
`FormApi`\<`TFormData`, `TFormValidator`, `TOnMountReturn`, `TOnChangeReturn`, `TOnChangeAsyncReturn`, `TOnBlurReturn`, `TOnBlurAsyncReturn`, `TOnSubmitReturn`, `TOnSubmitAsyncReturn`, `TOnServerReturn`\>
22 changes: 20 additions & 2 deletions docs/framework/angular/reference/functions/injectstore.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ id: injectStore
title: injectStore
---

<!-- DO NOT EDIT: this page is autogenerated from the type comments -->

# Function: injectStore()

```ts
function injectStore<TFormData, TFormValidator, TSelected>(form, selector?): Signal<TSelected>
function injectStore<TFormData, TFormValidator, TOnMountReturn, TOnChangeReturn, TOnChangeAsyncReturn, TOnBlurReturn, TOnBlurAsyncReturn, TOnSubmitReturn, TOnSubmitAsyncReturn, TOnServerReturn, TSelected>(form, selector?): Signal<TSelected>
```

Defined in: [inject-store.ts:4](https://github.com/TanStack/form/blob/main/packages/angular-form/src/inject-store.ts#L4)
Expand All @@ -17,13 +19,29 @@ Defined in: [inject-store.ts:4](https://github.com/TanStack/form/blob/main/packa

• **TFormValidator** *extends* `undefined` \| `Validator`\<`TFormData`, `unknown`\> = `undefined`

• **TOnMountReturn** = `undefined`

• **TOnChangeReturn** = `undefined`

• **TOnChangeAsyncReturn** = `undefined`

• **TOnBlurReturn** = `undefined`

• **TOnBlurAsyncReturn** = `undefined`

• **TOnSubmitReturn** = `undefined`

• **TOnSubmitAsyncReturn** = `undefined`

• **TOnServerReturn** = `undefined`

• **TSelected** = `NoInfer`\<`FormState`\<`TFormData`\>\>

## Parameters

### form

`FormApi`\<`TFormData`, `TFormValidator`\>
`FormApi`\<`TFormData`, `TFormValidator`, `TOnMountReturn`, `TOnChangeReturn`, `TOnChangeAsyncReturn`, `TOnBlurReturn`, `TOnBlurAsyncReturn`, `TOnSubmitReturn`, `TOnSubmitAsyncReturn`, `TOnServerReturn`\>

### selector?

Expand Down
2 changes: 2 additions & 0 deletions docs/framework/angular/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ id: "@tanstack/angular-form"
title: "@tanstack/angular-form"
---

<!-- DO NOT EDIT: this page is autogenerated from the type comments -->

# @tanstack/angular-form

## Classes
Expand Down
Loading
Loading