@golemui/gui-validators 1.4.0 → 1.5.0-rc.1
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/CHANGELOG.md +14 -0
- package/index.d.ts +1 -1
- package/index.js +197 -124
- package/index.umd.cjs +1 -1
- package/lib/validators.d.ts +21 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## 1.5.0-rc.0 (2026-09-04)
|
|
2
|
+
|
|
3
|
+
### 🚀 Features
|
|
4
|
+
|
|
5
|
+
- **gui-components:** add file upload widgets ([#332](https://github.com/golemui/golemui/pull/332))
|
|
6
|
+
|
|
7
|
+
### ❤️ Thank You
|
|
8
|
+
|
|
9
|
+
- Raúl Jiménez @Elecash
|
|
10
|
+
|
|
11
|
+
## 1.4.0 (2026-08-26)
|
|
12
|
+
|
|
13
|
+
This was a version bump only for gui-validators to align it with other projects, there were no code changes.
|
|
14
|
+
|
|
1
15
|
## 1.4.0-rc.0 (2026-08-26)
|
|
2
16
|
|
|
3
17
|
This was a version bump only for gui-validators to align it with other projects, there were no code changes.
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { initValidators } from './lib/validators';
|
|
2
|
-
export type { ArrayValidator, BooleanValidator, CustomValidator, CustomValidatorSchemaFn, CustomValidatorSchemas, NumberValidator, StringValidator, Validator, } from './lib/validators';
|
|
2
|
+
export type { ArrayValidator, BooleanValidator, CustomValidator, CustomValidatorSchemaFn, CustomValidatorSchemas, FileValidator, FilesValidator, NumberValidator, StringValidator, Validator, } from './lib/validators';
|
package/index.js
CHANGED
|
@@ -1,206 +1,279 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { standardValidate as I, isStandardValidateSuccess as b, filterTap as A } from "@golemui/core";
|
|
2
2
|
import "@standard-schema/spec";
|
|
3
|
-
import { iso as
|
|
4
|
-
import { uuid as
|
|
5
|
-
const
|
|
6
|
-
email: { schema:
|
|
7
|
-
hostname: { schema:
|
|
8
|
-
ipv4: { schema:
|
|
9
|
-
ipv6: { schema:
|
|
10
|
-
url: { schema:
|
|
11
|
-
uuid: { schema:
|
|
12
|
-
date: { schema:
|
|
13
|
-
time: { schema:
|
|
14
|
-
"date-time": { schema:
|
|
15
|
-
duration: { schema:
|
|
16
|
-
},
|
|
17
|
-
switch (
|
|
3
|
+
import { iso as g } from "zod";
|
|
4
|
+
import { uuid as _, url as T, ipv6 as $, ipv4 as j, hostname as D, email as N, any as a, superRefine as h, array as l, refine as u, minLength as x, maxLength as M, optional as R, boolean as q, number as S, int as P, minimum as G, maximum as J, string as w, regex as B } from "zod/mini";
|
|
5
|
+
const k = {
|
|
6
|
+
email: { schema: N() },
|
|
7
|
+
hostname: { schema: D() },
|
|
8
|
+
ipv4: { schema: j() },
|
|
9
|
+
ipv6: { schema: $() },
|
|
10
|
+
url: { schema: T() },
|
|
11
|
+
uuid: { schema: _() },
|
|
12
|
+
date: { schema: g.date() },
|
|
13
|
+
time: { schema: g.time() },
|
|
14
|
+
"date-time": { schema: g.datetime({ local: !0, offset: !0 }) },
|
|
15
|
+
duration: { schema: g.duration() }
|
|
16
|
+
}, L = Object.keys(k), C = ["uploading", "uploaded", "error"], ne = (m) => (t, e) => {
|
|
17
|
+
switch (t.type) {
|
|
18
18
|
case "string":
|
|
19
|
-
return
|
|
19
|
+
return K(t, e);
|
|
20
20
|
case "integer":
|
|
21
21
|
case "number":
|
|
22
|
-
return
|
|
22
|
+
return W(t, e);
|
|
23
23
|
case "boolean":
|
|
24
|
-
return
|
|
24
|
+
return H(t, e);
|
|
25
25
|
case "array":
|
|
26
|
-
return
|
|
26
|
+
return X(t, e);
|
|
27
|
+
case "file":
|
|
28
|
+
return Y(t, e);
|
|
29
|
+
case "files":
|
|
30
|
+
return Z(t, e);
|
|
27
31
|
case "custom": {
|
|
28
|
-
if (!
|
|
32
|
+
if (!m)
|
|
29
33
|
throw new Error(
|
|
30
34
|
'Validator type "custom" requires a customValidators object, but it was not supplied.'
|
|
31
35
|
);
|
|
32
|
-
return
|
|
36
|
+
return v(t, m);
|
|
33
37
|
}
|
|
34
38
|
default: {
|
|
35
|
-
const c =
|
|
39
|
+
const c = t;
|
|
36
40
|
throw new Error(
|
|
37
|
-
`Unknown validator config: ${JSON.stringify(c)}. The "type" property must be one of: "string", "number", "integer", "boolean", "array", "custom".`
|
|
41
|
+
`Unknown validator config: ${JSON.stringify(c)}. The "type" property must be one of: "string", "number", "integer", "boolean", "array", "file", "files", "custom".`
|
|
38
42
|
);
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
};
|
|
42
|
-
function o(
|
|
43
|
-
if (
|
|
44
|
-
return typeof
|
|
46
|
+
function o(m, t) {
|
|
47
|
+
if (m !== void 0)
|
|
48
|
+
return typeof m == "string" ? m : t?.translate(m.key, m.params, m.default) ?? m.default;
|
|
45
49
|
}
|
|
46
|
-
function
|
|
47
|
-
return f(
|
|
48
|
-
const c = o(e.messages?.invalid,
|
|
49
|
-
let
|
|
50
|
+
function K(m, t) {
|
|
51
|
+
return f(m, (e) => {
|
|
52
|
+
const c = o(e.messages?.invalid, t);
|
|
53
|
+
let r = c ? w(c) : w();
|
|
50
54
|
if (e.required === !0) {
|
|
51
|
-
const
|
|
52
|
-
|
|
55
|
+
const s = o(e.messages?.required, t) ?? "This field is required";
|
|
56
|
+
r = r.check(u((n) => n.length > 0, { error: s }));
|
|
53
57
|
}
|
|
54
58
|
if (typeof e.minLength == "number") {
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
const s = o(e.messages?.minLength, t), n = e.minLength;
|
|
60
|
+
r = r.check(
|
|
61
|
+
s ? u((i) => i.length >= n, { error: s }) : x(n)
|
|
58
62
|
);
|
|
59
63
|
}
|
|
60
64
|
if (typeof e.maxLength == "number") {
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
const s = o(e.messages?.maxLength, t), n = e.maxLength;
|
|
66
|
+
r = r.check(
|
|
67
|
+
s ? u((i) => i.length <= n, { error: s }) : M(n)
|
|
64
68
|
);
|
|
65
69
|
}
|
|
66
70
|
if (typeof e.pattern == "string") {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
const s = o(e.messages?.pattern, t), n = new RegExp(e.pattern);
|
|
72
|
+
r = r.check(
|
|
73
|
+
s ? u((i) => n.test(i), { error: s }) : B(n)
|
|
70
74
|
);
|
|
71
75
|
}
|
|
72
76
|
if (e.enum) {
|
|
73
|
-
const
|
|
74
|
-
|
|
77
|
+
const s = e.enum, n = o(e.messages?.enum, t);
|
|
78
|
+
r = r.check(u((i) => s.includes(i), n ? { error: n } : void 0));
|
|
75
79
|
}
|
|
76
80
|
if (e.const !== void 0) {
|
|
77
|
-
const
|
|
78
|
-
|
|
81
|
+
const s = o(e.messages?.const, t);
|
|
82
|
+
r = r.check(u((n) => n === e.const, s ? { error: s } : void 0));
|
|
79
83
|
}
|
|
80
84
|
if (e.format !== void 0)
|
|
81
|
-
if (
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
if (L.includes(e.format)) {
|
|
86
|
+
const s = o(e.messages?.format, t);
|
|
87
|
+
r = r.check(
|
|
88
|
+
s ? h((n, i) => {
|
|
89
|
+
const d = I(
|
|
90
|
+
k[e.format].schema,
|
|
91
|
+
n
|
|
88
92
|
);
|
|
89
|
-
|
|
90
|
-
}) :
|
|
93
|
+
b(d) || i.addIssue({ code: "custom", path: [], message: s });
|
|
94
|
+
}) : k[e.format].schema
|
|
91
95
|
);
|
|
92
96
|
} else
|
|
93
97
|
throw new Error(
|
|
94
|
-
`Unknown string validation format: ${JSON.stringify(e.format)}. The "format" property must be one of: ${
|
|
98
|
+
`Unknown string validation format: ${JSON.stringify(e.format)}. The "format" property must be one of: ${L.map((s) => `"${s}"`).join(", ")}.`
|
|
95
99
|
);
|
|
96
|
-
return
|
|
100
|
+
return r;
|
|
97
101
|
});
|
|
98
102
|
}
|
|
99
|
-
const
|
|
100
|
-
if (
|
|
103
|
+
const Q = (m, t) => {
|
|
104
|
+
if (t === 0)
|
|
101
105
|
return !1;
|
|
102
|
-
const e =
|
|
106
|
+
const e = m / t;
|
|
103
107
|
return Math.abs(e - Math.round(e)) < 1e-10;
|
|
104
108
|
};
|
|
105
|
-
function
|
|
106
|
-
return f(
|
|
107
|
-
const c = o(e.messages?.invalid,
|
|
108
|
-
let
|
|
109
|
-
if (e.type === "integer" && (
|
|
110
|
-
const
|
|
111
|
-
|
|
109
|
+
function W(m, t) {
|
|
110
|
+
return f(m, (e) => {
|
|
111
|
+
const c = o(e.messages?.invalid, t);
|
|
112
|
+
let r = c ? S(c) : S();
|
|
113
|
+
if (e.type === "integer" && (r = r.check(P())), e.minimum !== void 0) {
|
|
114
|
+
const s = o(e.messages?.minimum, t), n = e.minimum;
|
|
115
|
+
r = r.check(s ? u((i) => i >= n, { error: s }) : G(n));
|
|
112
116
|
}
|
|
113
117
|
if (e.maximum !== void 0) {
|
|
114
|
-
const
|
|
115
|
-
|
|
118
|
+
const s = o(e.messages?.maximum, t), n = e.maximum;
|
|
119
|
+
r = r.check(s ? u((i) => i <= n, { error: s }) : J(n));
|
|
116
120
|
}
|
|
117
121
|
if (e.exclusiveMinimum !== void 0) {
|
|
118
|
-
const
|
|
119
|
-
|
|
122
|
+
const s = o(e.messages?.exclusiveMinimum, t), n = e.exclusiveMinimum;
|
|
123
|
+
r = r.check(u((i) => i > n, s ? { error: s } : void 0));
|
|
120
124
|
}
|
|
121
125
|
if (e.exclusiveMaximum !== void 0) {
|
|
122
|
-
const
|
|
123
|
-
|
|
126
|
+
const s = o(e.messages?.exclusiveMaximum, t), n = e.exclusiveMaximum;
|
|
127
|
+
r = r.check(u((i) => i < n, s ? { error: s } : void 0));
|
|
124
128
|
}
|
|
125
129
|
if (e.multipleOf !== void 0) {
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
u((
|
|
130
|
+
const s = o(e.messages?.multipleOf, t), n = e.multipleOf;
|
|
131
|
+
r = r.check(
|
|
132
|
+
u((i) => Q(i, n), s ? { error: s } : void 0)
|
|
129
133
|
);
|
|
130
134
|
}
|
|
131
135
|
if (e.enum) {
|
|
132
|
-
const
|
|
133
|
-
|
|
136
|
+
const s = e.enum, n = o(e.messages?.enum, t);
|
|
137
|
+
r = r.check(u((i) => s.includes(i), n ? { error: n } : void 0));
|
|
134
138
|
}
|
|
135
139
|
if (e.const !== void 0) {
|
|
136
|
-
const
|
|
137
|
-
|
|
140
|
+
const s = o(e.messages?.const, t);
|
|
141
|
+
r = r.check(u((n) => n === e.const, s ? { error: s } : void 0));
|
|
138
142
|
}
|
|
139
|
-
return
|
|
143
|
+
return r;
|
|
140
144
|
});
|
|
141
145
|
}
|
|
142
|
-
function
|
|
143
|
-
return f(
|
|
144
|
-
const c = o(e.messages?.invalid,
|
|
145
|
-
let
|
|
146
|
+
function H(m, t) {
|
|
147
|
+
return f(m, (e) => {
|
|
148
|
+
const c = o(e.messages?.invalid, t);
|
|
149
|
+
let r = c ? q(c) : q();
|
|
146
150
|
if (e.const !== void 0) {
|
|
147
|
-
const
|
|
148
|
-
|
|
151
|
+
const s = o(e.messages?.const, t);
|
|
152
|
+
r = r.check(u((n) => n === e.const, s ? { error: s } : void 0));
|
|
149
153
|
}
|
|
150
|
-
return
|
|
154
|
+
return r;
|
|
151
155
|
});
|
|
152
156
|
}
|
|
153
|
-
function
|
|
154
|
-
return f(
|
|
155
|
-
const c = o(e.messages?.invalid,
|
|
156
|
-
let
|
|
157
|
+
function X(m, t) {
|
|
158
|
+
return f(m, (e) => {
|
|
159
|
+
const c = o(e.messages?.invalid, t);
|
|
160
|
+
let r = c ? l(a(), c) : l(a());
|
|
157
161
|
if (e.required === !0) {
|
|
158
|
-
const
|
|
159
|
-
|
|
162
|
+
const s = o(e.messages?.required, t) ?? "This field is required";
|
|
163
|
+
r = r.check(u((n) => n.length > 0, { error: s }));
|
|
160
164
|
}
|
|
161
165
|
if (typeof e.minItems == "number") {
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
166
|
+
const s = o(e.messages?.minItems, t), n = e.minItems;
|
|
167
|
+
r = r.check(
|
|
168
|
+
s ? u((i) => i.length >= n, { error: s }) : x(n)
|
|
165
169
|
);
|
|
166
170
|
}
|
|
167
171
|
if (typeof e.maxItems == "number") {
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
172
|
+
const s = o(e.messages?.maxItems, t), n = e.maxItems;
|
|
173
|
+
r = r.check(
|
|
174
|
+
s ? u((i) => i.length <= n, { error: s }) : M(n)
|
|
171
175
|
);
|
|
172
176
|
}
|
|
173
|
-
return
|
|
177
|
+
return r;
|
|
174
178
|
});
|
|
175
179
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return
|
|
180
|
+
const V = "This field is required", U = "Invalid file", F = "Wait for the upload to finish";
|
|
181
|
+
function y(m) {
|
|
182
|
+
if (m === null || typeof m != "object")
|
|
183
|
+
return !1;
|
|
184
|
+
const t = m;
|
|
185
|
+
return typeof t.id == "string" && typeof t.name == "string" && typeof t.size == "number" && typeof t.type == "string" && typeof t.status == "string" && C.includes(t.status);
|
|
186
|
+
}
|
|
187
|
+
function Y(m, t) {
|
|
188
|
+
const e = o(m.messages?.invalid, t) ?? U, c = o(m.messages?.required, t) ?? V, r = o(m.messages?.pendingUploads, t) ?? F, s = m.blockPendingUploads !== !1;
|
|
189
|
+
return a().check(
|
|
190
|
+
h((n, i) => {
|
|
191
|
+
if (n == null) {
|
|
192
|
+
m.required === !0 && i.addIssue({ code: "custom", message: c });
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (!y(n)) {
|
|
196
|
+
i.addIssue({ code: "custom", message: e });
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
s && n.status !== "uploaded" && i.addIssue({ code: "custom", message: r });
|
|
200
|
+
})
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
function Z(m, t) {
|
|
204
|
+
const e = o(m.messages?.required, t) ?? V, c = z(m, t, e);
|
|
205
|
+
return a().check(
|
|
206
|
+
h((r, s) => {
|
|
207
|
+
if (r == null) {
|
|
208
|
+
m.required === !0 && s.addIssue({ code: "custom", message: e });
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const n = I(
|
|
212
|
+
c,
|
|
213
|
+
r
|
|
214
|
+
);
|
|
215
|
+
if (!b(n))
|
|
216
|
+
for (const i of n.issues)
|
|
217
|
+
s.addIssue({ code: "custom", message: i.message });
|
|
218
|
+
})
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
function z(m, t, e) {
|
|
222
|
+
const c = o(m.messages?.invalid, t) ?? U;
|
|
223
|
+
let r = l(a(), c).check(
|
|
224
|
+
u((s) => s.every(y), { error: c })
|
|
225
|
+
);
|
|
226
|
+
if (m.required === !0 && (r = r.check(u((s) => s.length > 0, { error: e }))), typeof m.minItems == "number") {
|
|
227
|
+
const s = o(m.messages?.minItems, t), n = m.minItems;
|
|
228
|
+
r = r.check(
|
|
229
|
+
s ? u((i) => i.length >= n, { error: s }) : x(n)
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
if (typeof m.maxItems == "number") {
|
|
233
|
+
const s = o(m.messages?.maxItems, t), n = m.maxItems;
|
|
234
|
+
r = r.check(
|
|
235
|
+
s ? u((i) => i.length <= n, { error: s }) : M(n)
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
if (m.blockPendingUploads !== !1) {
|
|
239
|
+
const s = o(m.messages?.pendingUploads, t) ?? F;
|
|
240
|
+
r = r.check(
|
|
241
|
+
u(
|
|
242
|
+
(n) => n.every((i) => !y(i) || i.status === "uploaded"),
|
|
243
|
+
{ error: s }
|
|
244
|
+
)
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
return r;
|
|
248
|
+
}
|
|
249
|
+
function v(m, t) {
|
|
250
|
+
return f(m, (e) => {
|
|
251
|
+
let c = a();
|
|
252
|
+
return A(
|
|
180
253
|
Object.keys(e),
|
|
181
254
|
// filter non-custom validator keys
|
|
182
|
-
(
|
|
183
|
-
(
|
|
184
|
-
const
|
|
185
|
-
if (!
|
|
186
|
-
const
|
|
255
|
+
(r) => r !== "type" && r !== "required",
|
|
256
|
+
(r) => {
|
|
257
|
+
const s = e[r], n = t[r];
|
|
258
|
+
if (!n) {
|
|
259
|
+
const d = Object.keys(t).map((p) => `"${p}"`).join(", ");
|
|
187
260
|
throw new Error(
|
|
188
|
-
`Unknown custom validator rule "${
|
|
261
|
+
`Unknown custom validator rule "${r}". Registered rules: ${d || "(none)"}.`
|
|
189
262
|
);
|
|
190
263
|
}
|
|
191
|
-
const
|
|
264
|
+
const i = n(s);
|
|
192
265
|
c = c.check(
|
|
193
|
-
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
266
|
+
h((d, p) => {
|
|
267
|
+
const E = I(
|
|
268
|
+
i,
|
|
269
|
+
d
|
|
197
270
|
);
|
|
198
|
-
if (!
|
|
199
|
-
const
|
|
200
|
-
|
|
271
|
+
if (!b(E)) {
|
|
272
|
+
const O = E.issues[0];
|
|
273
|
+
p.addIssue({
|
|
201
274
|
code: "custom",
|
|
202
275
|
path: [],
|
|
203
|
-
message:
|
|
276
|
+
message: O?.message || `Validation failed for ${r}`
|
|
204
277
|
});
|
|
205
278
|
}
|
|
206
279
|
})
|
|
@@ -209,10 +282,10 @@ function H(i, n) {
|
|
|
209
282
|
), c;
|
|
210
283
|
});
|
|
211
284
|
}
|
|
212
|
-
function f(
|
|
213
|
-
const e =
|
|
214
|
-
return
|
|
285
|
+
function f(m, t) {
|
|
286
|
+
const e = t(m);
|
|
287
|
+
return m.required ? e : R(e);
|
|
215
288
|
}
|
|
216
289
|
export {
|
|
217
|
-
|
|
290
|
+
ne as initValidators
|
|
218
291
|
};
|
package/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i,
|
|
1
|
+
(function(i,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("@golemui/core"),require("@standard-schema/spec"),require("zod"),require("zod/mini")):typeof define=="function"&&define.amd?define(["exports","@golemui/core","@standard-schema/spec","zod","zod/mini"],d):(i=typeof globalThis<"u"?globalThis:i||self,d(i["gui-validators"]={},i.core,null,i.zod,i.mini))})(this,(function(i,d,j,h,o){"use strict";const p={email:{schema:o.email()},hostname:{schema:o.hostname()},ipv4:{schema:o.ipv4()},ipv6:{schema:o.ipv6()},url:{schema:o.url()},uuid:{schema:o.uuid()},date:{schema:h.iso.date()},time:{schema:h.iso.time()},"date-time":{schema:h.iso.datetime({local:!0,offset:!0})},duration:{schema:h.iso.duration()}},k=Object.keys(p),S=["uploading","uploaded","error"],q=u=>(t,e)=>{switch(t.type){case"string":return E(t,e);case"integer":case"number":return L(t,e);case"boolean":return w(t,e);case"array":return O(t,e);case"file":return T(t,e);case"files":return U(t,e);case"custom":{if(!u)throw new Error('Validator type "custom" requires a customValidators object, but it was not supplied.');return A(t,u)}default:{const m=t;throw new Error(`Unknown validator config: ${JSON.stringify(m)}. The "type" property must be one of: "string", "number", "integer", "boolean", "array", "file", "files", "custom".`)}}};function a(u,t){if(u!==void 0)return typeof u=="string"?u:t?.translate(u.key,u.params,u.default)??u.default}function E(u,t){return f(u,e=>{const m=a(e.messages?.invalid,t);let r=m?o.string(m):o.string();if(e.required===!0){const s=a(e.messages?.required,t)??"This field is required";r=r.check(o.refine(n=>n.length>0,{error:s}))}if(typeof e.minLength=="number"){const s=a(e.messages?.minLength,t),n=e.minLength;r=r.check(s?o.refine(c=>c.length>=n,{error:s}):o.minLength(n))}if(typeof e.maxLength=="number"){const s=a(e.messages?.maxLength,t),n=e.maxLength;r=r.check(s?o.refine(c=>c.length<=n,{error:s}):o.maxLength(n))}if(typeof e.pattern=="string"){const s=a(e.messages?.pattern,t),n=new RegExp(e.pattern);r=r.check(s?o.refine(c=>n.test(c),{error:s}):o.regex(n))}if(e.enum){const s=e.enum,n=a(e.messages?.enum,t);r=r.check(o.refine(c=>s.includes(c),n?{error:n}:void 0))}if(e.const!==void 0){const s=a(e.messages?.const,t);r=r.check(o.refine(n=>n===e.const,s?{error:s}:void 0))}if(e.format!==void 0)if(k.includes(e.format)){const s=a(e.messages?.format,t);r=r.check(s?o.superRefine((n,c)=>{const g=d.standardValidate(p[e.format].schema,n);d.isStandardValidateSuccess(g)||c.addIssue({code:"custom",path:[],message:s})}):p[e.format].schema)}else throw new Error(`Unknown string validation format: ${JSON.stringify(e.format)}. The "format" property must be one of: ${k.map(s=>`"${s}"`).join(", ")}.`);return r})}const V=(u,t)=>{if(t===0)return!1;const e=u/t;return Math.abs(e-Math.round(e))<1e-10};function L(u,t){return f(u,e=>{const m=a(e.messages?.invalid,t);let r=m?o.number(m):o.number();if(e.type==="integer"&&(r=r.check(o.int())),e.minimum!==void 0){const s=a(e.messages?.minimum,t),n=e.minimum;r=r.check(s?o.refine(c=>c>=n,{error:s}):o.minimum(n))}if(e.maximum!==void 0){const s=a(e.messages?.maximum,t),n=e.maximum;r=r.check(s?o.refine(c=>c<=n,{error:s}):o.maximum(n))}if(e.exclusiveMinimum!==void 0){const s=a(e.messages?.exclusiveMinimum,t),n=e.exclusiveMinimum;r=r.check(o.refine(c=>c>n,s?{error:s}:void 0))}if(e.exclusiveMaximum!==void 0){const s=a(e.messages?.exclusiveMaximum,t),n=e.exclusiveMaximum;r=r.check(o.refine(c=>c<n,s?{error:s}:void 0))}if(e.multipleOf!==void 0){const s=a(e.messages?.multipleOf,t),n=e.multipleOf;r=r.check(o.refine(c=>V(c,n),s?{error:s}:void 0))}if(e.enum){const s=e.enum,n=a(e.messages?.enum,t);r=r.check(o.refine(c=>s.includes(c),n?{error:n}:void 0))}if(e.const!==void 0){const s=a(e.messages?.const,t);r=r.check(o.refine(n=>n===e.const,s?{error:s}:void 0))}return r})}function w(u,t){return f(u,e=>{const m=a(e.messages?.invalid,t);let r=m?o.boolean(m):o.boolean();if(e.const!==void 0){const s=a(e.messages?.const,t);r=r.check(o.refine(n=>n===e.const,s?{error:s}:void 0))}return r})}function O(u,t){return f(u,e=>{const m=a(e.messages?.invalid,t);let r=m?o.array(o.any(),m):o.array(o.any());if(e.required===!0){const s=a(e.messages?.required,t)??"This field is required";r=r.check(o.refine(n=>n.length>0,{error:s}))}if(typeof e.minItems=="number"){const s=a(e.messages?.minItems,t),n=e.minItems;r=r.check(s?o.refine(c=>c.length>=n,{error:s}):o.minLength(n))}if(typeof e.maxItems=="number"){const s=a(e.messages?.maxItems,t),n=e.maxItems;r=r.check(s?o.refine(c=>c.length<=n,{error:s}):o.maxLength(n))}return r})}const b="This field is required",I="Invalid file",x="Wait for the upload to finish";function l(u){if(u===null||typeof u!="object")return!1;const t=u;return typeof t.id=="string"&&typeof t.name=="string"&&typeof t.size=="number"&&typeof t.type=="string"&&typeof t.status=="string"&&S.includes(t.status)}function T(u,t){const e=a(u.messages?.invalid,t)??I,m=a(u.messages?.required,t)??b,r=a(u.messages?.pendingUploads,t)??x,s=u.blockPendingUploads!==!1;return o.any().check(o.superRefine((n,c)=>{if(n==null){u.required===!0&&c.addIssue({code:"custom",message:m});return}if(!l(n)){c.addIssue({code:"custom",message:e});return}s&&n.status!=="uploaded"&&c.addIssue({code:"custom",message:r})}))}function U(u,t){const e=a(u.messages?.required,t)??b,m=F(u,t,e);return o.any().check(o.superRefine((r,s)=>{if(r==null){u.required===!0&&s.addIssue({code:"custom",message:e});return}const n=d.standardValidate(m,r);if(!d.isStandardValidateSuccess(n))for(const c of n.issues)s.addIssue({code:"custom",message:c.message})}))}function F(u,t,e){const m=a(u.messages?.invalid,t)??I;let r=o.array(o.any(),m).check(o.refine(s=>s.every(l),{error:m}));if(u.required===!0&&(r=r.check(o.refine(s=>s.length>0,{error:e}))),typeof u.minItems=="number"){const s=a(u.messages?.minItems,t),n=u.minItems;r=r.check(s?o.refine(c=>c.length>=n,{error:s}):o.minLength(n))}if(typeof u.maxItems=="number"){const s=a(u.messages?.maxItems,t),n=u.maxItems;r=r.check(s?o.refine(c=>c.length<=n,{error:s}):o.maxLength(n))}if(u.blockPendingUploads!==!1){const s=a(u.messages?.pendingUploads,t)??x;r=r.check(o.refine(n=>n.every(c=>!l(c)||c.status==="uploaded"),{error:s}))}return r}function A(u,t){return f(u,e=>{let m=o.any();return d.filterTap(Object.keys(e),r=>r!=="type"&&r!=="required",r=>{const s=e[r],n=t[r];if(!n){const g=Object.keys(t).map(y=>`"${y}"`).join(", ");throw new Error(`Unknown custom validator rule "${r}". Registered rules: ${g||"(none)"}.`)}const c=n(s);m=m.check(o.superRefine((g,y)=>{const M=d.standardValidate(c,g);if(!d.isStandardValidateSuccess(M)){const _=M.issues[0];y.addIssue({code:"custom",path:[],message:_?.message||`Validation failed for ${r}`})}}))}),m})}function f(u,t){const e=t(u);return u.required?e:o.optional(e)}i.initValidators=q,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|
package/lib/validators.d.ts
CHANGED
|
@@ -65,6 +65,26 @@ export interface ArrayValidator extends BaseValidator {
|
|
|
65
65
|
uniqueItems?: boolean;
|
|
66
66
|
items?: Validator;
|
|
67
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Validates the envelope stored by the `fileUpload` widget (`FileItem | null`).
|
|
70
|
+
* `blockPendingUploads` (default `true`) fails while the item's status is not
|
|
71
|
+
* `uploaded`, so an in-progress or failed upload cannot be submitted.
|
|
72
|
+
*/
|
|
73
|
+
export interface FileValidator extends BaseValidator {
|
|
74
|
+
type: 'file';
|
|
75
|
+
blockPendingUploads?: boolean;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Validates the envelope array stored by the `multiFileUpload` widget
|
|
79
|
+
* (`FileItem[]`). `required` means non-empty, like `array`. An absent value
|
|
80
|
+
* (`undefined` or `null`) counts as empty.
|
|
81
|
+
*/
|
|
82
|
+
export interface FilesValidator extends BaseValidator {
|
|
83
|
+
type: 'files';
|
|
84
|
+
minItems?: number;
|
|
85
|
+
maxItems?: number;
|
|
86
|
+
blockPendingUploads?: boolean;
|
|
87
|
+
}
|
|
68
88
|
export interface CustomValidator {
|
|
69
89
|
type: 'custom';
|
|
70
90
|
required?: boolean;
|
|
@@ -74,6 +94,6 @@ export type CustomValidatorSchemaFn = (input: any) => StandardSchemaV1;
|
|
|
74
94
|
export type CustomValidatorSchemas = {
|
|
75
95
|
[key: string]: CustomValidatorSchemaFn;
|
|
76
96
|
};
|
|
77
|
-
export type Validator = StringValidator | NumberValidator | BooleanValidator | ArrayValidator | CustomValidator;
|
|
97
|
+
export type Validator = StringValidator | NumberValidator | BooleanValidator | ArrayValidator | FileValidator | FilesValidator | CustomValidator;
|
|
78
98
|
export declare const initValidators: (customValidators?: CustomValidatorSchemas) => ValidatorFn<Validator>;
|
|
79
99
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@golemui/gui-validators",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0-rc.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"zod": "^4.0.0",
|
|
28
28
|
"@standard-schema/spec": "^1.0.0",
|
|
29
|
-
"@golemui/core": "1.
|
|
29
|
+
"@golemui/core": "1.5.0-rc.1"
|
|
30
30
|
},
|
|
31
31
|
"repository": {
|
|
32
32
|
"type": "git",
|