-
-
Notifications
You must be signed in to change notification settings - Fork 339
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
Add Arktype Validator #912
Draft
Makisuo
wants to merge
5
commits into
TanStack:main
Choose a base branch
from
Makisuo:feat/arktype-validator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,11 @@ | ||
// @ts-check | ||
|
||
/** @type {import('eslint').Linter.Config} */ | ||
const config = { | ||
extends: ['plugin:react/recommended', 'plugin:react-hooks/recommended'], | ||
rules: { | ||
'react/no-children-prop': 'off', | ||
}, | ||
} | ||
|
||
module.exports = config |
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,27 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
pnpm-lock.yaml | ||
yarn.lock | ||
package-lock.json | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
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,6 @@ | ||
# Example | ||
|
||
To run this example: | ||
|
||
- `npm install` | ||
- `npm run dev` |
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,16 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/emblem-light.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta name="theme-color" content="#000000" /> | ||
|
||
<title>TanStack Form React Valibot Example App</title> | ||
</head> | ||
<body> | ||
<noscript>You need to enable JavaScript to run this app.</noscript> | ||
<div id="root"></div> | ||
<script type="module" src="/src/index.tsx"></script> | ||
</body> | ||
</html> |
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,37 @@ | ||
{ | ||
"name": "@tanstack/form-example-react-arktype", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite --port=3001", | ||
"build": "vite build", | ||
"preview": "vite preview", | ||
"test:types": "tsc" | ||
}, | ||
"dependencies": { | ||
"@tanstack/arktype-form-adapter": "^0.28.0", | ||
"@tanstack/react-form": "^0.29.1", | ||
"arktype": "2.0.0-beta.5", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1", | ||
"valibot": "^0.37.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
}, | ||
"devDependencies": { | ||
"@types/react": "^18.3.3", | ||
"@types/react-dom": "^18.3.0", | ||
"@vitejs/plugin-react": "^4.3.1", | ||
"vite": "^5.4.0" | ||
}, | ||
"browserslist": { | ||
"production": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
], | ||
"development": [ | ||
"last 1 chrome version", | ||
"last 1 firefox version", | ||
"last 1 safari version" | ||
] | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,104 @@ | ||||||
import * as React from 'react' | ||||||
import { createRoot } from 'react-dom/client' | ||||||
import { useForm } from '@tanstack/react-form' | ||||||
import { arktype, arktypeValidator } from '@tanstack/arktype-form-adapter' | ||||||
import type { FieldApi } from '@tanstack/react-form' | ||||||
|
||||||
function FieldInfo({ field }: { field: FieldApi<any, any, any, any> }) { | ||||||
return ( | ||||||
<> | ||||||
{field.state.meta.isTouched && field.state.meta.errors.length ? ( | ||||||
<em>{field.state.meta.errors.join(',')}</em> | ||||||
) : null} | ||||||
{field.state.meta.isValidating ? 'Validating...' : null} | ||||||
</> | ||||||
) | ||||||
} | ||||||
|
||||||
export default function App() { | ||||||
const form = useForm({ | ||||||
defaultValues: { | ||||||
firstName: '', | ||||||
lastName: '', | ||||||
}, | ||||||
onSubmit: async ({ value }) => { | ||||||
// Do something with form data | ||||||
console.log(value) | ||||||
}, | ||||||
// Add a validator to support Arktype usage in Form and Field | ||||||
validatorAdapter: arktypeValidator(), | ||||||
}) | ||||||
|
||||||
return ( | ||||||
<div> | ||||||
<h1>Valibot Form Example</h1> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
<form | ||||||
onSubmit={(e) => { | ||||||
e.preventDefault() | ||||||
e.stopPropagation() | ||||||
form.handleSubmit() | ||||||
}} | ||||||
> | ||||||
<div> | ||||||
{/* A type-safe field component*/} | ||||||
<form.Field | ||||||
name="firstName" | ||||||
validators={{ | ||||||
onChange: arktype('string > 2'), | ||||||
}} | ||||||
children={(field) => { | ||||||
// Avoid hasty abstractions. Render props are great! | ||||||
return ( | ||||||
<> | ||||||
<label htmlFor={field.name}>First Name:</label> | ||||||
<input | ||||||
id={field.name} | ||||||
name={field.name} | ||||||
value={field.state.value} | ||||||
onBlur={field.handleBlur} | ||||||
onChange={(e) => field.handleChange(e.target.value)} | ||||||
/> | ||||||
<FieldInfo field={field} /> | ||||||
</> | ||||||
) | ||||||
}} | ||||||
/> | ||||||
</div> | ||||||
<div> | ||||||
<form.Field | ||||||
name="lastName" | ||||||
children={(field) => ( | ||||||
<> | ||||||
<label htmlFor={field.name}>Last Name:</label> | ||||||
<input | ||||||
id={field.name} | ||||||
name={field.name} | ||||||
value={field.state.value} | ||||||
onBlur={field.handleBlur} | ||||||
onChange={(e) => field.handleChange(e.target.value)} | ||||||
/> | ||||||
<FieldInfo field={field} /> | ||||||
</> | ||||||
)} | ||||||
/> | ||||||
</div> | ||||||
<form.Subscribe | ||||||
selector={(state) => [state.canSubmit, state.isSubmitting]} | ||||||
children={([canSubmit, isSubmitting]) => ( | ||||||
<button type="submit" disabled={!canSubmit}> | ||||||
{isSubmitting ? '...' : 'Submit'} | ||||||
</button> | ||||||
)} | ||||||
/> | ||||||
</form> | ||||||
</div> | ||||||
) | ||||||
} | ||||||
|
||||||
const rootElement = document.getElementById('root')! | ||||||
|
||||||
createRoot(rootElement).render( | ||||||
<React.StrictMode> | ||||||
<App /> | ||||||
</React.StrictMode>, | ||||||
) |
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,23 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
"module": "ESNext", | ||
"skipLibCheck": true, | ||
|
||
/* Bundler mode */ | ||
"moduleResolution": "Bundler", | ||
"allowImportingTsExtensions": true, | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true, | ||
"jsx": "react-jsx", | ||
|
||
/* Linting */ | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"include": ["src"] | ||
} |
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,5 @@ | ||
// @ts-check | ||
|
||
import rootConfig from '../../eslint.config.js' | ||
|
||
export default [...rootConfig] |
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,63 @@ | ||
{ | ||
"name": "@tanstack/arktype-form-adapter", | ||
"version": "0.28.0", | ||
"description": "The Arktype adapter for TanStack Form.", | ||
"author": "tannerlinsley", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/TanStack/form.git", | ||
"directory": "packages/zod-form-adapter" | ||
}, | ||
"homepage": "https://tanstack.com/form", | ||
"funding": { | ||
"type": "github", | ||
"url": "https://github.com/sponsors/tannerlinsley" | ||
}, | ||
"scripts": { | ||
"clean": "rimraf ./dist && rimraf ./coverage", | ||
"test:eslint": "eslint ./src ./tests", | ||
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"", | ||
"test:types:ts49": "node ../../node_modules/typescript49/lib/tsc.js -p tsconfig.legacy.json", | ||
"test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js", | ||
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js", | ||
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js", | ||
"test:types:ts53": "node ../../node_modules/typescript53/lib/tsc.js", | ||
"test:types:ts54": "tsc", | ||
"test:lib": "vitest", | ||
"test:lib:dev": "pnpm run test:lib --watch", | ||
"test:build": "publint --strict", | ||
"build": "vite build" | ||
}, | ||
"type": "module", | ||
"types": "dist/esm/index.d.ts", | ||
"main": "dist/cjs/index.cjs", | ||
"module": "dist/esm/index.js", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/esm/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"require": { | ||
"types": "./dist/cjs/index.d.cts", | ||
"default": "./dist/cjs/index.cjs" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"sideEffects": false, | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"dependencies": { | ||
"@tanstack/form-core": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"arktype": "2.0.0-beta.5" | ||
}, | ||
"peerDependencies": { | ||
"arktype": "^2.x" | ||
} | ||
} |
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 @@ | ||
export * from './validator' |
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,59 @@ | ||
import { type } from 'arktype' | ||
import type { TypeParser } from 'arktype/internal/type.js' | ||
import type { ValidationError, Validator } from '@tanstack/form-core' | ||
import type { ArkErrors, Type, inferAmbient, validateAmbient } from 'arktype' | ||
import type { inferred } from 'arktype/internal/ast.js' | ||
|
||
type Params = { | ||
transformErrors?: (errors: ArkErrors) => ValidationError | ||
} | ||
|
||
export const arktypeValidator = (params: Params = {}) => | ||
(() => { | ||
return { | ||
validate({ value }, fn) { | ||
console.log(fn, 'XD') | ||
// Call Arktype on the value here and return the error message | ||
const result = fn.type(value) | ||
|
||
console.log(result) | ||
|
||
if (result instanceof type.errors) { | ||
if (params.transformErrors) { | ||
return params.transformErrors(result) | ||
} | ||
|
||
return result.summary | ||
} | ||
|
||
return | ||
}, | ||
async validateAsync({ value }, fn): Promise<ValidationError> { | ||
console.log(fn, 'XD') | ||
|
||
// Call Arktype on the value here and return the error message | ||
const result = fn.type(value) | ||
|
||
if (result instanceof type.errors) { | ||
if (params.transformErrors) { | ||
return params.transformErrors(result) | ||
} | ||
|
||
return result.summary | ||
} | ||
|
||
return | ||
}, | ||
} | ||
}) as Validator<unknown, { type: Type }> | ||
|
||
export type cast<t> = { | ||
[inferred]?: t | ||
wow: TypeParser<{}> | ||
} | ||
|
||
export type ParseType = <const def>(def: validateAmbient<def>) => { | ||
type: Type<inferAmbient<def>> | ||
} | ||
|
||
export const arktype: ParseType = (v) => ({ type: type(v) }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.