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