Skip to content

feat(0): add useRules composable with Zod/Yup adapters#140

Open
johnleider wants to merge 1 commit intomasterfrom
feat/create-rules
Open

feat(0): add useRules composable with Zod/Yup adapters#140
johnleider wants to merge 1 commit intomasterfrom
feat/create-rules

Conversation

@johnleider
Copy link
Member

Summary

  • Adds useRules plugin to @vuetify/v0 providing alias-based form validation rules with token registry message storage and useLocale integration
  • Adds @vuetify/v0/rules/adapters/zod and @vuetify/v0/rules/adapters/yup sub-path exports for zero-dependency integration with Zod and Yup validation schemas
  • Adds full documentation at composables/plugins/use-rules.md including useLocale integration guide
  • Fixes knip.json to explicitly list sub-path adapter files as entry points

New API

useRules plugin

import { createRulesPlugin, useRules } from '@vuetify/v0'

const app = createApp(App)
app.use(createRulesPlugin({
  aliases: {
    required: v => !!v || 'Required',
    email: v => /^.+@.+\..+$/.test(String(v)) || 'Invalid email',
  },
}))

// In component:
const rules = useRules()
const resolved = rules.resolve(['required', ['minLength', 8]])

Zod adapter

import { toRule } from '@vuetify/v0/rules/adapters/zod'
import { z } from 'zod'

const email = toRule(z.string().email('Must be a valid email'))
// Returns a FormValidationRule — no zod runtime in bundle for users who don't import

Yup adapter

import { toRule } from '@vuetify/v0/rules/adapters/yup'
import * as yup from 'yup'

const minAge = toRule(yup.number().min(18, 'Must be 18 or older'))

Integration with createForm

const form = createForm({ rules: createRules() })
const email = form.register({
  id: 'email',
  value: '',
  rules: ['required', toZodRule(z.string().email())],
})

Test plan

  • useRules composable: 66 tests covering all 11 built-in aliases, resolve(), custom aliases, locale fallback, Trinity pattern
  • All 2884 existing tests still pass
  • Lint, typecheck, knip, sherif all pass

Adds a new `useRules` plugin to @vuetify/v0 that provides:
- Alias-based validation rules with token registry message storage
- `createRules`, `createRulesPlugin`, `useRules`, `createRulesFallback`
- 11 built-in aliases: required, minLength, maxLength, min, max, email,
  url, integer, decimal, pattern, phone
- `useLocale` integration via `rules.<alias>` locale key pattern
- Overridable messages via `messages` option in `createRules()`
- `RulesContext` integration in `createForm()` via `rules: [...]`

Adds Zod and Yup adapters as sub-path exports:
- `@vuetify/v0/rules/adapters/zod` — `toRule(schema)` with duck-typed ZodSchema
- `@vuetify/v0/rules/adapters/yup` — `toRule(schema)` with duck-typed YupSchema
- Zero runtime dependency — structural interfaces only

Adds documentation:
- `composables/plugins/use-rules.md` with full API reference
- `useLocale` integration section and 4-step message priority chain
- Updated `composables/index.md` to list useRules in Plugins

Also fixes knip.json to add explicit entry patterns for sub-path adapter
files and root scripts, resolving false-positive unused file warnings.
@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 26, 2026

Open in StackBlitz

commit: 83c4cef

@AndreyYolkin
Copy link
Contributor

I think it's might be better to support https://standardschema.dev/, and get broader validation libs support: Zod, Valibot, ArkType, and more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants