11import { type ClerkError , createClerkGlobalHookError , isClerkAPIResponseError } from '@clerk/shared/error' ;
2- import type { Errors , SignInSignal , SignUpSignal } from '@clerk/shared/types' ;
2+ import type { Errors , SignInErrors , SignInSignal , SignUpErrors , SignUpSignal } from '@clerk/shared/types' ;
33import { snakeToCamel } from '@clerk/shared/underscore' ;
44import { computed , signal } from 'alien-signals' ;
55
@@ -15,7 +15,7 @@ export const signInComputedSignal: SignInSignal = computed(() => {
1515 const error = signInErrorSignal ( ) . error ;
1616 const fetchStatus = signInFetchSignal ( ) . status ;
1717
18- const errors = errorsToParsedErrors ( error ) ;
18+ const errors = errorsToSignInErrors ( error ) ;
1919
2020 return { errors, fetchStatus, signIn : signIn ? signIn . __internal_future : null } ;
2121} ) ;
@@ -29,7 +29,7 @@ export const signUpComputedSignal: SignUpSignal = computed(() => {
2929 const error = signUpErrorSignal ( ) . error ;
3030 const fetchStatus = signUpFetchSignal ( ) . status ;
3131
32- const errors = errorsToParsedErrors ( error ) ;
32+ const errors = errorsToSignUpErrors ( error ) ;
3333
3434 return { errors, fetchStatus, signUp : signUp ? signUp . __internal_future : null } ;
3535} ) ;
@@ -38,20 +38,12 @@ export const signUpComputedSignal: SignUpSignal = computed(() => {
3838 * Converts an error to a parsed errors object that reports the specific fields that the error pertains to. Will put
3939 * generic non-API errors into the global array.
4040 */
41- function errorsToParsedErrors ( error : ClerkError | null ) : Errors {
42- const parsedErrors : Errors = {
43- fields : {
44- firstName : null ,
45- lastName : null ,
46- emailAddress : null ,
47- identifier : null ,
48- phoneNumber : null ,
49- password : null ,
50- username : null ,
51- code : null ,
52- captcha : null ,
53- legalAccepted : null ,
54- } ,
41+ export function errorsToParsedErrors < T extends Record < string , unknown > > (
42+ error : ClerkError | null ,
43+ initialFields : T ,
44+ ) : Errors < T > {
45+ const parsedErrors : Errors < T > = {
46+ fields : { ...initialFields } ,
5547 raw : null ,
5648 global : null ,
5749 } ;
@@ -76,7 +68,9 @@ function errorsToParsedErrors(error: ClerkError | null): Errors {
7668 }
7769 if ( 'meta' in error && error . meta && 'paramName' in error . meta ) {
7870 const name = snakeToCamel ( error . meta . paramName ) ;
79- parsedErrors . fields [ name as keyof typeof parsedErrors . fields ] = error ;
71+ if ( name in parsedErrors . fields ) {
72+ ( parsedErrors . fields as any ) [ name ] = error ;
73+ }
8074 }
8175 // Note that this assumes a given ClerkAPIResponseError will only have either field errors or global errors, but
8276 // not both. If a global error is present, it will be discarded.
@@ -91,3 +85,25 @@ function errorsToParsedErrors(error: ClerkError | null): Errors {
9185
9286 return parsedErrors ;
9387}
88+
89+ function errorsToSignInErrors ( error : ClerkError | null ) : SignInErrors {
90+ return errorsToParsedErrors ( error , {
91+ identifier : null ,
92+ password : null ,
93+ code : null ,
94+ } ) ;
95+ }
96+
97+ function errorsToSignUpErrors ( error : ClerkError | null ) : SignUpErrors {
98+ return errorsToParsedErrors ( error , {
99+ firstName : null ,
100+ lastName : null ,
101+ emailAddress : null ,
102+ phoneNumber : null ,
103+ password : null ,
104+ username : null ,
105+ code : null ,
106+ captcha : null ,
107+ legalAccepted : null ,
108+ } ) ;
109+ }
0 commit comments