-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add react-hook form resolver
- Loading branch information
1 parent
60bc748
commit cf664eb
Showing
6 changed files
with
192 additions
and
7 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
packages/elements-react/src/components/form/__snapshots__/form-resolver.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`case={"method":"code","code":"","email":""} 1`] = ` | ||
{ | ||
"errors": { | ||
"code": { | ||
"context": { | ||
"property": "code", | ||
}, | ||
"id": 4000002, | ||
"text": "Property code is missing", | ||
"type": "error", | ||
}, | ||
}, | ||
"values": { | ||
"code": "", | ||
"email": "", | ||
"method": "code", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`case={"method":"code","code":"123456","email":""} 1`] = ` | ||
{ | ||
"errors": {}, | ||
"values": { | ||
"code": "123456", | ||
"email": "", | ||
"method": "code", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`case={"method":"code","code":"123456","email":"[email protected]"} 1`] = ` | ||
{ | ||
"errors": {}, | ||
"values": { | ||
"code": "123456", | ||
"email": "[email protected]", | ||
"method": "code", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`case={"method":"code"} 1`] = ` | ||
{ | ||
"errors": { | ||
"code": { | ||
"context": { | ||
"property": "code", | ||
}, | ||
"id": 4000002, | ||
"text": "Property code is missing", | ||
"type": "error", | ||
}, | ||
}, | ||
"values": { | ||
"method": "code", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`case={"method":"password"} 1`] = ` | ||
{ | ||
"errors": {}, | ||
"values": { | ||
"method": "password", | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/elements-react/src/components/form/form-resolver.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { renderHook } from "@testing-library/react" | ||
import { FormValues } from "../../types" | ||
import { useOryFormResolver } from "./form-resolver" | ||
import { OryFlowProvider } from "../../context/flow-context" | ||
import { defaultConfiguration } from "../../tests/jest/test-utils" | ||
import { PropsWithChildren } from "react" | ||
import { FlowType, LoginFlow } from "@ory/client-fetch" | ||
|
||
const testCases = [ | ||
{ | ||
method: "code", | ||
code: "", | ||
email: "", | ||
}, | ||
{ | ||
method: "code", | ||
}, | ||
{ | ||
method: "code", | ||
code: "123456", | ||
email: "", | ||
}, | ||
{ | ||
method: "code", | ||
code: "123456", | ||
email: "[email protected]", | ||
}, | ||
{ | ||
method: "password", | ||
}, | ||
] satisfies FormValues[] | ||
|
||
const wrapper = ({ children }: PropsWithChildren) => ( | ||
<OryFlowProvider | ||
config={defaultConfiguration} | ||
flow={ | ||
{ | ||
active: "code", | ||
ui: { nodes: [], action: "", method: "" }, | ||
} as unknown as LoginFlow // Fine, we're just testing the resolver | ||
} | ||
flowType={FlowType.Login} | ||
> | ||
{children} | ||
</OryFlowProvider> | ||
) | ||
|
||
for (const testCase of testCases) { | ||
test("case=" + JSON.stringify(testCase), () => { | ||
const formResolver = renderHook(() => useOryFormResolver(), { | ||
wrapper, | ||
}) | ||
expect(formResolver.result.current(testCase)).toMatchSnapshot() | ||
}) | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/elements-react/src/components/form/form-resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useOryFlow } from "../../context" | ||
import { FormValues } from "../../types" | ||
|
||
function isCodeResendRequest(data: FormValues) { | ||
return data.email ?? data.resend | ||
} | ||
|
||
/** | ||
* Creates a resolver for the Ory form | ||
* | ||
* The resolver does form validation for missing fields in the form. | ||
* | ||
* @returns a react-hook-form resolver for the Ory form | ||
*/ | ||
export function useOryFormResolver() { | ||
const flowContainer = useOryFlow() | ||
|
||
return (data: FormValues) => { | ||
if (flowContainer.formState.current === "method_active") { | ||
if (data.method === "code" && !data.code && !isCodeResendRequest(data)) { | ||
return { | ||
values: data, | ||
errors: { | ||
code: { | ||
id: 4000002, | ||
context: { | ||
property: "code", | ||
}, | ||
type: "error", | ||
text: "Property code is missing", | ||
}, | ||
}, | ||
} | ||
} | ||
} | ||
return { | ||
values: data, | ||
errors: {}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters