@form-observer/vue 0.7.1 → 0.9.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/createFormValidityObserver.cjs +14 -11
- package/createFormValidityObserver.d.cts +6 -5
- package/createFormValidityObserver.d.ts +6 -5
- package/createFormValidityObserver.js +14 -11
- package/package.json +3 -3
- package/types.d.cts +21 -15
- package/types.d.ts +21 -15
|
@@ -7,16 +7,17 @@ module.exports.default = createFormValidityObserver;
|
|
|
7
7
|
/**
|
|
8
8
|
* Creates an enhanced version of the {@link FormValidityObserver} that's more convenient for `Vue` apps
|
|
9
9
|
*
|
|
10
|
-
* @template {import("./index.d.cts").
|
|
10
|
+
* @template {import("./index.d.cts").EventType | null} T
|
|
11
11
|
* @template [M=string]
|
|
12
12
|
* @template {import("./index.d.cts").ValidatableField} [E=import("./index.d.cts").ValidatableField]
|
|
13
|
-
* @
|
|
14
|
-
* @param {
|
|
15
|
-
* @
|
|
13
|
+
* @template {boolean} [R=false]
|
|
14
|
+
* @param {T} type
|
|
15
|
+
* @param {import("./index.d.cts").FormValidityObserverOptions<M, E, R>} [options]
|
|
16
|
+
* @returns {import("./types.d.cts").VueFormValidityObserver<M, R>}
|
|
16
17
|
*/
|
|
17
|
-
function createFormValidityObserver(
|
|
18
|
-
const observer = /** @type {import("./types.d.cts").VueFormValidityObserver<M>} */ (
|
|
19
|
-
/** @type {unknown} */ (new FormValidityObserver(
|
|
18
|
+
function createFormValidityObserver(type, options) {
|
|
19
|
+
const observer = /** @type {import("./types.d.cts").VueFormValidityObserver<M, R>} */ (
|
|
20
|
+
/** @type {unknown} */ (new FormValidityObserver(type, options))
|
|
20
21
|
);
|
|
21
22
|
|
|
22
23
|
/* -------------------- Bindings -------------------- */
|
|
@@ -32,7 +33,7 @@ function createFormValidityObserver(types, options) {
|
|
|
32
33
|
observer.clearFieldError = observer.clearFieldError.bind(observer);
|
|
33
34
|
|
|
34
35
|
/** **Private** reference to the original {@link FormValidityObserver.configure} method */
|
|
35
|
-
const originalConfigure = /** @type {FormValidityObserver<M>["configure"]} */ (observer.configure.bind(observer));
|
|
36
|
+
const originalConfigure = /** @type {FormValidityObserver<M, R>["configure"]} */ (observer.configure.bind(observer));
|
|
36
37
|
|
|
37
38
|
/* -------------------- Enhancements -------------------- */
|
|
38
39
|
// Add automatic setup/teardown
|
|
@@ -56,9 +57,11 @@ function createFormValidityObserver(types, options) {
|
|
|
56
57
|
|
|
57
58
|
// Enhanced `configure` method
|
|
58
59
|
observer.configure = function configure(name, errorMessages) {
|
|
59
|
-
const keys = /** @type {Array<keyof import("./types.d.cts").VueValidationErrors<M>>} */ (
|
|
60
|
+
const keys = /** @type {Array<keyof import("./types.d.cts").VueValidationErrors<M, any, R>>} */ (
|
|
61
|
+
Object.keys(errorMessages)
|
|
62
|
+
);
|
|
60
63
|
const props = /** @type {import("./types.d.cts").VueFieldProps} */ ({ name });
|
|
61
|
-
const config = /** @type {import("./index.d.cts").ValidationErrors<M>} */ ({});
|
|
64
|
+
const config = /** @type {import("./index.d.cts").ValidationErrors<M, any, R>} */ ({});
|
|
62
65
|
|
|
63
66
|
// Build `props` object and error `config` object from `errorMessages`
|
|
64
67
|
for (let i = 0; i < keys.length; i++) {
|
|
@@ -79,7 +82,7 @@ function createFormValidityObserver(types, options) {
|
|
|
79
82
|
|
|
80
83
|
/* ----- Standrd HTML Attributes ----- */
|
|
81
84
|
// Value Only
|
|
82
|
-
if (typeof constraintValue !== "object") {
|
|
85
|
+
if (typeof constraintValue !== "object" || !("message" in constraintValue)) {
|
|
83
86
|
if (constraint === "required" && typeof constraintValue !== "boolean") config[constraint] = constraintValue;
|
|
84
87
|
props[constraint] = constraint === "required" ? true : constraintValue;
|
|
85
88
|
continue;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates an enhanced version of the {@link FormValidityObserver} that's more convenient for `Vue` apps
|
|
3
3
|
*
|
|
4
|
-
* @template {import("./index.d.cts").
|
|
4
|
+
* @template {import("./index.d.cts").EventType | null} T
|
|
5
5
|
* @template [M=string]
|
|
6
6
|
* @template {import("./index.d.cts").ValidatableField} [E=import("./index.d.cts").ValidatableField]
|
|
7
|
-
* @
|
|
8
|
-
* @param {
|
|
9
|
-
* @
|
|
7
|
+
* @template {boolean} [R=false]
|
|
8
|
+
* @param {T} type
|
|
9
|
+
* @param {import("./index.d.cts").FormValidityObserverOptions<M, E, R>} [options]
|
|
10
|
+
* @returns {import("./types.d.cts").VueFormValidityObserver<M, R>}
|
|
10
11
|
*/
|
|
11
|
-
export default function createFormValidityObserver<T extends
|
|
12
|
+
export default function createFormValidityObserver<T extends keyof DocumentEventMap | null, M = string, E extends import("@form-observer/core/types").ValidatableField = import("@form-observer/core/types").ValidatableField, R extends boolean = false>(type: T, options?: import("@form-observer/core/FormValidityObserver").FormValidityObserverOptions<M, E, R> | undefined): import("./types.d.cts").VueFormValidityObserver<M, R>;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates an enhanced version of the {@link FormValidityObserver} that's more convenient for `Vue` apps
|
|
3
3
|
*
|
|
4
|
-
* @template {import("./index.d.ts").
|
|
4
|
+
* @template {import("./index.d.ts").EventType | null} T
|
|
5
5
|
* @template [M=string]
|
|
6
6
|
* @template {import("./index.d.ts").ValidatableField} [E=import("./index.d.ts").ValidatableField]
|
|
7
|
-
* @
|
|
8
|
-
* @param {
|
|
9
|
-
* @
|
|
7
|
+
* @template {boolean} [R=false]
|
|
8
|
+
* @param {T} type
|
|
9
|
+
* @param {import("./index.d.ts").FormValidityObserverOptions<M, E, R>} [options]
|
|
10
|
+
* @returns {import("./types.d.ts").VueFormValidityObserver<M, R>}
|
|
10
11
|
*/
|
|
11
|
-
export default function createFormValidityObserver<T extends
|
|
12
|
+
export default function createFormValidityObserver<T extends keyof DocumentEventMap | null, M = string, E extends import("@form-observer/core/types").ValidatableField = import("@form-observer/core/types").ValidatableField, R extends boolean = false>(type: T, options?: import("@form-observer/core/FormValidityObserver").FormValidityObserverOptions<M, E, R> | undefined): import("./types.d.ts").VueFormValidityObserver<M, R>;
|
|
@@ -3,16 +3,17 @@ import FormValidityObserver from "@form-observer/core/FormValidityObserver";
|
|
|
3
3
|
/**
|
|
4
4
|
* Creates an enhanced version of the {@link FormValidityObserver} that's more convenient for `Vue` apps
|
|
5
5
|
*
|
|
6
|
-
* @template {import("./index.d.ts").
|
|
6
|
+
* @template {import("./index.d.ts").EventType | null} T
|
|
7
7
|
* @template [M=string]
|
|
8
8
|
* @template {import("./index.d.ts").ValidatableField} [E=import("./index.d.ts").ValidatableField]
|
|
9
|
-
* @
|
|
10
|
-
* @param {
|
|
11
|
-
* @
|
|
9
|
+
* @template {boolean} [R=false]
|
|
10
|
+
* @param {T} type
|
|
11
|
+
* @param {import("./index.d.ts").FormValidityObserverOptions<M, E, R>} [options]
|
|
12
|
+
* @returns {import("./types.d.ts").VueFormValidityObserver<M, R>}
|
|
12
13
|
*/
|
|
13
|
-
export default function createFormValidityObserver(
|
|
14
|
-
const observer = /** @type {import("./types.d.ts").VueFormValidityObserver<M>} */ (
|
|
15
|
-
/** @type {unknown} */ (new FormValidityObserver(
|
|
14
|
+
export default function createFormValidityObserver(type, options) {
|
|
15
|
+
const observer = /** @type {import("./types.d.ts").VueFormValidityObserver<M, R>} */ (
|
|
16
|
+
/** @type {unknown} */ (new FormValidityObserver(type, options))
|
|
16
17
|
);
|
|
17
18
|
|
|
18
19
|
/* -------------------- Bindings -------------------- */
|
|
@@ -28,7 +29,7 @@ export default function createFormValidityObserver(types, options) {
|
|
|
28
29
|
observer.clearFieldError = observer.clearFieldError.bind(observer);
|
|
29
30
|
|
|
30
31
|
/** **Private** reference to the original {@link FormValidityObserver.configure} method */
|
|
31
|
-
const originalConfigure = /** @type {FormValidityObserver<M>["configure"]} */ (observer.configure.bind(observer));
|
|
32
|
+
const originalConfigure = /** @type {FormValidityObserver<M, R>["configure"]} */ (observer.configure.bind(observer));
|
|
32
33
|
|
|
33
34
|
/* -------------------- Enhancements -------------------- */
|
|
34
35
|
// Add automatic setup/teardown
|
|
@@ -52,9 +53,11 @@ export default function createFormValidityObserver(types, options) {
|
|
|
52
53
|
|
|
53
54
|
// Enhanced `configure` method
|
|
54
55
|
observer.configure = function configure(name, errorMessages) {
|
|
55
|
-
const keys = /** @type {Array<keyof import("./types.d.ts").VueValidationErrors<M>>} */ (
|
|
56
|
+
const keys = /** @type {Array<keyof import("./types.d.ts").VueValidationErrors<M, any, R>>} */ (
|
|
57
|
+
Object.keys(errorMessages)
|
|
58
|
+
);
|
|
56
59
|
const props = /** @type {import("./types.d.ts").VueFieldProps} */ ({ name });
|
|
57
|
-
const config = /** @type {import("./index.d.ts").ValidationErrors<M>} */ ({});
|
|
60
|
+
const config = /** @type {import("./index.d.ts").ValidationErrors<M, any, R>} */ ({});
|
|
58
61
|
|
|
59
62
|
// Build `props` object and error `config` object from `errorMessages`
|
|
60
63
|
for (let i = 0; i < keys.length; i++) {
|
|
@@ -75,7 +78,7 @@ export default function createFormValidityObserver(types, options) {
|
|
|
75
78
|
|
|
76
79
|
/* ----- Standrd HTML Attributes ----- */
|
|
77
80
|
// Value Only
|
|
78
|
-
if (typeof constraintValue !== "object") {
|
|
81
|
+
if (typeof constraintValue !== "object" || !("message" in constraintValue)) {
|
|
79
82
|
if (constraint === "required" && typeof constraintValue !== "boolean") config[constraint] = constraintValue;
|
|
80
83
|
props[constraint] = constraint === "required" ? true : constraintValue;
|
|
81
84
|
continue;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@form-observer/vue",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.9.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "Convenience functions for the `@form-observer/core` package, designed for Vue apps",
|
|
7
7
|
"exports": {
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@form-observer/core": "^0.
|
|
32
|
+
"@form-observer/core": "^0.9.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@testing-library/vue": "^8.0.
|
|
35
|
+
"@testing-library/vue": "^8.0.3",
|
|
36
36
|
"@vitejs/plugin-vue": "^5.0.4",
|
|
37
37
|
"vue": "^3.3.6"
|
|
38
38
|
},
|
package/types.d.cts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { ErrorMessage, ValidationErrors, ValidatableField, FormValidityObserver } from "@form-observer/core";
|
|
2
2
|
import type { InputHTMLAttributes } from "vue";
|
|
3
3
|
|
|
4
|
-
export interface VueFormValidityObserver<M = string
|
|
4
|
+
export interface VueFormValidityObserver<M = string, R extends boolean = false>
|
|
5
|
+
extends Omit<FormValidityObserver<M, R>, "configure"> {
|
|
5
6
|
/**
|
|
6
7
|
* An enhanced version of {@link FormValidityObserver.configure} for `Vue`. In addition to configuring a field's
|
|
7
8
|
* error messages, it generates the props that should be applied to the field based on the provided arguments.
|
|
@@ -21,7 +22,7 @@ export interface VueFormValidityObserver<M = string> extends Omit<FormValidityOb
|
|
|
21
22
|
* <input v-bind="configure('comment', { maxlength: 10 })" />
|
|
22
23
|
* <input name="another-comment" maxlength="10" />
|
|
23
24
|
*/
|
|
24
|
-
configure<E extends ValidatableField>(name: string, errorMessages: VueValidationErrors<M, E>): VueFieldProps;
|
|
25
|
+
configure<E extends ValidatableField>(name: string, errorMessages: VueValidationErrors<M, E, R>): VueFieldProps;
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* Creates a Vue function `ref` used to automatically setup and cleanup a form's observer.
|
|
@@ -49,21 +50,26 @@ export type VueFieldProps = Pick<
|
|
|
49
50
|
* An augmetation of {@link ValidationErrors} for `Vue`. Represents the constraints that should be applied
|
|
50
51
|
* to a form field, and the error messages that should be displayed when those constraints are broken.
|
|
51
52
|
*/
|
|
52
|
-
export interface VueValidationErrors<M, E extends ValidatableField = ValidatableField>
|
|
53
|
-
extends Pick<ValidationErrors<M, E>, "badinput" | "validate"> {
|
|
53
|
+
export interface VueValidationErrors<M, E extends ValidatableField = ValidatableField, R extends boolean = false>
|
|
54
|
+
extends Pick<ValidationErrors<M, E, R>, "badinput" | "validate"> {
|
|
54
55
|
// Standard HTML Attributes
|
|
55
|
-
required?: VueErrorDetails<M, InputHTMLAttributes["required"], E> | ErrorMessage<string, E>;
|
|
56
|
-
minlength?: VueErrorDetails<M, InputHTMLAttributes["minlength"], E>;
|
|
57
|
-
min?: VueErrorDetails<M, InputHTMLAttributes["min"], E>;
|
|
58
|
-
maxlength?: VueErrorDetails<M, InputHTMLAttributes["maxlength"], E>;
|
|
59
|
-
max?: VueErrorDetails<M, InputHTMLAttributes["max"], E>;
|
|
60
|
-
step?: VueErrorDetails<M, InputHTMLAttributes["step"], E>;
|
|
61
|
-
type?: VueErrorDetails<M, InputHTMLAttributes["type"], E>;
|
|
62
|
-
pattern?: VueErrorDetails<M, InputHTMLAttributes["pattern"], E>;
|
|
56
|
+
required?: VueErrorDetails<M, InputHTMLAttributes["required"], E, R> | ErrorMessage<R extends true ? M : string, E>;
|
|
57
|
+
minlength?: VueErrorDetails<M, InputHTMLAttributes["minlength"], E, R>;
|
|
58
|
+
min?: VueErrorDetails<M, InputHTMLAttributes["min"], E, R>;
|
|
59
|
+
maxlength?: VueErrorDetails<M, InputHTMLAttributes["maxlength"], E, R>;
|
|
60
|
+
max?: VueErrorDetails<M, InputHTMLAttributes["max"], E, R>;
|
|
61
|
+
step?: VueErrorDetails<M, InputHTMLAttributes["step"], E, R>;
|
|
62
|
+
type?: VueErrorDetails<M, InputHTMLAttributes["type"], E, R>;
|
|
63
|
+
pattern?: VueErrorDetails<M, InputHTMLAttributes["pattern"], E, R>;
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
/** An augmentation of `ErrorDetails` for `Vue`. */
|
|
66
|
-
export type VueErrorDetails<M, V, E extends ValidatableField = ValidatableField> =
|
|
67
|
+
export type VueErrorDetails<M, V, E extends ValidatableField = ValidatableField, R extends boolean = false> =
|
|
67
68
|
| V
|
|
68
|
-
|
|
|
69
|
-
|
|
69
|
+
| (R extends true
|
|
70
|
+
?
|
|
71
|
+
| { render?: true; message: ErrorMessage<M, E>; value: V }
|
|
72
|
+
| { render: false; message: ErrorMessage<string, E>; value: V }
|
|
73
|
+
:
|
|
74
|
+
| { render: true; message: ErrorMessage<M, E>; value: V }
|
|
75
|
+
| { render?: false; message: ErrorMessage<string, E>; value: V });
|
package/types.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { ErrorMessage, ValidationErrors, ValidatableField, FormValidityObserver } from "@form-observer/core";
|
|
2
2
|
import type { InputHTMLAttributes } from "vue";
|
|
3
3
|
|
|
4
|
-
export interface VueFormValidityObserver<M = string
|
|
4
|
+
export interface VueFormValidityObserver<M = string, R extends boolean = false>
|
|
5
|
+
extends Omit<FormValidityObserver<M, R>, "configure"> {
|
|
5
6
|
/**
|
|
6
7
|
* An enhanced version of {@link FormValidityObserver.configure} for `Vue`. In addition to configuring a field's
|
|
7
8
|
* error messages, it generates the props that should be applied to the field based on the provided arguments.
|
|
@@ -21,7 +22,7 @@ export interface VueFormValidityObserver<M = string> extends Omit<FormValidityOb
|
|
|
21
22
|
* <input v-bind="configure('comment', { maxlength: 10 })" />
|
|
22
23
|
* <input name="another-comment" maxlength="10" />
|
|
23
24
|
*/
|
|
24
|
-
configure<E extends ValidatableField>(name: string, errorMessages: VueValidationErrors<M, E>): VueFieldProps;
|
|
25
|
+
configure<E extends ValidatableField>(name: string, errorMessages: VueValidationErrors<M, E, R>): VueFieldProps;
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* Creates a Vue function `ref` used to automatically setup and cleanup a form's observer.
|
|
@@ -49,21 +50,26 @@ export type VueFieldProps = Pick<
|
|
|
49
50
|
* An augmetation of {@link ValidationErrors} for `Vue`. Represents the constraints that should be applied
|
|
50
51
|
* to a form field, and the error messages that should be displayed when those constraints are broken.
|
|
51
52
|
*/
|
|
52
|
-
export interface VueValidationErrors<M, E extends ValidatableField = ValidatableField>
|
|
53
|
-
extends Pick<ValidationErrors<M, E>, "badinput" | "validate"> {
|
|
53
|
+
export interface VueValidationErrors<M, E extends ValidatableField = ValidatableField, R extends boolean = false>
|
|
54
|
+
extends Pick<ValidationErrors<M, E, R>, "badinput" | "validate"> {
|
|
54
55
|
// Standard HTML Attributes
|
|
55
|
-
required?: VueErrorDetails<M, InputHTMLAttributes["required"], E> | ErrorMessage<string, E>;
|
|
56
|
-
minlength?: VueErrorDetails<M, InputHTMLAttributes["minlength"], E>;
|
|
57
|
-
min?: VueErrorDetails<M, InputHTMLAttributes["min"], E>;
|
|
58
|
-
maxlength?: VueErrorDetails<M, InputHTMLAttributes["maxlength"], E>;
|
|
59
|
-
max?: VueErrorDetails<M, InputHTMLAttributes["max"], E>;
|
|
60
|
-
step?: VueErrorDetails<M, InputHTMLAttributes["step"], E>;
|
|
61
|
-
type?: VueErrorDetails<M, InputHTMLAttributes["type"], E>;
|
|
62
|
-
pattern?: VueErrorDetails<M, InputHTMLAttributes["pattern"], E>;
|
|
56
|
+
required?: VueErrorDetails<M, InputHTMLAttributes["required"], E, R> | ErrorMessage<R extends true ? M : string, E>;
|
|
57
|
+
minlength?: VueErrorDetails<M, InputHTMLAttributes["minlength"], E, R>;
|
|
58
|
+
min?: VueErrorDetails<M, InputHTMLAttributes["min"], E, R>;
|
|
59
|
+
maxlength?: VueErrorDetails<M, InputHTMLAttributes["maxlength"], E, R>;
|
|
60
|
+
max?: VueErrorDetails<M, InputHTMLAttributes["max"], E, R>;
|
|
61
|
+
step?: VueErrorDetails<M, InputHTMLAttributes["step"], E, R>;
|
|
62
|
+
type?: VueErrorDetails<M, InputHTMLAttributes["type"], E, R>;
|
|
63
|
+
pattern?: VueErrorDetails<M, InputHTMLAttributes["pattern"], E, R>;
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
/** An augmentation of `ErrorDetails` for `Vue`. */
|
|
66
|
-
export type VueErrorDetails<M, V, E extends ValidatableField = ValidatableField> =
|
|
67
|
+
export type VueErrorDetails<M, V, E extends ValidatableField = ValidatableField, R extends boolean = false> =
|
|
67
68
|
| V
|
|
68
|
-
|
|
|
69
|
-
|
|
69
|
+
| (R extends true
|
|
70
|
+
?
|
|
71
|
+
| { render?: true; message: ErrorMessage<M, E>; value: V }
|
|
72
|
+
| { render: false; message: ErrorMessage<string, E>; value: V }
|
|
73
|
+
:
|
|
74
|
+
| { render: true; message: ErrorMessage<M, E>; value: V }
|
|
75
|
+
| { render?: false; message: ErrorMessage<string, E>; value: V });
|