@leaflink/stash 53.4.3 → 53.4.5

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.
@@ -1,5 +1,5 @@
1
1
  import v from "@leaflink/snitch";
2
- import { a as I } from "./index-B1Gkwuxd.js";
2
+ import { a as I } from "./index-DA70nQCT.js";
3
3
  import { isRef as w, toValue as D, nextTick as A, computed as i, ref as y, watch as S, onMounted as $, onBeforeUnmount as G } from "vue";
4
4
  function O({ list: n, from: s, to: e }) {
5
5
  if (s === e) return;
@@ -57,6 +57,18 @@ export declare function email(config?: Config): ValidationRule;
57
57
  */
58
58
  export declare function isDefined(value: unknown): boolean;
59
59
 
60
+ /**
61
+ * Minimal shape for a JSON:API error object when setting validation errors from an HTTP response.
62
+ * @see https://jsonapi.org/format/#error-objects
63
+ */
64
+ export declare interface JsonApiValidationError {
65
+ detail?: string;
66
+ source?: {
67
+ pointer?: string;
68
+ };
69
+ title?: string;
70
+ }
71
+
60
72
  export declare function maxLength(config: Config_2): ValidationRule;
61
73
 
62
74
  export declare function maxValue(config: Config_3): ValidationRule;
@@ -181,6 +193,16 @@ export declare interface ValidationGroup<Values extends object> {
181
193
  * Indicates if one or more fields have had their value changed at least one time
182
194
  */
183
195
  someTouched: boolean;
196
+ /**
197
+ * Sets field error messages from a JSON:API error response.
198
+ * Parses each error's `source.pointer` (e.g. `/data/attributes/email`) to a field name and uses `detail` as the message.
199
+ * Only applies errors for field names that exist in the validation rules; unknown fields are ignored.
200
+ */
201
+ setApiErrors: (errors: JsonApiValidationError[]) => void;
202
+ /**
203
+ * Clears API-set error messages. If `fieldName` is provided, clears only that field; otherwise clears all.
204
+ */
205
+ clearApiErrors: (fieldName?: string) => void;
184
206
  }
185
207
 
186
208
  /**
@@ -1,249 +1,290 @@
1
- import O from "lodash-es/debounce";
2
- import V from "lodash-es/get";
3
- import { toValue as u, computed as c, ref as N, watch as L, reactive as I, nextTick as w, readonly as E } from "vue";
4
- import { DECIMAL_PRECISION as A, DEBOUNCE as q } from "./constants.js";
5
- import { t as f } from "./locale.js";
6
- import { i as l } from "./isDefined-DzVx0B6k.js";
7
- import D from "lodash-es/round";
8
- function j(e) {
9
- return e && e.constructor && typeof e.constructor.isBuffer == "function" && e.constructor.isBuffer(e);
10
- }
11
- function C(e) {
12
- return e;
1
+ import F from "lodash-es/debounce";
2
+ import A from "lodash-es/get";
3
+ import { toValue as c, computed as v, ref as T, watch as R, reactive as L, nextTick as I, readonly as w } from "vue";
4
+ import { DECIMAL_PRECISION as j, DEBOUNCE as q } from "./constants.js";
5
+ import { t as h } from "./locale.js";
6
+ import { i as f } from "./isDefined-DzVx0B6k.js";
7
+ import B from "lodash-es/round";
8
+ function D(t) {
9
+ return t && t.constructor && typeof t.constructor.isBuffer == "function" && t.constructor.isBuffer(t);
10
+ }
11
+ function M(t) {
12
+ return t;
13
13
  }
14
- function F(e, t) {
15
- t = t || {};
16
- const i = t.delimiter || ".", o = t.maxDepth, s = t.transformKey || C, m = {};
17
- function v(h, y, g) {
18
- g = g || 1, Object.keys(h).forEach(function(p) {
19
- const r = h[p], x = t.safe && Array.isArray(r), n = Object.prototype.toString.call(r), a = j(r), b = n === "[object Object]" || n === "[object Array]", d = y ? y + i + s(p) : s(p);
20
- if (!x && !a && b && Object.keys(r).length && (!t.maxDepth || g < o))
21
- return v(r, d, g + 1);
22
- m[d] = r;
14
+ function C(t, e) {
15
+ e = e || {};
16
+ const r = e.delimiter || ".", i = e.maxDepth, a = e.transformKey || M, u = {};
17
+ function m(E, S, d) {
18
+ d = d || 1, Object.keys(E).forEach(function(x) {
19
+ const l = E[x], o = e.safe && Array.isArray(l), g = Object.prototype.toString.call(l), N = D(l), p = g === "[object Object]" || g === "[object Array]", n = S ? S + r + a(x) : a(x);
20
+ if (!o && !N && p && Object.keys(l).length && (!e.maxDepth || d < i))
21
+ return m(l, n, d + 1);
22
+ u[n] = l;
23
23
  });
24
24
  }
25
- return v(e), m;
25
+ return m(t), u;
26
+ }
27
+ function k(t) {
28
+ const e = t.replace(/^\//, ""), r = "data/attributes/";
29
+ if (e.startsWith(r))
30
+ return e.slice(r.length).split("/").filter(Boolean).join(".");
31
+ const i = e.split("/").filter(Boolean);
32
+ return i[i.length - 1] || t;
26
33
  }
27
- function Y(e = {}) {
34
+ function U(t) {
35
+ var r;
36
+ const e = {};
37
+ for (const i of t) {
38
+ const a = (r = i.source) == null ? void 0 : r.pointer;
39
+ if (!a)
40
+ continue;
41
+ const u = i.detail || i.title || "";
42
+ if (!u)
43
+ continue;
44
+ const m = k(a);
45
+ e[m] = u;
46
+ }
47
+ return e;
48
+ }
49
+ function H(t = {}) {
28
50
  return {
29
51
  name: "email",
30
- validator(t) {
31
- return !l(t) || typeof t == "string" && /^\S+@\S+\.\S+$/.test(t);
52
+ validator(e) {
53
+ return !f(e) || typeof e == "string" && /^\S+@\S+\.\S+$/.test(e);
32
54
  },
33
- message: e.message || f("ll.validation.email")
55
+ message: t.message || h("ll.validation.email")
34
56
  };
35
57
  }
36
- function _(e) {
58
+ function Q(t) {
37
59
  return {
38
60
  name: "maxLength",
39
- validator(t) {
40
- const i = u(e.max), o = u(t);
41
- if (!l(o))
61
+ validator(e) {
62
+ const r = c(t.max), i = c(e);
63
+ if (!f(i))
42
64
  return !0;
43
- let s = String(o);
44
- return e.trim && (s = s.trim()), s.length <= i;
65
+ let a = String(i);
66
+ return t.trim && (a = a.trim()), a.length <= r;
45
67
  },
46
- message: e.message || (() => f("ll.validation.maxLength", { max: String(u(e.max)) }))
68
+ message: t.message || (() => h("ll.validation.maxLength", { max: String(c(t.max)) }))
47
69
  };
48
70
  }
49
- function z(e) {
71
+ function X(t) {
50
72
  return {
51
73
  name: "maxValue",
52
- validator(t) {
53
- const i = u(e.max);
54
- return !l(t) || Number(t) <= i;
74
+ validator(e) {
75
+ const r = c(t.max);
76
+ return !f(e) || Number(e) <= r;
55
77
  },
56
- message: e.message || (() => f("ll.validation.maxValue", { max: String(u(e.max)) }))
78
+ message: t.message || (() => h("ll.validation.maxValue", { max: String(c(t.max)) }))
57
79
  };
58
80
  }
59
- function G(e) {
81
+ function Z(t) {
60
82
  return {
61
83
  name: "minLength",
62
- validator(t) {
63
- const i = u(e.min), o = u(t);
64
- if (!l(o))
84
+ validator(e) {
85
+ const r = c(t.min), i = c(e);
86
+ if (!f(i))
65
87
  return !0;
66
- let s = String(o);
67
- return e.trim && (s = s.trim()), s.length >= i;
88
+ let a = String(i);
89
+ return t.trim && (a = a.trim()), a.length >= r;
68
90
  },
69
- message: e.message || (() => f("ll.validation.minLength", { min: String(u(e.min)) }))
91
+ message: t.message || (() => h("ll.validation.minLength", { min: String(c(t.min)) }))
70
92
  };
71
93
  }
72
- function H(e) {
94
+ function ee(t) {
73
95
  return {
74
96
  name: "minValue",
75
- validator(t) {
76
- const i = u(e.min);
77
- return !l(t) || Number(t) >= i;
97
+ validator(e) {
98
+ const r = c(t.min);
99
+ return !f(e) || Number(e) >= r;
78
100
  },
79
- message: e.message || (() => f("ll.validation.minValue", { min: String(u(e.min)) }))
101
+ message: t.message || (() => h("ll.validation.minValue", { min: String(c(t.min)) }))
80
102
  };
81
103
  }
82
- function J(e) {
104
+ function te(t) {
83
105
  return {
84
106
  name: "pattern",
85
- validator(t) {
86
- const i = u(t);
87
- return !l(i) || e.regex.test(String(i));
107
+ validator(e) {
108
+ const r = c(e);
109
+ return !f(r) || t.regex.test(String(r));
88
110
  },
89
111
  message: () => {
90
- if (typeof e.message != "string" || !e.message.trim())
112
+ if (typeof t.message != "string" || !t.message.trim())
91
113
  throw new Error(
92
114
  'The "pattern" ruleFactory requires a custom message informing the user of the required pattern.'
93
115
  );
94
- return e.message;
116
+ return t.message;
95
117
  }
96
118
  };
97
119
  }
98
- function Q(e = {}) {
120
+ function re(t = {}) {
99
121
  return {
100
122
  name: "positiveNumber",
101
- validator(t) {
102
- return !l(t) || Number(t) > 0;
123
+ validator(e) {
124
+ return !f(e) || Number(e) > 0;
103
125
  },
104
- message: e.message || f("ll.validation.positiveNumber")
126
+ message: t.message || h("ll.validation.positiveNumber")
105
127
  };
106
128
  }
107
- function X(e = {}) {
129
+ function ne(t = {}) {
108
130
  return {
109
131
  name: "price",
110
- validator(t) {
111
- const i = Number(t);
112
- return !l(t) || i !== 1 / 0 && i >= 0 && i === D(i, A.CURRENCY);
132
+ validator(e) {
133
+ const r = Number(e);
134
+ return !f(e) || r !== 1 / 0 && r >= 0 && r === B(r, j.CURRENCY);
113
135
  },
114
- message: e.message || f("ll.validation.price")
136
+ message: t.message || h("ll.validation.price")
115
137
  };
116
138
  }
117
- function Z(e = {}) {
139
+ function se(t = {}) {
118
140
  return {
119
141
  name: "required",
120
- validator(t) {
121
- return l(typeof t == "string" ? t.trim() : t);
142
+ validator(e) {
143
+ return f(typeof e == "string" ? e.trim() : e);
122
144
  },
123
- message: e.message || f("ll.validation.required")
145
+ message: t.message || h("ll.validation.required")
124
146
  };
125
147
  }
126
- function W(e = {}) {
148
+ function ae(t = {}) {
127
149
  return {
128
150
  name: "wholeNumber",
129
- validator(t) {
130
- return !l(t) || Number.isInteger(Number(t)) && Number(t) >= 0;
151
+ validator(e) {
152
+ return !f(e) || Number.isInteger(Number(e)) && Number(e) >= 0;
131
153
  },
132
- message: e.message || f("ll.validation.wholeNumber")
154
+ message: t.message || h("ll.validation.wholeNumber")
133
155
  };
134
156
  }
135
- function ee({
136
- rules: e,
137
- values: t
157
+ const _ = "api";
158
+ function ie({
159
+ rules: t,
160
+ values: e
138
161
  }) {
139
- const o = c(() => Object.keys(F(e, { safe: !0 }))).value.reduce(
140
- (n, a) => {
141
- const b = N(V(u(t), a)), d = (T) => {
142
- b.value = V(u(t), T);
143
- }, S = O(() => {
144
- d(a);
162
+ const r = v(() => Object.keys(C(t, { safe: !0 }))), i = T({}), a = r.value.reduce(
163
+ (n, s) => {
164
+ const V = T(A(c(e), s)), y = (O) => {
165
+ V.value = A(c(e), O);
166
+ }, b = F(() => {
167
+ y(s);
145
168
  }, q.FAST);
146
- return L(
147
- () => V(u(t), a),
169
+ return R(
170
+ () => A(c(e), s),
148
171
  () => {
149
- S();
172
+ b();
150
173
  }
151
- ), n[a] = B({
152
- fieldName: a,
153
- fieldValue: b,
174
+ ), n[s] = K({
175
+ fieldName: s,
176
+ fieldValue: V,
154
177
  flush: () => {
155
- S.cancel(), d(a);
178
+ b.cancel(), y(s);
156
179
  },
157
- rules: V(e, a)
180
+ getApiError: () => i.value[s] || "",
181
+ rules: A(t, s)
158
182
  }), n;
159
183
  },
160
184
  {}
161
- ), s = c(() => Object.values(o)), m = c(() => s.value.some((n) => n.errors.length > 0)), v = c(() => s.value.some((n) => n.isTouched)), h = c(() => s.value.filter((n) => n.isDirty));
162
- return I({
163
- validate: async function({ preventScroll: n } = {}) {
164
- if (s.value.forEach((a) => {
165
- a.setTouched(!1), a.setTouched(!0);
166
- }), await w(), m.value && !n) {
167
- const a = Object.entries(o).find(([, S]) => S.errorMessage), b = (a == null ? void 0 : a[0]) || "", d = document == null ? void 0 : document.querySelector(`[name=${b}]`);
168
- d && d.scrollIntoView({ behavior: "smooth" });
169
- }
170
- return !m.value;
171
- },
172
- getError: function(n) {
173
- var a;
174
- return ((a = o[n]) == null ? void 0 : a.errorMessage) || "";
175
- },
176
- touch: function(n) {
177
- if (!o[n])
178
- throw new Error(`In touch: fieldName "${n}" does not exist in the validation fields.`);
179
- o[n].setTouched();
180
- },
181
- setAllUntouched: async function() {
182
- await w(), s.value.forEach((n) => {
183
- n.setTouched(!1);
184
- });
185
- },
186
- setInitialValues: function(n) {
187
- s.value.forEach((a) => {
188
- a.setInitialValue(n[a.name]);
189
- });
190
- },
191
- fields: o,
192
- dirtyFields: h,
185
+ ), u = v(() => Object.values(a)), m = v(() => u.value.some((n) => n.errors.length > 0)), E = v(() => u.value.some((n) => n.isTouched)), S = v(() => u.value.filter((n) => n.isDirty)), d = async function({ preventScroll: n } = {}) {
186
+ if (u.value.forEach((s) => {
187
+ s.setTouched(!1), s.setTouched(!0);
188
+ }), await I(), m.value && !n) {
189
+ const s = Object.entries(a).find(([, b]) => b.errorMessage), V = (s == null ? void 0 : s[0]) || "", y = document == null ? void 0 : document.querySelector(`[name=${V}]`);
190
+ y && y.scrollIntoView({ behavior: "smooth" });
191
+ }
192
+ return !m.value;
193
+ }, x = function(n) {
194
+ var s;
195
+ return ((s = a[n]) == null ? void 0 : s.errorMessage) || "";
196
+ }, l = function(n) {
197
+ if (!a[n])
198
+ throw new Error(`In touch: fieldName "${n}" does not exist in the validation fields.`);
199
+ a[n].setTouched();
200
+ }, o = async function() {
201
+ await I(), u.value.forEach((n) => {
202
+ n.setTouched(!1);
203
+ });
204
+ }, g = function(n) {
205
+ u.value.forEach((s) => {
206
+ s.setInitialValue(n[s.name]);
207
+ });
208
+ };
209
+ function N(n) {
210
+ const s = U(n), V = r.value, y = {};
211
+ for (const b of V)
212
+ s[b] && (y[b] = s[b]);
213
+ i.value = y;
214
+ }
215
+ function p(n) {
216
+ if (n) {
217
+ const s = { ...i.value };
218
+ delete s[n], i.value = s;
219
+ } else
220
+ i.value = {};
221
+ }
222
+ return L({
223
+ validate: d,
224
+ getError: x,
225
+ touch: l,
226
+ setAllUntouched: o,
227
+ setInitialValues: g,
228
+ setApiErrors: N,
229
+ clearApiErrors: p,
230
+ fields: a,
231
+ dirtyFields: S,
193
232
  hasErrors: m,
194
- someTouched: v
233
+ someTouched: E
195
234
  });
196
235
  }
197
- function B({
198
- fieldName: e,
199
- fieldValue: t,
200
- rules: i,
201
- flush: o
236
+ function K({
237
+ fieldName: t,
238
+ fieldValue: e,
239
+ rules: r,
240
+ flush: i,
241
+ getApiError: a
202
242
  }) {
203
- const s = N(t.value), m = N(!1), v = c(() => (t.value ?? "") !== (s.value ?? "")), h = c(() => m.value ? i.map((r) => ({
204
- fieldName: e,
205
- ruleName: r.name,
206
- isValid: r.validator(t.value),
207
- message: typeof r.message == "function" ? r.message(t.value) : r.message
208
- })) : []), y = c(
209
- () => h.value.filter((r) => !r.isValid).map((r) => ({
210
- ruleName: r.ruleName,
211
- message: r.message
212
- }))
213
- );
214
- function g(r = !0) {
215
- o(), m.value = r;
243
+ const u = T(e.value), m = T(!1), E = v(() => (e.value ?? "") !== (u.value ?? "")), S = v(() => m.value ? r.map((o) => ({
244
+ fieldName: t,
245
+ ruleName: o.name,
246
+ isValid: o.validator(e.value),
247
+ message: typeof o.message == "function" ? o.message(e.value) : o.message
248
+ })) : []), d = v(() => {
249
+ const o = a(), g = o !== "" ? [{ ruleName: _, message: o }] : [], N = S.value.filter((p) => !p.isValid).map((p) => ({
250
+ ruleName: p.ruleName,
251
+ message: p.message
252
+ }));
253
+ return [...g, ...N];
254
+ });
255
+ function x(o = !0) {
256
+ i(), m.value = o;
216
257
  }
217
- function p(r) {
218
- s.value = r;
258
+ function l(o) {
259
+ u.value = o;
219
260
  }
220
- return I({
221
- name: e,
222
- currentValue: E(t),
223
- initialValue: E(s),
224
- isTouched: E(m),
225
- isDirty: E(v),
226
- errorMessage: c(() => {
227
- var r, x;
228
- return ((x = (r = y.value) == null ? void 0 : r[0]) == null ? void 0 : x.message) || "";
261
+ return L({
262
+ name: t,
263
+ currentValue: w(e),
264
+ initialValue: w(u),
265
+ isTouched: w(m),
266
+ isDirty: w(E),
267
+ errorMessage: v(() => {
268
+ var o, g;
269
+ return ((g = (o = d.value) == null ? void 0 : o[0]) == null ? void 0 : g.message) || "";
229
270
  }),
230
- errors: y,
231
- setInitialValue: p,
232
- setTouched: g
271
+ errors: d,
272
+ setInitialValue: l,
273
+ setTouched: x
233
274
  });
234
275
  }
235
276
  export {
236
- ee as default,
237
- Y as email,
238
- l as isDefined,
239
- _ as maxLength,
240
- z as maxValue,
241
- G as minLength,
242
- H as minValue,
243
- J as pattern,
244
- Q as positiveNumber,
245
- X as price,
246
- Z as required,
247
- W as wholeNumber
277
+ ie as default,
278
+ H as email,
279
+ f as isDefined,
280
+ Q as maxLength,
281
+ X as maxValue,
282
+ Z as minLength,
283
+ ee as minValue,
284
+ te as pattern,
285
+ re as positiveNumber,
286
+ ne as price,
287
+ se as required,
288
+ ae as wholeNumber
248
289
  };
249
290
  //# sourceMappingURL=useValidation.js.map