Skip to content

Commit 6dfea2a

Browse files
author
JB AUBREE
committed
feat(types): internalize external types
1 parent 73b391e commit 6dfea2a

File tree

8 files changed

+77
-21
lines changed

8 files changed

+77
-21
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { useFormValidation } from 'vue-use-form-validation'
4141

4242
```ts
4343
// Import types for better TypeScript support
44-
import type { FieldErrors, Form, GetErrorsFn, ReturnType, Schema } from './types'
44+
import type { FieldErrors, Form, GetErrorsFn, InputSchema, ReturnType } from './types'
4545
```
4646

4747
## Basic Example
@@ -91,7 +91,7 @@ async function onSubmit() {
9191
```ts
9292
const options = {
9393
mode: 'eager', // or 'lazy'
94-
transformFn: (schema: Schema, form: Form) => {
94+
transformFn: (schema: InputSchema, form: Form) => {
9595
// Custom validation logic
9696
return {} // Return errors if any
9797
},
@@ -111,7 +111,7 @@ const { validate } = useFormValidation(schema, form, options)
111111
## API Reference
112112

113113
```ts
114-
declare function useFormValidation<S extends Schema<F>, F extends Form>(
114+
declare function useFormValidation<S extends InputSchema<F>, F extends Form>(
115115
schema: S,
116116
form: MaybeRefOrGetter<F>,
117117
options?: {

build.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ export default defineBuildConfig({
44
entries: [
55
'src/index',
66
],
7-
declaration: true,
87
clean: true,
98
rollup: {
109
esbuild: {
1110
minify: true,
1211
},
1312
emitCJS: true,
1413
},
14+
failOnWarn: false,
1515
})

dts.config.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilationOptions": {
3+
"followSymlinks": true,
4+
"preferredConfigPath": "./tsconfig.json"
5+
},
6+
"entries": [
7+
{
8+
"filePath": "./src/index.ts",
9+
"outFile": "./dist/index.d.ts",
10+
"libraries": {
11+
"importedLibraries": ["vue"],
12+
"inlinedLibraries": [
13+
"joi",
14+
"superstruct",
15+
"valibot",
16+
"yup",
17+
"zod"
18+
]
19+
},
20+
"output": {
21+
"exportReferencedTypes": false
22+
}
23+
}
24+
]
25+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"dist"
3838
],
3939
"scripts": {
40-
"build": "unbuild",
40+
"build": "unbuild && pnpm build:types",
41+
"build:types": "dts-bundle-generator --config dts.config.json",
4142
"dev": "unbuild --stub",
4243
"lint": "eslint .",
4344
"prepublishOnly": "nr build",
@@ -79,6 +80,7 @@
7980
"@types/node": "^22.7.5",
8081
"@vitest/coverage-v8": "^2.1.3",
8182
"bumpp": "^9.7.1",
83+
"dts-bundle-generator": "^9.5.1",
8284
"eslint": "^9.12.0",
8385
"esno": "^4.8.0",
8486
"happy-dom": "^15.7.4",

pnpm-lock.yaml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { FieldErrors, Form, GetErrorsFn, Schema } from './types'
1+
import type { FieldErrors, Form, GetErrorsFn, InputSchema } from './types'
22
import { getJoiErrors, isJoiSchema } from './joi'
33
import { getSuperStructErrors, isSuperStructSchema } from './superstruct'
44
import { getValibotErrors, isValibotSchema } from './valibot'
55
import { getYupErrors, isYupSchema } from './yup'
66
import { getZodErrors, isZodSchema } from './zod'
77

8-
export async function getErrors<S extends Schema<F>, F extends Form>(
8+
export async function getErrors<S extends InputSchema<F>, F extends Form>(
99
schema: S,
1010
form: F,
1111
): Promise<FieldErrors<F>>

src/types.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1-
import type { Schema as JoiSchema } from 'joi'
2-
import type { Struct } from 'superstruct'
3-
import type { BaseIssue, BaseSchema as ValibotSchema } from 'valibot'
1+
import type { Schema } from 'joi'
42
import type { ComputedRef, Ref } from 'vue'
5-
import type { ObjectSchema as YupSchema } from 'yup'
6-
import type { ZodSchema } from 'zod'
73

8-
export type Awaitable<T> = T | PromiseLike<T>
4+
type AnyObject = Record<string, any>
95

6+
interface ZodSchema<F> extends AnyObject {
7+
shape: Record<keyof F, unknown>
8+
}
9+
interface YupSchema<F> extends AnyObject {
10+
fields: Record<keyof F, unknown>
11+
}
12+
interface ValibotSchema<F> extends AnyObject {
13+
entries: Record<keyof F, unknown>
14+
}
15+
// interface JoiTerms<F> extends AnyObject {
16+
// keys: Array<{ key: keyof F, schema: unknown }>
17+
// }
18+
// interface JoiSchema<F> extends AnyObject {
19+
// $_terms: JoiTerms<F>
20+
// }
21+
interface SuperstructSchema<F> extends AnyObject {
22+
schema: Record<keyof F, unknown>
23+
}
24+
25+
export type Awaitable<T> = T | PromiseLike<T>
1026
export type FieldErrors<F> = Partial<Record<keyof F, string>>
1127
export type Form = Record<string, unknown>
1228
export type GetErrorsFn<S, F extends Form> = (schema: S, form: F) => Awaitable<FieldErrors<F>>
1329

14-
export type Schema<F extends Form> =
15-
| ZodSchema
30+
export type InputSchema<F extends Form> =
31+
| ZodSchema<F>
1632
| YupSchema<F>
17-
| ValibotSchema<unknown, unknown, BaseIssue<unknown>>
18-
| JoiSchema
19-
| Struct<F>
33+
| ValibotSchema<F>
34+
| Schema<F>
35+
| SuperstructSchema<F>
2036

2137
export interface ReturnType<F> {
2238
validate: () => Promise<FieldErrors<F>>

src/useFormValidation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FieldErrors, Form, GetErrorsFn, ReturnType, Schema } from './types'
1+
import type { FieldErrors, Form, GetErrorsFn, InputSchema, ReturnType } from './types'
22
import { computed, type MaybeRefOrGetter, ref, shallowRef, toValue, watch } from 'vue'
33
import { getErrors } from './errors'
44
import { polyfillGroupBy } from './polyfill'
@@ -8,12 +8,12 @@ export function useFormValidation<S, F extends Form>(
88
form: MaybeRefOrGetter<F>,
99
options: { mode?: 'eager' | 'lazy', transformFn: GetErrorsFn<S, F> },
1010
): ReturnType<F>
11-
export function useFormValidation<S extends Schema<F>, F extends Form>(
11+
export function useFormValidation<S extends InputSchema<F>, F extends Form>(
1212
schema: S,
1313
form: MaybeRefOrGetter<F>,
1414
options?: { mode?: 'eager' | 'lazy' },
1515
): ReturnType<F>
16-
export function useFormValidation<S extends Schema<F>, F extends Form>(
16+
export function useFormValidation<S extends InputSchema<F>, F extends Form>(
1717
schema: S,
1818
form: MaybeRefOrGetter<F>,
1919
options?: { mode?: 'eager' | 'lazy', transformFn?: GetErrorsFn<S, F> },

0 commit comments

Comments
 (0)