[V2] Add schema-validating formOptions helpers with a named schema output #2354
lucas-dtrn
started this conversation in
v2 Ideas and Discussions
Replies: 1 comment 1 reply
|
The main counter-argument I have against this is that the returned object is just the options you specified. You used a schema as reference, but what you actually do with the schema is up to you. Previously, it was only one argument and it inferred the rest, so this was possible: formOptions.looseSchema({
defaultValues: {
firstName: 'John',
lastName: 'Doe',
},
validators: [
{
triggers: [],
// It inferred from here
run: schema,
},
],
}),
);However, as soon as you added in any other validator, it would break completely. The two-argument approach does mean you have to specify the schema twice, but inference is as good as it can get, without worrying about |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I am using
@tanstack/react-form@2.0.0-alpha.2and would like to propose a schema-backed options helper that combines type inference and runtime validation.Current behavior
strictSchemaandlooseSchemause the supplied Standard Schema for type inference, but do not register it as a validator.A schema-backed form therefore requires passing the same schema twice:
This has two drawbacks:
looseSchema(schema, ...)also enables runtime validation.Proposed helpers
Add helpers that use the schema for both form-state inference and runtime validation. The names are only suggestions:
Example:
The schema passed as the first argument would become a distinguished primary schema validator. Additional validators could still be supplied through
validators.The existing meaning of
valueshould remain unchanged. It should continue to contain the editable form state rather than the transformed schema output.This distinction matters for transforming schemas:
In this case:
Named schema output
The primary schema output should be exposed as a named property in the
onSubmitcontext:This avoids coupling application code to a position such as
schemaOutputs[0].The primary schema validator could remain part of the internal validator pipeline, but its output would be tracked separately.
schemaOutputscould then represent the explicitly supplied validators without including the implicit primary schema output.Validator merge strategy
createFormHooknow supports reusable default options and alistenersMergestrategy. A similarvalidatorsMergeoption could define where the automatically registered primary schema validator runs relative to explicitly supplied validators:No default validators would be configured in
defaultFormOptions. Every form has different values and validation requirements.validatorsMergewould only configure the ordering between:validatedStrictSchemaorvalidatedLooseSchemaSuggested semantics:
appendwould run the primary schema validator first:prependwould run the form validators first:I would omit
replacebecause these helpers promise that the supplied schema participates in runtime validation. Replacing the primary validator would break that guarantee.Behavior without
createFormHookThe helpers should belong to the core
FormOptionsApi, so they work with bothformOptionsandappFormOptions.For plain
useForm, the core could use a documented default:validatorsMerge: 'append'When the same
formOptionsobject is passed touseAppForm, the form instance could apply thevalidatorsMergestrategy configured bycreateFormHook:The options helper would not need to know the merge strategy when it creates the object. It could retain the primary schema validator separately, and the form instance could resolve the final validator order during initialization.
This also matches the current relationship between
formOptionsandappFormOptions: they are the same runtime helper, whileappFormOptionsadditionally preserves the registered App Form component types.Expected guarantees
The proposed helpers would guarantee that:
schemaOutputcontains the validated and transformed schema output.valueremains the editable form state.formOptionsuses a predictable core default.schemaOutputsarray index.Would this fit the intended direction for the new schema and default-options APIs?
All reactions