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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: wip on things more
crutchcorn committed Jan 11, 2025
commit 8b874042ce18dc70c48a80e5346a62f90b924dce
88 changes: 81 additions & 7 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
@@ -418,7 +418,15 @@ export interface FieldApiOptions<
form: FormApi<TParentData, TFormValidator>
}

export type FieldMetaBase = {
export type FieldMetaBase<
TOnMountReturn = unknown,
TOnChangeReturn = unknown,
TOnChangeAsyncReturn = unknown,
TOnBlurReturn = unknown,
TOnBlurAsyncReturn = unknown,
TOnSubmitReturn = unknown,
TOnSubmitAsyncReturn = unknown,
> = {
/**
* A flag indicating whether the field has been touched.
*/
@@ -434,18 +442,42 @@ export type FieldMetaBase = {
/**
* A map of errors related to the field value.
*/
errorMap: ValidationErrorMap
errorMap: ValidationErrorMap<
TOnMountReturn,
TOnChangeReturn,
TOnChangeAsyncReturn,
TOnBlurReturn,
TOnBlurAsyncReturn,
TOnSubmitReturn,
TOnSubmitAsyncReturn
>
/**
* A flag indicating whether the field is currently being validated.
*/
isValidating: boolean
}

export type FieldMetaDerived = {
export type FieldMetaDerived<
TOnMountReturn = unknown,
TOnChangeReturn = unknown,
TOnChangeAsyncReturn = unknown,
TOnBlurReturn = unknown,
TOnBlurAsyncReturn = unknown,
TOnSubmitReturn = unknown,
TOnSubmitAsyncReturn = unknown,
> = {
/**
* An array of errors related to the field value.
*/
errors: ValidationError[]
errors: Array<
| TOnMountReturn
| TOnChangeReturn
| TOnChangeAsyncReturn
| TOnBlurReturn
| TOnBlurAsyncReturn
| TOnSubmitReturn
| TOnSubmitAsyncReturn
>
/**
* A flag that is `true` if the field's value has not been modified by the user. Opposite of `isDirty`.
*/
@@ -455,20 +487,62 @@ export type FieldMetaDerived = {
/**
* An object type representing the metadata of a field in a form.
*/
export type FieldMeta = FieldMetaBase & FieldMetaDerived
export type FieldMeta<
TOnMountReturn = unknown,
TOnChangeReturn = unknown,
TOnChangeAsyncReturn = unknown,
TOnBlurReturn = unknown,
TOnBlurAsyncReturn = unknown,
TOnSubmitReturn = unknown,
TOnSubmitAsyncReturn = unknown,
> = FieldMetaBase<
TOnMountReturn,
TOnChangeReturn,
TOnChangeAsyncReturn,
TOnBlurReturn,
TOnBlurAsyncReturn,
TOnSubmitReturn,
TOnSubmitAsyncReturn
> &
FieldMetaDerived<
TOnMountReturn,
TOnChangeReturn,
TOnChangeAsyncReturn,
TOnBlurReturn,
TOnBlurAsyncReturn,
TOnSubmitReturn,
TOnSubmitAsyncReturn
>

/**
* An object type representing the state of a field.
*/
export type FieldState<TData> = {
export type FieldState<
TData,
TOnMountReturn = unknown,
TOnChangeReturn = unknown,
TOnChangeAsyncReturn = unknown,
TOnBlurReturn = unknown,
TOnBlurAsyncReturn = unknown,
TOnSubmitReturn = unknown,
TOnSubmitAsyncReturn = unknown,
> = {
/**
* The current value of the field.
*/
value: TData
/**
* The current metadata of the field.
*/
meta: FieldMeta
meta: FieldMeta<
TOnMountReturn,
TOnChangeReturn,
TOnChangeAsyncReturn,
TOnBlurReturn,
TOnBlurAsyncReturn,
TOnSubmitReturn,
TOnSubmitAsyncReturn
>
}

/**
4 changes: 2 additions & 2 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import {
isStandardSchemaValidator,
standardSchemaValidator,
} from './standardSchemaValidator'
import {
import type {
FormValidationError,
FormValidationErrorMap,
SpecialFormValidationError,
@@ -759,7 +759,7 @@ export class FormApi<
) {
errors = Object.values(currBaseStore.errorMap).reduce(
(prev, curr) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition

if (curr === undefined) return prev
if (typeof curr === 'string') {
prev.push(curr)
6 changes: 3 additions & 3 deletions packages/form-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -8,15 +8,15 @@ export type ValidationSource = 'form' | 'field'
* If/when TypeScript supports higher-kinded types, this should not be `unknown` anymore
* @private
*/
export type Validator<Type, Fn = unknown> = () => {
export type Validator<Type, Fn = unknown, TReturnType = unknown> = () => {
validate(
options: { value: Type; validationSource: ValidationSource },
fn: Fn,
): ValidationError | FormValidationError<unknown>
): TReturnType
validateAsync(
options: { value: Type; validationSource: ValidationSource },
fn: Fn,
): Promise<ValidationError | FormValidationError<unknown>>
): Promise<TReturnType>
}

/**