From 0dc440d7d91312374243748271abef123ab2c5b0 Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Sun, 5 Nov 2023 05:59:47 -0800 Subject: [PATCH] fix: errors should no longer throw on intial render (#508) --- packages/form-core/src/FormApi.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index 977c276f3..445e1d4df 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -177,16 +177,21 @@ export class FormApi { mount = () => { const doValidate = () => { - if (typeof this.options.onMount === 'function') { - return this.options.onMount(this.state.values, this) - } - if (this.options.validator) { + if ( + this.options.validator && + typeof this.options.onMount !== 'function' + ) { return (this.options.validator as Validator)().validate( this.state.values, this.options.onMount, ) } + return (this.options.onMount as ValidateFn)( + this.state.values, + this, + ) } + if (!this.options.onMount) return const error = doValidate() if (error) { this.store.setState((prev) => ({ @@ -269,17 +274,16 @@ export class FormApi { const errorMapKey = getErrorMapKey(cause) const doValidate = () => { - if (typeof validate === 'function') { - return validate(this.state.values, this) as ValidationError - } if (this.options.validator && typeof validate !== 'function') { return (this.options.validator as Validator)().validate( this.state.values, validate, ) } - throw new Error( - `Form validation for ${errorMapKey} failed. ${errorMapKey} should either be a function, or \`validator\` should be correct.`, + + return (validate as ValidateFn)( + this.state.values, + this, ) }