@ciwergrp/nuxid 1.6.1 → 1.6.3
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/dist/module.json +1 -1
- package/dist/module.mjs +8 -0
- package/docs/.docs/built-in-modules/element-plus.md +28 -0
- package/docs/.docs/built-in-modules/index.md +15 -0
- package/docs/.docs/built-in-modules/lodash.md +14 -0
- package/docs/.docs/built-in-modules/nuxt-icon.md +31 -0
- package/docs/.docs/built-in-modules/sentry.md +13 -0
- package/docs/.docs/built-in-modules/vueuse.md +28 -0
- package/docs/.docs/composables/use-breadcrumbs.md +53 -0
- package/docs/.docs/composables/use-cursor-http.md +56 -0
- package/docs/.docs/composables/use-http.md +57 -0
- package/docs/.docs/composables/use-pinia.md +65 -0
- package/docs/.docs/composables/use-query-filters.md +88 -0
- package/docs/.docs/composables/use-route-query.md +39 -0
- package/docs/.docs/composables/use-title.md +48 -0
- package/docs/.docs/composables/use-version-updater.md +38 -0
- package/docs/.docs/configuration.md +164 -0
- package/docs/.docs/form-validation/create-validation-rules.md +130 -0
- package/docs/.docs/form-validation/quick-start.md +67 -0
- package/docs/.docs/form-validation/request-generator.md +38 -0
- package/docs/.docs/form-validation/rules.md +418 -0
- package/docs/.docs/forms/use-form.md +58 -0
- package/docs/.docs/helpers/array.md +169 -0
- package/docs/.docs/helpers/date.md +702 -0
- package/docs/.docs/helpers/index.md +34 -0
- package/docs/.docs/helpers/number.md +169 -0
- package/docs/.docs/helpers/object.md +85 -0
- package/docs/.docs/helpers/string.md +316 -0
- package/docs/.docs/index.md +17 -0
- package/docs/.docs/structure-directory.md +28 -0
- package/package.json +3 -1
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Validation Rules
|
|
3
|
+
outline: [2, 3]
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Validation Rules
|
|
7
|
+
|
|
8
|
+
This page documents all client-side validation rules supported by `src/runtime/validator.ts`.
|
|
9
|
+
Use these rules with `createValidationRules` by passing an array of rule strings per field.
|
|
10
|
+
|
|
11
|
+
Common usage:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
const rules = createValidationRules(
|
|
15
|
+
{
|
|
16
|
+
name: ['required', 'string', 'min:2'],
|
|
17
|
+
email: ['required', 'email'],
|
|
18
|
+
},
|
|
19
|
+
formState,
|
|
20
|
+
)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Notes:
|
|
24
|
+
- Empty means `null`, `undefined`, or `''`.
|
|
25
|
+
- Many rules skip validation on empty values unless the rule itself implies required behavior.
|
|
26
|
+
- If a field has both `required` and `nullable`, the validator throws a runtime error.
|
|
27
|
+
|
|
28
|
+
## Rule Reference
|
|
29
|
+
|
|
30
|
+
### required
|
|
31
|
+
- Summary: Value must be present.
|
|
32
|
+
- Technique: Element Plus `required: true`.
|
|
33
|
+
- Use: `'required'`
|
|
34
|
+
- Example: `['required']`
|
|
35
|
+
|
|
36
|
+
### string
|
|
37
|
+
- Summary: Value must be a string.
|
|
38
|
+
- Technique: `typeof value === 'string'` (empty values pass).
|
|
39
|
+
- Use: `'string'`
|
|
40
|
+
- Example: `['string']`
|
|
41
|
+
|
|
42
|
+
### nullable
|
|
43
|
+
- Summary: Allows empty values and skips remaining rules when empty.
|
|
44
|
+
- Technique: Early exit when empty and not `required`.
|
|
45
|
+
- Use: `'nullable'`
|
|
46
|
+
- Example: `['nullable', 'email']`
|
|
47
|
+
|
|
48
|
+
### email
|
|
49
|
+
- Summary: Must be a valid email address.
|
|
50
|
+
- Technique: Element Plus `type: 'email'`.
|
|
51
|
+
- Use: `'email'`
|
|
52
|
+
- Example: `['email']`
|
|
53
|
+
|
|
54
|
+
### accepted
|
|
55
|
+
- Summary: Must be accepted (yes/on/1/true).
|
|
56
|
+
- Technique: String/boolean normalization check.
|
|
57
|
+
- Use: `'accepted'`
|
|
58
|
+
- Example: `['accepted']`
|
|
59
|
+
|
|
60
|
+
### accepted_if:field,value,...
|
|
61
|
+
- Summary: Must be accepted if another field equals any value.
|
|
62
|
+
- Technique: Compare other field then apply `accepted` check.
|
|
63
|
+
- Use: `'accepted_if:newsletter,true'`
|
|
64
|
+
- Example: `['accepted_if:newsletter,true']`
|
|
65
|
+
|
|
66
|
+
### boolean
|
|
67
|
+
- Summary: Must be boolean-like (true/false/0/1/'0'/'1').
|
|
68
|
+
- Technique: Simple membership check.
|
|
69
|
+
- Use: `'boolean'`
|
|
70
|
+
- Example: `['boolean']`
|
|
71
|
+
|
|
72
|
+
### filled
|
|
73
|
+
- Summary: If field exists, it must not be empty.
|
|
74
|
+
- Technique: Checks presence + non-empty value.
|
|
75
|
+
- Use: `'filled'`
|
|
76
|
+
- Example: `['filled']`
|
|
77
|
+
|
|
78
|
+
### present
|
|
79
|
+
- Summary: Field key must exist in the form state.
|
|
80
|
+
- Technique: Path existence check (`hasNestedKey`).
|
|
81
|
+
- Use: `'present'`
|
|
82
|
+
- Example: `['present']`
|
|
83
|
+
|
|
84
|
+
### required_if:field,value,...
|
|
85
|
+
- Summary: Required when another field equals any value.
|
|
86
|
+
- Technique: Compare other field then require non-empty.
|
|
87
|
+
- Use: `'required_if:status,active'`
|
|
88
|
+
- Example: `['required_if:status,active']`
|
|
89
|
+
|
|
90
|
+
### required_unless:field,value,...
|
|
91
|
+
- Summary: Required unless another field equals any value.
|
|
92
|
+
- Technique: Compare other field then require non-empty.
|
|
93
|
+
- Use: `'required_unless:role,admin'`
|
|
94
|
+
- Example: `['required_unless:role,admin']`
|
|
95
|
+
|
|
96
|
+
### required_with:field1,field2,...
|
|
97
|
+
- Summary: Required if any listed field is filled.
|
|
98
|
+
- Technique: `hasAnyFilled` then require non-empty.
|
|
99
|
+
- Use: `'required_with:phone,email'`
|
|
100
|
+
- Example: `['required_with:phone,email']`
|
|
101
|
+
|
|
102
|
+
### required_with_all:field1,field2,...
|
|
103
|
+
- Summary: Required if all listed fields are filled.
|
|
104
|
+
- Technique: `hasAllFilled` then require non-empty.
|
|
105
|
+
- Use: `'required_with_all:lat,lng'`
|
|
106
|
+
- Example: `['required_with_all:lat,lng']`
|
|
107
|
+
|
|
108
|
+
### required_without:field1,field2,...
|
|
109
|
+
- Summary: Required if any listed field is empty.
|
|
110
|
+
- Technique: Require non-empty unless any other is filled.
|
|
111
|
+
- Use: `'required_without:email,phone'`
|
|
112
|
+
- Example: `['required_without:email,phone']`
|
|
113
|
+
|
|
114
|
+
### required_without_all:field1,field2,...
|
|
115
|
+
- Summary: Required if all listed fields are empty.
|
|
116
|
+
- Technique: Require non-empty unless all others are filled.
|
|
117
|
+
- Use: `'required_without_all:email,phone'`
|
|
118
|
+
- Example: `['required_without_all:email,phone']`
|
|
119
|
+
|
|
120
|
+
### prohibited
|
|
121
|
+
- Summary: Must be empty.
|
|
122
|
+
- Technique: Fail if value is filled.
|
|
123
|
+
- Use: `'prohibited'`
|
|
124
|
+
- Example: `['prohibited']`
|
|
125
|
+
|
|
126
|
+
### prohibited_if:field,value,...
|
|
127
|
+
- Summary: Must be empty when another field equals any value.
|
|
128
|
+
- Technique: Compare other field then fail if filled.
|
|
129
|
+
- Use: `'prohibited_if:role,guest'`
|
|
130
|
+
- Example: `['prohibited_if:role,guest']`
|
|
131
|
+
|
|
132
|
+
### prohibited_unless:field,value,...
|
|
133
|
+
- Summary: Must be empty unless another field equals any value.
|
|
134
|
+
- Technique: Compare other field then fail if filled.
|
|
135
|
+
- Use: `'prohibited_unless:role,admin'`
|
|
136
|
+
- Example: `['prohibited_unless:role,admin']`
|
|
137
|
+
|
|
138
|
+
### prohibits:field1,field2,...
|
|
139
|
+
- Summary: If this field is filled, listed fields must be empty.
|
|
140
|
+
- Technique: On filled value, check other fields for emptiness.
|
|
141
|
+
- Use: `'prohibits:password'`
|
|
142
|
+
- Example: `['prohibits:password']`
|
|
143
|
+
|
|
144
|
+
### min:n
|
|
145
|
+
- Summary: Minimum length (string) / count (array) / numeric min.
|
|
146
|
+
- Technique: Element Plus `min` with trim.
|
|
147
|
+
- Use: `'min:3'`
|
|
148
|
+
- Example: `['min:3']`
|
|
149
|
+
|
|
150
|
+
### max:n
|
|
151
|
+
- Summary: Maximum length (string) / count (array) / numeric max.
|
|
152
|
+
- Technique: Element Plus `max` with trim.
|
|
153
|
+
- Use: `'max:255'`
|
|
154
|
+
- Example: `['max:255']`
|
|
155
|
+
|
|
156
|
+
### between:min,max
|
|
157
|
+
- Summary: Size between min and max.
|
|
158
|
+
- Technique: Size calculator (number, string length, array length, object keys).
|
|
159
|
+
- Use: `'between:2,10'`
|
|
160
|
+
- Example: `['between:2,10']`
|
|
161
|
+
|
|
162
|
+
### size:n
|
|
163
|
+
- Summary: Exact size.
|
|
164
|
+
- Technique: Same size calculator as `between`.
|
|
165
|
+
- Use: `'size:3'`
|
|
166
|
+
- Example: `['size:3']`
|
|
167
|
+
|
|
168
|
+
### same:field
|
|
169
|
+
- Summary: Must match another field.
|
|
170
|
+
- Technique: Compare with `getNestedValue`.
|
|
171
|
+
- Use: `'same:password'`
|
|
172
|
+
- Example: `['same:password']`
|
|
173
|
+
|
|
174
|
+
### different:field
|
|
175
|
+
- Summary: Must be different from another field.
|
|
176
|
+
- Technique: Compare with `getNestedValue`.
|
|
177
|
+
- Use: `'different:username'`
|
|
178
|
+
- Example: `['different:username']`
|
|
179
|
+
|
|
180
|
+
### alpha
|
|
181
|
+
- Summary: Letters only.
|
|
182
|
+
- Technique: Regex `/^[a-z]+$/i`.
|
|
183
|
+
- Use: `'alpha'`
|
|
184
|
+
- Example: `['alpha']`
|
|
185
|
+
|
|
186
|
+
### alpha_num
|
|
187
|
+
- Summary: Letters and numbers only.
|
|
188
|
+
- Technique: Regex `/^[a-z0-9]+$/i`.
|
|
189
|
+
- Use: `'alpha_num'`
|
|
190
|
+
- Example: `['alpha_num']`
|
|
191
|
+
|
|
192
|
+
### alpha_dash
|
|
193
|
+
- Summary: Letters, numbers, dashes, underscores.
|
|
194
|
+
- Technique: Regex `/^[\\w-]+$/`.
|
|
195
|
+
- Use: `'alpha_dash'`
|
|
196
|
+
- Example: `['alpha_dash']`
|
|
197
|
+
|
|
198
|
+
### alphanumeric_space
|
|
199
|
+
- Summary: Letters, numbers, spaces, dashes, underscores.
|
|
200
|
+
- Technique: Regex `/^[\\w -]+$/`.
|
|
201
|
+
- Use: `'alphanumeric_space'`
|
|
202
|
+
- Example: `['alphanumeric_space']`
|
|
203
|
+
|
|
204
|
+
### alpha_space
|
|
205
|
+
- Summary: Letters, spaces, dashes, underscores.
|
|
206
|
+
- Technique: Regex `/^[a-z _-]+$/i`.
|
|
207
|
+
- Use: `'alpha_space'`
|
|
208
|
+
- Example: `['alpha_space']`
|
|
209
|
+
|
|
210
|
+
### numeric
|
|
211
|
+
- Summary: Must be numeric.
|
|
212
|
+
- Technique: `Number(value)` and `isNaN` check.
|
|
213
|
+
- Use: `'numeric'`
|
|
214
|
+
- Example: `['numeric']`
|
|
215
|
+
|
|
216
|
+
### integer
|
|
217
|
+
- Summary: Must be an integer.
|
|
218
|
+
- Technique: `Number.isInteger(Number(value))`.
|
|
219
|
+
- Use: `'integer'`
|
|
220
|
+
- Example: `['integer']`
|
|
221
|
+
|
|
222
|
+
### confirmed
|
|
223
|
+
- Summary: Must match `<field>_confirmation`.
|
|
224
|
+
- Technique: Compare with confirmation field path.
|
|
225
|
+
- Use: `'confirmed'`
|
|
226
|
+
- Example: `['confirmed']`
|
|
227
|
+
|
|
228
|
+
### digits:n
|
|
229
|
+
- Summary: Must be numeric string with exact length.
|
|
230
|
+
- Technique: Regex + length check.
|
|
231
|
+
- Use: `'digits:6'`
|
|
232
|
+
- Example: `['digits:6']`
|
|
233
|
+
|
|
234
|
+
### digits_between:min,max
|
|
235
|
+
- Summary: Must be numeric string with length in range.
|
|
236
|
+
- Technique: Regex + length range check.
|
|
237
|
+
- Use: `'digits_between:6,8'`
|
|
238
|
+
- Example: `['digits_between:6,8']`
|
|
239
|
+
|
|
240
|
+
### bail
|
|
241
|
+
- Summary: Placeholder (no-op).
|
|
242
|
+
- Technique: No behavior; included for parity.
|
|
243
|
+
- Use: `'bail'`
|
|
244
|
+
- Example: `['bail', 'required']`
|
|
245
|
+
|
|
246
|
+
### gt:field
|
|
247
|
+
- Summary: Must be greater than another field.
|
|
248
|
+
- Technique: Numeric comparison via `Number`.
|
|
249
|
+
- Use: `'gt:other'`
|
|
250
|
+
- Example: `['gt:other']`
|
|
251
|
+
|
|
252
|
+
### gte:field
|
|
253
|
+
- Summary: Must be greater than or equal to another field.
|
|
254
|
+
- Technique: Numeric comparison via `Number`.
|
|
255
|
+
- Use: `'gte:other'`
|
|
256
|
+
- Example: `['gte:other']`
|
|
257
|
+
|
|
258
|
+
### lt:field
|
|
259
|
+
- Summary: Must be less than another field.
|
|
260
|
+
- Technique: Numeric comparison via `Number`.
|
|
261
|
+
- Use: `'lt:other'`
|
|
262
|
+
- Example: `['lt:other']`
|
|
263
|
+
|
|
264
|
+
### lte:field
|
|
265
|
+
- Summary: Must be less than or equal to another field.
|
|
266
|
+
- Technique: Numeric comparison via `Number`.
|
|
267
|
+
- Use: `'lte:other'`
|
|
268
|
+
- Example: `['lte:other']`
|
|
269
|
+
|
|
270
|
+
### starts_with:a,b,c
|
|
271
|
+
- Summary: Must start with any of the given prefixes.
|
|
272
|
+
- Technique: Regex with alternation.
|
|
273
|
+
- Use: `'starts_with:foo,bar'`
|
|
274
|
+
- Example: `['starts_with:foo,bar']`
|
|
275
|
+
|
|
276
|
+
### ends_with:a,b,c
|
|
277
|
+
- Summary: Must end with any of the given suffixes.
|
|
278
|
+
- Technique: Regex with alternation.
|
|
279
|
+
- Use: `'ends_with:foo,bar'`
|
|
280
|
+
- Example: `['ends_with:foo,bar']`
|
|
281
|
+
|
|
282
|
+
### regex:pattern
|
|
283
|
+
- Summary: Must match a regex.
|
|
284
|
+
- Technique: `new RegExp(pattern)`.
|
|
285
|
+
- Use: `'regex:^\\d{4}$'`
|
|
286
|
+
- Example: `['regex:^\\d{4}$']`
|
|
287
|
+
|
|
288
|
+
### date_format:format
|
|
289
|
+
- Summary: Must match a date format.
|
|
290
|
+
- Technique: Format-to-regex (supports `Y m d H i s`).
|
|
291
|
+
- Use: `'date_format:Y-m-d'`
|
|
292
|
+
- Example: `['date_format:Y-m-d']`
|
|
293
|
+
|
|
294
|
+
### date
|
|
295
|
+
- Summary: Must be a valid `YYYY-MM-DD` date.
|
|
296
|
+
- Technique: Regex + `Date` ISO validation.
|
|
297
|
+
- Use: `'date'`
|
|
298
|
+
- Example: `['date']`
|
|
299
|
+
|
|
300
|
+
### before:date
|
|
301
|
+
- Summary: Must be before given date.
|
|
302
|
+
- Technique: `Date` comparison.
|
|
303
|
+
- Use: `'before:2024-01-01'`
|
|
304
|
+
- Example: `['before:2024-01-01']`
|
|
305
|
+
|
|
306
|
+
### after:date
|
|
307
|
+
- Summary: Must be after given date.
|
|
308
|
+
- Technique: `Date` comparison.
|
|
309
|
+
- Use: `'after:2024-01-01'`
|
|
310
|
+
- Example: `['after:2024-01-01']`
|
|
311
|
+
|
|
312
|
+
### before_or_equal:date
|
|
313
|
+
- Summary: Must be before or equal to given date.
|
|
314
|
+
- Technique: `Date` comparison.
|
|
315
|
+
- Use: `'before_or_equal:2024-01-01'`
|
|
316
|
+
- Example: `['before_or_equal:2024-01-01']`
|
|
317
|
+
|
|
318
|
+
### after_or_equal:date
|
|
319
|
+
- Summary: Must be after or equal to given date.
|
|
320
|
+
- Technique: `Date` comparison.
|
|
321
|
+
- Use: `'after_or_equal:2024-01-01'`
|
|
322
|
+
- Example: `['after_or_equal:2024-01-01']`
|
|
323
|
+
|
|
324
|
+
### array
|
|
325
|
+
- Summary: Must be an array.
|
|
326
|
+
- Technique: `Array.isArray`.
|
|
327
|
+
- Use: `'array'`
|
|
328
|
+
- Example: `['array']`
|
|
329
|
+
|
|
330
|
+
### distinct
|
|
331
|
+
- Summary: Array must not contain duplicates.
|
|
332
|
+
- Technique: `Set(JSON.stringify(item))` comparison.
|
|
333
|
+
- Use: `'distinct'`
|
|
334
|
+
- Example: `['distinct']`
|
|
335
|
+
|
|
336
|
+
### in_array:field
|
|
337
|
+
- Summary: Value must be in another field's array.
|
|
338
|
+
- Technique: `Array.includes` on referenced field.
|
|
339
|
+
- Use: `'in_array:allowed'`
|
|
340
|
+
- Example: `['in_array:allowed']`
|
|
341
|
+
|
|
342
|
+
### required_array_keys:key1,key2
|
|
343
|
+
- Summary: Object/array must contain required keys.
|
|
344
|
+
- Technique: `hasOwnProperty` per key.
|
|
345
|
+
- Use: `'required_array_keys:name,email'`
|
|
346
|
+
- Example: `['required_array_keys:name,email']`
|
|
347
|
+
|
|
348
|
+
### list
|
|
349
|
+
- Summary: Must be a list with sequential indices.
|
|
350
|
+
- Technique: Array and index presence check.
|
|
351
|
+
- Use: `'list'`
|
|
352
|
+
- Example: `['list']`
|
|
353
|
+
|
|
354
|
+
### url
|
|
355
|
+
- Summary: Must be a valid http/https URL.
|
|
356
|
+
- Technique: `new URL` + protocol check.
|
|
357
|
+
- Use: `'url'`
|
|
358
|
+
- Example: `['url']`
|
|
359
|
+
|
|
360
|
+
### ip
|
|
361
|
+
- Summary: Must be a valid IPv4 or IPv6 address.
|
|
362
|
+
- Technique: IPv4/IPv6 regex checks.
|
|
363
|
+
- Use: `'ip'`
|
|
364
|
+
- Example: `['ip']`
|
|
365
|
+
|
|
366
|
+
### hex_color
|
|
367
|
+
- Summary: Must be a valid hex color.
|
|
368
|
+
- Technique: Regex for 3/6 hex digits.
|
|
369
|
+
- Use: `'hex_color'`
|
|
370
|
+
- Example: `['hex_color']`
|
|
371
|
+
|
|
372
|
+
### lowercase
|
|
373
|
+
- Summary: Must be lowercase.
|
|
374
|
+
- Technique: `value === value.toLowerCase()`.
|
|
375
|
+
- Use: `'lowercase'`
|
|
376
|
+
- Example: `['lowercase']`
|
|
377
|
+
|
|
378
|
+
### uppercase
|
|
379
|
+
- Summary: Must be uppercase.
|
|
380
|
+
- Technique: `value === value.toUpperCase()`.
|
|
381
|
+
- Use: `'uppercase'`
|
|
382
|
+
- Example: `['uppercase']`
|
|
383
|
+
|
|
384
|
+
### uuid
|
|
385
|
+
- Summary: Must be a valid UUID (v1-8).
|
|
386
|
+
- Technique: Regex for UUID versions.
|
|
387
|
+
- Use: `'uuid'`
|
|
388
|
+
- Example: `['uuid']`
|
|
389
|
+
|
|
390
|
+
### ulid
|
|
391
|
+
- Summary: Must be a valid ULID.
|
|
392
|
+
- Technique: Crockford-base32 regex.
|
|
393
|
+
- Use: `'ulid'`
|
|
394
|
+
- Example: `['ulid']`
|
|
395
|
+
|
|
396
|
+
### timezone
|
|
397
|
+
- Summary: Must be a valid IANA timezone.
|
|
398
|
+
- Technique: `Intl.DateTimeFormat` with `timeZone`.
|
|
399
|
+
- Use: `'timezone'` or `'timezone:region'` (same behavior).
|
|
400
|
+
- Example: `['timezone']`
|
|
401
|
+
|
|
402
|
+
### in:a,b,c
|
|
403
|
+
- Summary: Value must be one of the listed values.
|
|
404
|
+
- Technique: String set membership.
|
|
405
|
+
- Use: `'in:small,medium,large'`
|
|
406
|
+
- Example: `['in:small,medium,large']`
|
|
407
|
+
|
|
408
|
+
### multiple_of:n
|
|
409
|
+
- Summary: Numeric value must be a multiple of n.
|
|
410
|
+
- Technique: `Number(value) % n === 0`.
|
|
411
|
+
- Use: `'multiple_of:5'`
|
|
412
|
+
- Example: `['multiple_of:5']`
|
|
413
|
+
|
|
414
|
+
### decimal:min[,max]
|
|
415
|
+
- Summary: Must be numeric with decimal places in range.
|
|
416
|
+
- Technique: Numeric regex + decimal count.
|
|
417
|
+
- Use: `'decimal:2'` or `'decimal:2,4'`
|
|
418
|
+
- Example: `['decimal:2,4']`
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# useForm
|
|
2
|
+
|
|
3
|
+
This helper is implemented in `src/runtime/form.ts` and exported as `useHttp`. The API below matches that implementation.
|
|
4
|
+
|
|
5
|
+
`useForm` (aka `useHttp`) builds a reactive form object with submit helpers that use Nuxt `$fetch`. It normalizes strings and nested values, supports `FormData` when files are present, and tracks `processing`, `errors`, and `response`.
|
|
6
|
+
|
|
7
|
+
## Signature
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const form = useHttp(initialData, formOptions)
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Options
|
|
14
|
+
|
|
15
|
+
- `alwaysFormData` (boolean): force `FormData` even when there are no files.
|
|
16
|
+
- `fetcher` (function): custom fetcher. Defaults to Nuxt `$fetch`.
|
|
17
|
+
- `fetchOptions` (object): shared options passed to `$fetch` (headers, onResponse, etc).
|
|
18
|
+
|
|
19
|
+
## Returns
|
|
20
|
+
|
|
21
|
+
The returned object includes your initial fields plus:
|
|
22
|
+
|
|
23
|
+
- `submit(method, url, options?)`
|
|
24
|
+
- `post(url, options?)`
|
|
25
|
+
- `patch(url, options?)`
|
|
26
|
+
- `put(url, options?)`
|
|
27
|
+
- `delete(url, options?)`
|
|
28
|
+
- `processing` (boolean)
|
|
29
|
+
- `errors` (any)
|
|
30
|
+
- `response` (object | null)
|
|
31
|
+
|
|
32
|
+
## Examples
|
|
33
|
+
|
|
34
|
+
Basic submit:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
const form = useHttp({ name: '', avatar: null })
|
|
38
|
+
|
|
39
|
+
await form.post('/api/users')
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Send multipart data and attach hooks:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const form = useHttp(
|
|
46
|
+
{ name: '', avatar: null, tags: [] },
|
|
47
|
+
{
|
|
48
|
+
alwaysFormData: true,
|
|
49
|
+
fetchOptions: {
|
|
50
|
+
onResponse({ response }) {
|
|
51
|
+
console.log('ok', response.ok)
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
await form.post('/api/users')
|
|
58
|
+
```
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Array Helpers
|
|
3
|
+
outline: [2, 3]
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Array Helpers
|
|
7
|
+
|
|
8
|
+
Array helpers focus on collection utilities like filtering, sorting, grouping, and selection.
|
|
9
|
+
|
|
10
|
+
Factory style:
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
const first = array().first(['a', 'b'])
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Prefix style:
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
const first = First(['a', 'b'])
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Methods
|
|
23
|
+
|
|
24
|
+
### collapse
|
|
25
|
+
Summary: Flattens an array of arrays by one level using iteration and spreading.
|
|
26
|
+
Parameters: `arrays` (Array<T[]>).
|
|
27
|
+
- Returns: `T[]`.
|
|
28
|
+
- Simple usage: `array().collapse([[1, 2], [3]])` = `[1, 2, 3]`
|
|
29
|
+
- Advanced usage: `array().collapse([[1], null as any, [2, 3]])` = `[1, 2, 3]`
|
|
30
|
+
|
|
31
|
+
### crossJoin
|
|
32
|
+
Summary: Builds a cartesian product using reduce and nested loops.
|
|
33
|
+
Parameters: `arrays` (T[][]) via rest arguments.
|
|
34
|
+
- Returns: `T[][]`.
|
|
35
|
+
- Simple usage: `array().crossJoin([1, 2], ['a', 'b'])` = `[[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]`
|
|
36
|
+
- Advanced usage: `array().crossJoin([1, 2], ['a'], ['x', 'y'])` = `[[1, 'a', 'x'], [1, 'a', 'y'], [2, 'a', 'x'], [2, 'a', 'y']]`
|
|
37
|
+
|
|
38
|
+
### every
|
|
39
|
+
Summary: Tests whether all items satisfy a predicate via native `every`.
|
|
40
|
+
Parameters: `array` (T[]), `predicate` ((item, index) => boolean).
|
|
41
|
+
- Returns: `boolean`.
|
|
42
|
+
- Simple usage: `array().every([1, 2], n => n > 0)` = `true`
|
|
43
|
+
- Advanced usage: `array().every([{ role: 'admin', active: true }], u => u.active && u.role === 'admin')` = `true`
|
|
44
|
+
|
|
45
|
+
### first
|
|
46
|
+
Summary: Returns the first item or first matching item using a forward scan.
|
|
47
|
+
Parameters: `array` (T[]), `callback` ((item, index) => boolean, optional), `defaultValue` (T | null, optional).
|
|
48
|
+
- Returns: `T | null`.
|
|
49
|
+
- Simple usage: `array().first([1, 2, 3])` = `1`
|
|
50
|
+
- Advanced usage: `array().first([{ id: 1 }, { id: 2 }], u => u.id === 1, null)` = `{ id: 1 }`
|
|
51
|
+
|
|
52
|
+
### flatten
|
|
53
|
+
Summary: Flattens nested arrays to a given depth using recursion.
|
|
54
|
+
Parameters: `input` (T[]), `depth` (number, optional; default: Infinity).
|
|
55
|
+
- Returns: `any[]`.
|
|
56
|
+
- Simple usage: `array().flatten([1, [2, [3]]], 1)` = `[1, 2, [3]]`
|
|
57
|
+
- Advanced usage: `array().flatten([1, [2, [3]]])` = `[1, 2, 3]`
|
|
58
|
+
|
|
59
|
+
### join
|
|
60
|
+
Summary: Joins array items with optional final glue by splitting head/tail.
|
|
61
|
+
Parameters: `array` (any[]), `glue` (string), `finalGlue` (string, optional).
|
|
62
|
+
- Returns: `string`.
|
|
63
|
+
- Simple usage: `array().join(['a', 'b', 'c'], ', ')` = `'a, b, c'`
|
|
64
|
+
- Advanced usage: `array().join(['a', 'b', 'c'], ', ', ' and ')` = `'a, b and c'`
|
|
65
|
+
|
|
66
|
+
### last
|
|
67
|
+
Summary: Returns the last item or last matching item using a reverse scan.
|
|
68
|
+
Parameters: `array` (T[]), `callback` ((item, index) => boolean, optional), `defaultValue` (T | null, optional).
|
|
69
|
+
- Returns: `T | null`.
|
|
70
|
+
- Simple usage: `array().last([1, 2, 3])` = `3`
|
|
71
|
+
- Advanced usage: `array().last([{ id: 1 }, { id: 2 }], u => u.id === 2, null)` = `{ id: 2 }`
|
|
72
|
+
|
|
73
|
+
### map
|
|
74
|
+
Summary: Transforms items with native `map`.
|
|
75
|
+
Parameters: `array` (T[]), `callback` ((item, index) => U).
|
|
76
|
+
- Returns: `U[]`.
|
|
77
|
+
- Simple usage: `array().map([1, 2], n => n * 2)` = `[2, 4]`
|
|
78
|
+
- Advanced usage: `array().map([{ name: 'A' }, { name: 'B' }], (u, i) => ({ ...u, index: i }))` = `[{ name: 'A', index: 0 }, { name: 'B', index: 1 }]`
|
|
79
|
+
|
|
80
|
+
### mapSpread
|
|
81
|
+
Summary: Spreads tuple items into the callback during mapping.
|
|
82
|
+
Parameters: `array` (T[] of tuples), `callback` ((...args) => U).
|
|
83
|
+
- Returns: `U[]`.
|
|
84
|
+
- Simple usage: `array().mapSpread([[1, 2], [3, 4]], (a, b) => a + b)` = `[3, 7]`
|
|
85
|
+
- Advanced usage: `array().mapSpread([[1, 2], [3, 4]], (min, max) => ({ min, max }))` = `[{ min: 1, max: 2 }, { min: 3, max: 4 }]`
|
|
86
|
+
|
|
87
|
+
### partition
|
|
88
|
+
Summary: Splits into `[pass, fail]` with a single pass.
|
|
89
|
+
Parameters: `array` (T[]), `predicate` ((item, index) => boolean).
|
|
90
|
+
- Returns: `[T[], T[]]`.
|
|
91
|
+
- Simple usage: `array().partition([1, 2, 3], n => n % 2 === 0)` = `[[2], [1, 3]]`
|
|
92
|
+
- Advanced usage: `array().partition([{ id: 1 }, { id: 2 }], u => u.id === 1)` = `[[{ id: 1 }], [{ id: 2 }]]`
|
|
93
|
+
|
|
94
|
+
### random
|
|
95
|
+
Summary: Picks one or multiple random items using random indices and splicing.
|
|
96
|
+
Parameters: `array` (T[]), `count` (number, optional).
|
|
97
|
+
- Returns: `T | T[] | undefined`.
|
|
98
|
+
- Simple usage: `array().random([1, 2, 3])` = `2 (random)`
|
|
99
|
+
- Advanced usage: `array().random([1, 2, 3, 4], 2)` = `[3, 1] (random order)`
|
|
100
|
+
|
|
101
|
+
### reject
|
|
102
|
+
Summary: Filters out items matching a predicate by negating `filter`.
|
|
103
|
+
Parameters: `array` (T[]), `predicate` ((item, index) => boolean).
|
|
104
|
+
- Returns: `T[]`.
|
|
105
|
+
- Simple usage: `array().reject([1, 2, 3], n => n > 2)` = `[1, 2]`
|
|
106
|
+
- Advanced usage: `array().reject([{ id: 1, disabled: false }, { id: 2, disabled: true }], u => u.disabled)` = `[{ id: 1, disabled: false }]`
|
|
107
|
+
|
|
108
|
+
### shuffle
|
|
109
|
+
Summary: Randomizes order using Fisher-Yates shuffle.
|
|
110
|
+
Parameters: `array` (T[]).
|
|
111
|
+
- Returns: `T[]`.
|
|
112
|
+
- Simple usage: `array().shuffle([1, 2, 3])` = `[2, 1, 3] (random order)`
|
|
113
|
+
- Advanced usage: `array().shuffle([{ id: 1 }, { id: 2 }])` = `[{ id: 2 }, { id: 1 }] (random order)`
|
|
114
|
+
|
|
115
|
+
### sole
|
|
116
|
+
Summary: Returns the only match or throws if zero/multiple matches.
|
|
117
|
+
Parameters: `array` (T[]), `predicate` ((item, index) => boolean, optional).
|
|
118
|
+
- Returns: `T`.
|
|
119
|
+
- Simple usage: `array().sole([1])` = `1`
|
|
120
|
+
- Advanced usage: `array().sole([{ id: 42 }], u => u.id === 42)` = `{ id: 42 }`
|
|
121
|
+
|
|
122
|
+
### some
|
|
123
|
+
Summary: Tests whether any item matches a predicate using native `some`.
|
|
124
|
+
Parameters: `array` (T[]), `predicate` ((item, index) => boolean).
|
|
125
|
+
- Returns: `boolean`.
|
|
126
|
+
- Simple usage: `array().some([1, 2], n => n > 1)` = `true`
|
|
127
|
+
- Advanced usage: `array().some([{ role: 'admin' }], u => u.role === 'admin')` = `true`
|
|
128
|
+
|
|
129
|
+
### sort
|
|
130
|
+
Summary: Sorts ascending by copying and using native `sort`.
|
|
131
|
+
Parameters: `array` (T[]), `comparator` ((a, b) => number, optional).
|
|
132
|
+
- Returns: `T[]`.
|
|
133
|
+
- Simple usage: `array().sort([3, 1, 2])` = `[1, 2, 3]`
|
|
134
|
+
- Advanced usage: `array().sort([{ age: 30 }, { age: 18 }], (a, b) => a.age - b.age)` = `[{ age: 18 }, { age: 30 }]`
|
|
135
|
+
|
|
136
|
+
### sortDesc
|
|
137
|
+
Summary: Sorts descending by reversing the comparator logic.
|
|
138
|
+
Parameters: `array` (T[]), `comparator` ((a, b) => number, optional).
|
|
139
|
+
- Returns: `T[]`.
|
|
140
|
+
- Simple usage: `array().sortDesc([3, 1, 2])` = `[3, 2, 1]`
|
|
141
|
+
- Advanced usage: `array().sortDesc([{ age: 30 }, { age: 18 }], (a, b) => a.age - b.age)` = `[{ age: 30 }, { age: 18 }]`
|
|
142
|
+
|
|
143
|
+
### take
|
|
144
|
+
Summary: Takes N items from start or end via `slice`.
|
|
145
|
+
Parameters: `array` (T[]), `limit` (number).
|
|
146
|
+
- Returns: `T[]`.
|
|
147
|
+
- Simple usage: `array().take([1, 2, 3], 2)` = `[1, 2]`
|
|
148
|
+
- Advanced usage: `array().take([1, 2, 3], -1)` = `[3]`
|
|
149
|
+
|
|
150
|
+
### where
|
|
151
|
+
Summary: Filters items matching a predicate using native `filter`.
|
|
152
|
+
Parameters: `array` (T[]), `predicate` ((item, index) => boolean).
|
|
153
|
+
- Returns: `T[]`.
|
|
154
|
+
- Simple usage: `array().where([1, 2, 3], n => n > 1)` = `[2, 3]`
|
|
155
|
+
- Advanced usage: `array().where([{ id: 1, active: true }, { id: 4, active: true }], u => u.active)` = `[{ id: 1, active: true }, { id: 4, active: true }]`
|
|
156
|
+
|
|
157
|
+
### whereNotNull
|
|
158
|
+
Summary: Removes `null` and `undefined` values.
|
|
159
|
+
Parameters: `array` (T[]).
|
|
160
|
+
- Returns: `T[]`.
|
|
161
|
+
- Simple usage: `array().whereNotNull([1, null, 2])` = `[1, 2]`
|
|
162
|
+
- Advanced usage: `array().whereNotNull(['a', undefined, 'b'])` = `['a', 'b']`
|
|
163
|
+
|
|
164
|
+
### wrap
|
|
165
|
+
Summary: Normalizes a value into an array.
|
|
166
|
+
Parameters: `value` (T | T[] | null | undefined).
|
|
167
|
+
- Returns: `T[]`.
|
|
168
|
+
- Simple usage: `array().wrap(1)` = `[1]`
|
|
169
|
+
- Advanced usage: `array().wrap([1, 2])` = `[1, 2]`
|