Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { StandardSchemaV1 } from '@standard-schema/spec'
import type { Issues } from './types'
import type { AnyRecord, Issues } from './types'
import { reactive, watch, toValue, type MaybeRefOrGetter } from 'vue'

export function useValidation<T extends Record<string, unknown>>(data: MaybeRefOrGetter<T>, schema: StandardSchemaV1<T>) {
export function useValidation<T extends AnyRecord>(data: MaybeRefOrGetter<T>, schema: StandardSchemaV1<T>) {
const issues = reactive<Issues<T>>({})

const clearIssues = () => Object.keys(issues).forEach((key) => delete issues[key])
Expand Down
16 changes: 12 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
type Combine<T, K extends PropertyKey = T extends unknown ? keyof T : never> = T extends unknown
? T & Partial<Record<Exclude<K, keyof T>, never>>
: never
/** Generate a union of all property keys from T, including keys from all members when T is a union itself. */
type KeysOf<T> = T extends unknown ? keyof T : never

/** Pick property type for a possibly missing key */
type PropType<T, K extends PropertyKey> = T extends { [P in K]?: unknown } ? T[K] : never

/** Nested record of issue message arrays */
export type Issues<T> = {
[Key in keyof Combine<T>]?: Combine<T>[Key] extends object ? Issues<Combine<T>[Key]> : string[]
[K in KeysOf<T>]?: PropType<T, K> extends object
? Issues<PropType<T, K>> // if property type is object (or array): recurse
: string[] // else: array of messages
}

/** Any record with unknown value type */
export type AnyRecord = Record<PropertyKey, unknown>
38 changes: 38 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,44 @@ describe('useValidation', () => {
expect(issues.address?.street).toBeDefined()
expect(issues.address?.city).toBeDefined()
})

it('should accept complex union typed data', async () => {
type Auth =
| { mode: 'login'; auth: { password: string; email: { address: string } } }
| { mode: 'signup'; auth: { code: string; provider: { id: string } } }

const loginSchema = z.object({
mode: z.literal('login'),
auth: z.object({
password: z.string().min(1, 'Password is required'),
email: z.object({
address: z.email('Invalid email address'),
}),
}),
})

const signupSchema = z.object({
mode: z.literal('signup'),
auth: z.object({
code: z.string().length(6, 'Code is required'),
provider: z.object({
id: z.enum(['basic', 'oauth'], 'Invalid provider'),
}),
}),
})

const schema = z.discriminatedUnion('mode', [loginSchema, signupSchema])

const data = reactive<Auth>({ mode: 'login', auth: { password: '', email: { address: '' } } })

const { validate, issues } = useValidation(data, schema)
await validate()

expect(issues.auth?.password).toBeDefined()
expect(issues.auth?.email?.address).toBeDefined()
expect(issues.auth?.code).not.toBeDefined()
expect(issues.auth?.provider?.id).not.toBeDefined()
})
})

describe('clearIssues function', () => {
Expand Down