ctrovalidate-vue 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +112 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +103 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ctrotech
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# ctrovalidate-vue
|
|
2
|
+
|
|
3
|
+
**Reactive form validation for Vue 3.**
|
|
4
|
+
|
|
5
|
+
`ctrovalidate-vue` provides a `useCtrovalidate` composable that wraps [`ctrovalidate-core`](https://www.npmjs.com/package/ctrovalidate-core)'s validation engine with Vue 3's reactivity system. Supports `v-model`, watcher-based real-time validation, and automatic abort handling.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/ctrovalidate-vue)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install ctrovalidate-vue ctrovalidate-core
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
**Requirements:** Vue ^3.0.0
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```vue
|
|
25
|
+
<script setup lang="ts">
|
|
26
|
+
import { useCtrovalidate } from 'ctrovalidate-vue';
|
|
27
|
+
|
|
28
|
+
interface LoginForm { email: string; password: string; }
|
|
29
|
+
|
|
30
|
+
const { values, errors, handleBlur, validateForm, isValidating } =
|
|
31
|
+
useCtrovalidate<LoginForm>({
|
|
32
|
+
initialValues: { email: '', password: '' },
|
|
33
|
+
schema: { email: 'required|email', password: 'required|minLength:8' },
|
|
34
|
+
});
|
|
35
|
+
</script>
|
|
36
|
+
|
|
37
|
+
<template>
|
|
38
|
+
<form @submit.prevent="validateForm()">
|
|
39
|
+
<input v-model="values.email" @blur="handleBlur('email')" />
|
|
40
|
+
<span v-if="errors.email">{{ errors.email }}</span>
|
|
41
|
+
|
|
42
|
+
<input type="password" v-model="values.password" @blur="handleBlur('password')" />
|
|
43
|
+
<span v-if="errors.password">{{ errors.password }}</span>
|
|
44
|
+
|
|
45
|
+
<button :disabled="Object.values(isValidating).some(Boolean)">Login</button>
|
|
46
|
+
</form>
|
|
47
|
+
</template>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## API
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
const {
|
|
56
|
+
values, // Reactive<T> — form state, v-model ready
|
|
57
|
+
errors, // Reactive — pre-initialized, string | undefined per field
|
|
58
|
+
isDirty, // Reactive — tracks touched fields
|
|
59
|
+
isValidating, // Reactive — tracks async validation
|
|
60
|
+
isValid, // boolean — overall form validity (Ref.unwrap)
|
|
61
|
+
handleChange, // (name, value) => void
|
|
62
|
+
handleBlur, // (name) => void
|
|
63
|
+
validateField, // (name) => Promise<boolean>
|
|
64
|
+
validateForm, // () => Promise<boolean>
|
|
65
|
+
reset, // (newValues?) => void
|
|
66
|
+
} = useCtrovalidate<T>({
|
|
67
|
+
schema, // Required: field-to-rules mapping
|
|
68
|
+
initialValues, // Optional: form defaults (default: {})
|
|
69
|
+
validateOnBlur, // Optional: validate on blur (default: true)
|
|
70
|
+
validateOnChange, // Optional: watch values, validate dirty fields (default: true)
|
|
71
|
+
customRules, // Optional: custom sync/async rule functions
|
|
72
|
+
aliases, // Optional: rule combination aliases
|
|
73
|
+
messages, // Optional: custom error messages
|
|
74
|
+
locale, // Optional: locale override for translator
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Validation Behavior
|
|
81
|
+
|
|
82
|
+
| Trigger | Condition | Action |
|
|
83
|
+
|---------|-----------|--------|
|
|
84
|
+
| `handleChange(name, value)` | `validateOnChange` | Updates value, marks dirty, validates |
|
|
85
|
+
| `handleBlur(name)` | `validateOnBlur` | Marks dirty, validates |
|
|
86
|
+
| `v-model` change | `validateOnChange` + `watch()` | Validates dirty fields |
|
|
87
|
+
|
|
88
|
+
When `validateOnChange` is enabled, a `watch()` with `{ deep: true }` on `values` re-validates any dirty field whose value changed.
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## Abort Handling
|
|
93
|
+
|
|
94
|
+
Each field has its own `AbortController`. Re-validating a field aborts any in-flight async rule. All controllers are aborted on `onUnmounted`.
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Related Packages
|
|
99
|
+
|
|
100
|
+
- **[ctrovalidate-core](https://www.npmjs.com/package/ctrovalidate-core)** — Validation engine
|
|
101
|
+
- **[ctrovalidate-browser](https://www.npmjs.com/package/ctrovalidate-browser)** — Vanilla JS DOM integration
|
|
102
|
+
- **[ctrovalidate-react](https://www.npmjs.com/package/ctrovalidate-react)** — React hook
|
|
103
|
+
- **[ctrovalidate-svelte](https://www.npmjs.com/package/ctrovalidate-svelte)** — Svelte stores
|
|
104
|
+
- **[ctrovalidate-next](https://www.npmjs.com/package/ctrovalidate-next)** — Next.js server actions
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## License
|
|
109
|
+
|
|
110
|
+
MIT © [Ctrotech](https://github.com/ctrotech-tutor)
|
|
111
|
+
|
|
112
|
+
Full documentation: https://ctrovalidate.vercel.app
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("vue"),y=require("ctrovalidate-core");function p({schema:e,initialValues:b={},validateOnBlur:w=!0,validateOnChange:g=!0,customRules:j={},aliases:k={},messages:C={},locale:O}){const s=i.reactive({...b}),a=i.reactive(Object.keys(e).reduce((r,t)=>(r[t]=void 0,r),{})),n=i.reactive(Object.keys(e).reduce((r,t)=>(r[t]=!1,r),{})),f=i.reactive(Object.keys(e).reduce((r,t)=>(r[t]=!1,r),{})),v=i.ref(!0),l={};i.onUnmounted(()=>{Object.values(l).forEach(r=>r.abort())});const d=async r=>{var u;const t=e[r];if(!t)return!0;l[r]&&l[r].abort(),l[r]=new AbortController,f[r]=!0;try{const c=(u=(await y.validateAsync({[r]:s[r]},{[r]:t},{customRules:j,aliases:k,messages:C,locale:O,signal:l[r].signal}))[r])==null?void 0:u.error;return a[r]=c||void 0,!c}catch(o){return o instanceof Error&&o.name==="AbortError"||y.Logger.error(`Validation failed for ${String(r)}:`,o),!1}finally{f[r]=!1}};async function A(){var u;const r=await y.validateAsync(s,e,{customRules:j,aliases:k,messages:C,locale:O});let t=!0;for(const o in e){const c=(u=r[o])==null?void 0:u.error;a[o]=c||void 0,c&&(t=!1)}return v.value=t,t}const S=r=>{Object.assign(s,{...b,...r});for(const t in e)a[t]=void 0,n[t]=!1,f[t]=!1;v.value=!0},V=(r,t)=>{s[r]=t,n[r]=!0,g&&d(r)},E=r=>{n[r]=!0,w&&d(r)};return g&&i.watch(s,r=>{for(const t in r)n[t]&&d(t)},{deep:!0}),{values:s,errors:a,isDirty:n,isValidating:f,isValid:v.value,validateField:d,validateForm:A,reset:S,handleChange:V,handleBlur:E}}exports.useCtrovalidate=p;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { AsyncRuleLogic } from 'ctrovalidate-core';
|
|
2
|
+
import { RuleLogic } from 'ctrovalidate-core';
|
|
3
|
+
import { SchemaRule } from 'ctrovalidate-core';
|
|
4
|
+
import { ValidationSchema } from 'ctrovalidate-core';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* useCtrovalidate composable for Vue 3.
|
|
8
|
+
* Provides reactive validation logic for form state.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useCtrovalidate<T extends object>({ schema, initialValues, validateOnBlur, validateOnChange, customRules, aliases, messages, locale, }: UseCtrovalidateOptions<T>): UseCtrovalidateReturn<T>;
|
|
11
|
+
|
|
12
|
+
export declare interface UseCtrovalidateOptions<T extends object> {
|
|
13
|
+
schema: ValidationSchema;
|
|
14
|
+
initialValues?: T;
|
|
15
|
+
validateOnBlur?: boolean;
|
|
16
|
+
validateOnChange?: boolean;
|
|
17
|
+
customRules?: Record<string, RuleLogic | AsyncRuleLogic>;
|
|
18
|
+
aliases?: Record<string, SchemaRule>;
|
|
19
|
+
messages?: Record<string, string>;
|
|
20
|
+
locale?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export declare interface UseCtrovalidateReturn<T extends object> {
|
|
24
|
+
values: T;
|
|
25
|
+
errors: Record<keyof T, string | undefined>;
|
|
26
|
+
isDirty: Record<keyof T, boolean>;
|
|
27
|
+
isValidating: Record<keyof T, boolean>;
|
|
28
|
+
isValid: boolean;
|
|
29
|
+
validateField: (name: keyof T) => Promise<boolean>;
|
|
30
|
+
validateForm: () => Promise<boolean>;
|
|
31
|
+
reset: (newValues?: Partial<T>) => void;
|
|
32
|
+
handleChange: (name: keyof T, value: T[keyof T]) => void;
|
|
33
|
+
handleBlur: (name: keyof T) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { reactive as a, ref as F, onUnmounted as S, watch as x } from "vue";
|
|
2
|
+
import { validateAsync as w, Logger as B } from "ctrovalidate-core";
|
|
3
|
+
function L({
|
|
4
|
+
schema: e,
|
|
5
|
+
initialValues: y = {},
|
|
6
|
+
validateOnBlur: O = !0,
|
|
7
|
+
validateOnChange: b = !0,
|
|
8
|
+
customRules: g = {},
|
|
9
|
+
aliases: k = {},
|
|
10
|
+
messages: j = {},
|
|
11
|
+
locale: p
|
|
12
|
+
}) {
|
|
13
|
+
const i = a({ ...y }), f = a(
|
|
14
|
+
Object.keys(e).reduce(
|
|
15
|
+
(r, t) => (r[t] = void 0, r),
|
|
16
|
+
{}
|
|
17
|
+
)
|
|
18
|
+
), s = a(
|
|
19
|
+
Object.keys(e).reduce(
|
|
20
|
+
(r, t) => (r[t] = !1, r),
|
|
21
|
+
{}
|
|
22
|
+
)
|
|
23
|
+
), c = a(
|
|
24
|
+
Object.keys(e).reduce(
|
|
25
|
+
(r, t) => (r[t] = !1, r),
|
|
26
|
+
{}
|
|
27
|
+
)
|
|
28
|
+
), v = F(!0), n = {};
|
|
29
|
+
S(() => {
|
|
30
|
+
Object.values(n).forEach((r) => r.abort());
|
|
31
|
+
});
|
|
32
|
+
const d = async (r) => {
|
|
33
|
+
var l;
|
|
34
|
+
const t = e[r];
|
|
35
|
+
if (!t) return !0;
|
|
36
|
+
n[r] && n[r].abort(), n[r] = new AbortController(), c[r] = !0;
|
|
37
|
+
try {
|
|
38
|
+
const u = (l = (await w(
|
|
39
|
+
{ [r]: i[r] },
|
|
40
|
+
{ [r]: t },
|
|
41
|
+
{
|
|
42
|
+
customRules: g,
|
|
43
|
+
aliases: k,
|
|
44
|
+
messages: j,
|
|
45
|
+
locale: p,
|
|
46
|
+
signal: n[r].signal
|
|
47
|
+
}
|
|
48
|
+
))[r]) == null ? void 0 : l.error;
|
|
49
|
+
return f[r] = u || void 0, !u;
|
|
50
|
+
} catch (o) {
|
|
51
|
+
return o instanceof Error && o.name === "AbortError" || B.error(`Validation failed for ${String(r)}:`, o), !1;
|
|
52
|
+
} finally {
|
|
53
|
+
c[r] = !1;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
async function V() {
|
|
57
|
+
var l;
|
|
58
|
+
const r = await w(i, e, {
|
|
59
|
+
customRules: g,
|
|
60
|
+
aliases: k,
|
|
61
|
+
messages: j,
|
|
62
|
+
locale: p
|
|
63
|
+
});
|
|
64
|
+
let t = !0;
|
|
65
|
+
for (const o in e) {
|
|
66
|
+
const u = (l = r[o]) == null ? void 0 : l.error;
|
|
67
|
+
f[o] = u || void 0, u && (t = !1);
|
|
68
|
+
}
|
|
69
|
+
return v.value = t, t;
|
|
70
|
+
}
|
|
71
|
+
const A = (r) => {
|
|
72
|
+
Object.assign(i, { ...y, ...r });
|
|
73
|
+
for (const t in e)
|
|
74
|
+
f[t] = void 0, s[t] = !1, c[t] = !1;
|
|
75
|
+
v.value = !0;
|
|
76
|
+
}, C = (r, t) => {
|
|
77
|
+
i[r] = t, s[r] = !0, b && d(r);
|
|
78
|
+
}, E = (r) => {
|
|
79
|
+
s[r] = !0, O && d(r);
|
|
80
|
+
};
|
|
81
|
+
return b && x(
|
|
82
|
+
i,
|
|
83
|
+
(r) => {
|
|
84
|
+
for (const t in r)
|
|
85
|
+
s[t] && d(t);
|
|
86
|
+
},
|
|
87
|
+
{ deep: !0 }
|
|
88
|
+
), {
|
|
89
|
+
values: i,
|
|
90
|
+
errors: f,
|
|
91
|
+
isDirty: s,
|
|
92
|
+
isValidating: c,
|
|
93
|
+
isValid: v.value,
|
|
94
|
+
validateField: d,
|
|
95
|
+
validateForm: V,
|
|
96
|
+
reset: A,
|
|
97
|
+
handleChange: C,
|
|
98
|
+
handleBlur: E
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export {
|
|
102
|
+
L as useCtrovalidate
|
|
103
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ctrovalidate-vue",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Vue 3 adapter for Ctrovalidate - The lightweight, declarative validation library.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "vite build",
|
|
21
|
+
"test": "vitest run --passWithNoTests",
|
|
22
|
+
"lint": "eslint .",
|
|
23
|
+
"format": "prettier . --check"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://ctrovalidate.vercel.app/api/vue",
|
|
26
|
+
"keywords": [
|
|
27
|
+
"ctrovalidate",
|
|
28
|
+
"validation",
|
|
29
|
+
"form-validation",
|
|
30
|
+
"vue",
|
|
31
|
+
"vue3"
|
|
32
|
+
],
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/ctrotech-tutor/ctrovalidate-vue.git"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/ctrotech-tutor/ctrovalidate-vue/issues"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"ctrovalidate-core": "^1.0.0",
|
|
45
|
+
"vue": "^3.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@eslint/js": "^9.39.2",
|
|
49
|
+
"ctrovalidate-core": "^1.0.0",
|
|
50
|
+
"eslint": "^9.39.2",
|
|
51
|
+
"eslint-config-prettier": "^9.1.0",
|
|
52
|
+
"globals": "^17.2.0",
|
|
53
|
+
"prettier": "^3.1.1",
|
|
54
|
+
"vite": "^6.0.0",
|
|
55
|
+
"vite-plugin-dts": "^4.0.0",
|
|
56
|
+
"typescript": "^5.9.3",
|
|
57
|
+
"typescript-eslint": "^8.54.0",
|
|
58
|
+
"vitest": "^4.0.18",
|
|
59
|
+
"vue": "^3.5.0"
|
|
60
|
+
}
|
|
61
|
+
}
|