sanity-advanced-validators 0.2.1 → 0.3.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/README.md +106 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
*🚓 Never trust a user! 👮*
|
|
2
|
+
|
|
1
3
|
# Sanity Advanced Validators
|
|
2
4
|
|
|
3
|
-
This package includes a set of Sanity validators for aggressive and weird edge cases.
|
|
5
|
+
This package includes a set of Sanity validators for aggressive and weird edge cases. *Maintain sanity with micro-managed validation.*
|
|
4
6
|
|
|
5
|
-
Note
|
|
7
|
+
Note: Every validator can accept an optional custom error message as its last parameter. See [minDimensions](#minDimensions) for an example.
|
|
6
8
|
|
|
7
9
|
## Tools
|
|
8
10
|
|
|
@@ -18,7 +20,10 @@ Note that every validator can accept an optional custom error message as its las
|
|
|
18
20
|
|
|
19
21
|
## Mega-example
|
|
20
22
|
|
|
21
|
-
Imagine that you’ve got a document that has an optional video file
|
|
23
|
+
Imagine that you’ve got a document that has an optional video file, but…
|
|
24
|
+
- it’s required on the `/about` page
|
|
25
|
+
- if the video exists, it must be either **MP4** or **MOV**
|
|
26
|
+
- and there must be a poster image that's between **1250x800** and **2500x1600** pixels in size
|
|
22
27
|
|
|
23
28
|
```typescript
|
|
24
29
|
const Page = defineType({
|
|
@@ -51,7 +56,7 @@ const Page = defineType({
|
|
|
51
56
|
|
|
52
57
|
## Examples
|
|
53
58
|
|
|
54
|
-
###
|
|
59
|
+
### fileExtension
|
|
55
60
|
|
|
56
61
|
Enforces that an uploaded file asset is of a certain format.
|
|
57
62
|
|
|
@@ -65,7 +70,7 @@ const Page = defineType({
|
|
|
65
70
|
defineField({
|
|
66
71
|
name: "catalog",
|
|
67
72
|
type: "file",
|
|
68
|
-
validation: (rule) => rule.custom(
|
|
73
|
+
validation: (rule) => rule.custom(fileExtension("pdf")),
|
|
69
74
|
}),
|
|
70
75
|
defineField({
|
|
71
76
|
name: "video",
|
|
@@ -76,6 +81,14 @@ const Page = defineType({
|
|
|
76
81
|
})
|
|
77
82
|
```
|
|
78
83
|
|
|
84
|
+
#### parameters
|
|
85
|
+
```typescript
|
|
86
|
+
fileType: string | Array<string>,
|
|
87
|
+
message?: string // optional custom error message; replaces {validFileExtension} with fileType (flattened)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
79
92
|
### minDimensions
|
|
80
93
|
|
|
81
94
|
Enforces that an uploaded image asset is at minimum certain dimensions.
|
|
@@ -113,6 +126,15 @@ defineField({
|
|
|
113
126
|
})
|
|
114
127
|
```
|
|
115
128
|
|
|
129
|
+
#### parameters
|
|
130
|
+
```typescript
|
|
131
|
+
dimensions: {x?: number, y?: number},
|
|
132
|
+
message?: string // optional custom error message; replaces {x} and {y} with your dimension requirements, and {width} and {height} with submitted image dimensions
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
|
|
116
138
|
### maxDimensions
|
|
117
139
|
|
|
118
140
|
Enforces that an uploaded image asset is at most certain dimensions.
|
|
@@ -140,11 +162,20 @@ defineField({
|
|
|
140
162
|
})
|
|
141
163
|
```
|
|
142
164
|
|
|
165
|
+
#### parameters
|
|
166
|
+
```typescript
|
|
167
|
+
dimensions: {x?: number, y?: number},
|
|
168
|
+
message?: string // optional custom error message; replaces {x} and {y} with your dimension requirements, and {width} and {height} with submitted image dimensions
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
|
|
143
174
|
### requiredIfSiblingEq
|
|
144
175
|
|
|
145
|
-
|
|
176
|
+
Mark a field as `required` if a sibling field has a particular value. This is the validator we use most. *It’s super effective!*
|
|
146
177
|
|
|
147
|
-
This is
|
|
178
|
+
This is handy if you have a field that is hidden under some circumstances, but is `required()` when it’s visible.
|
|
148
179
|
|
|
149
180
|
_note:_ This does not work for slugs, because they have to match a nested `.current` value. Use the [requiredIfSlugEq validator](#requiredIfSlugEq) instead.
|
|
150
181
|
|
|
@@ -181,7 +212,7 @@ defineType({
|
|
|
181
212
|
})
|
|
182
213
|
```
|
|
183
214
|
|
|
184
|
-
This also works for null.
|
|
215
|
+
“If not that, then this.” This also works for null.
|
|
185
216
|
|
|
186
217
|
```typescript
|
|
187
218
|
defineType({
|
|
@@ -242,6 +273,16 @@ defineType({
|
|
|
242
273
|
})
|
|
243
274
|
```
|
|
244
275
|
|
|
276
|
+
#### parameters
|
|
277
|
+
```typescript
|
|
278
|
+
key: string, // name of sibling
|
|
279
|
+
operand: string | number | null | Array<string, number, null> // value that you’re testing for (i.e. if 'name' === operand)
|
|
280
|
+
message?: string // optional custom error message; replaces {key} and {operand} with your input, and {siblingValue} with the value of the sibling you’re testing against.
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
---
|
|
284
|
+
|
|
285
|
+
|
|
245
286
|
### requiredIfSiblingNeq
|
|
246
287
|
|
|
247
288
|
For a given object that has multiple fields, mark a field as `required` if a sibling does _not_ have a particular value.
|
|
@@ -276,6 +317,16 @@ defineType({
|
|
|
276
317
|
})
|
|
277
318
|
```
|
|
278
319
|
|
|
320
|
+
#### parameters
|
|
321
|
+
```typescript
|
|
322
|
+
key: string, // name of sibling
|
|
323
|
+
operand: string | number | null | Array<string, number, null> // value that you’re testing for (i.e. if 'name' === operand)
|
|
324
|
+
message?: string // optional custom error message; replaces {key} and {operand} with your input, and {siblingValue} with the value of the sibling you’re testing against.
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
|
|
279
330
|
### requiredIfSlugEq
|
|
280
331
|
|
|
281
332
|
Require for matching slugs.
|
|
@@ -313,6 +364,16 @@ defineField({
|
|
|
313
364
|
}),
|
|
314
365
|
```
|
|
315
366
|
|
|
367
|
+
#### parameters
|
|
368
|
+
```typescript
|
|
369
|
+
operand: string | number | null | Array<string, number, null> // possible slug or slugs you’re testing
|
|
370
|
+
key?: string, // name of sibling if not "slug"
|
|
371
|
+
message?: string // optional custom error message; replaces {slugKey} and {operand} with your input, and {siblingSlugValue} with the value of the sibling you’re testing against.
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
|
|
316
377
|
### requiredIfSlugNeq
|
|
317
378
|
|
|
318
379
|
Require fields on pages that don't match one or more slugs.
|
|
@@ -342,13 +403,23 @@ defineType({
|
|
|
342
403
|
})
|
|
343
404
|
```
|
|
344
405
|
|
|
406
|
+
#### parameters
|
|
407
|
+
```typescript
|
|
408
|
+
operand: string | number | null | Array<string, number, null> // possible slug or slugs you’re testing
|
|
409
|
+
key?: string, // name of sibling if not "slug"
|
|
410
|
+
message?: string // optional custom error message; replaces {slugKey} and {operand} with your input, and {siblingSlugValue} with the value of the sibling you’re testing against.
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
---
|
|
414
|
+
|
|
415
|
+
|
|
345
416
|
### referencedDocumentRequires
|
|
346
417
|
|
|
347
418
|
You might want to enforce some validation on a referenced document. This validator enforces that a given value is not null in the referenced document.
|
|
348
419
|
|
|
349
420
|
```typescript
|
|
350
421
|
defineField({
|
|
351
|
-
name: '
|
|
422
|
+
name: 'referredArticle',
|
|
352
423
|
description: 'An article (must include a valid poster image)',
|
|
353
424
|
type: 'reference',
|
|
354
425
|
to: [{type: 'article'}],
|
|
@@ -356,6 +427,16 @@ defineField({
|
|
|
356
427
|
}),
|
|
357
428
|
```
|
|
358
429
|
|
|
430
|
+
#### parameters
|
|
431
|
+
```typescript
|
|
432
|
+
documentType: string // type of document you’re referring to
|
|
433
|
+
field: string, // name of document field that is required
|
|
434
|
+
message?: string // optional custom error message; replaces {documentType} and {field} with your input
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
---
|
|
438
|
+
|
|
439
|
+
|
|
359
440
|
### maxDepth
|
|
360
441
|
|
|
361
442
|
It can be useful to have a nested type. This often comes up when making some kind of navigation tree, like…
|
|
@@ -428,7 +509,20 @@ const navLink = defineType({
|
|
|
428
509
|
|
|
429
510
|
This will enforce that a subnav list can embed in a subnav, which can also be embedded in a subnav — but no further.
|
|
430
511
|
|
|
431
|
-
|
|
512
|
+
|
|
513
|
+
#### parameters
|
|
514
|
+
```typescript
|
|
515
|
+
maxDepth: number // maximum "depth" of embedding (including parent)
|
|
516
|
+
key: string, // name of the field that includes the cursive value (i.e. the field’s own name)
|
|
517
|
+
message?: string // optional custom error message; replaces {maxDepth} and {key} with your input
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
---
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
#### Note to any Sanity dev who looks at this
|
|
524
|
+
|
|
525
|
+
I’d love to include similar logic on my `hidden:` attribute, but I don’t think that’t possible without a `path` array in `hidden`’s `ConditionalPropertyCallbackContext` that’s similar to the one fed to the `ValidationContext` (todo: type this correctly). Wouldn’t this be cool?
|
|
432
526
|
|
|
433
527
|
```typescript
|
|
434
528
|
defineField({
|
|
@@ -443,6 +537,8 @@ defineField({
|
|
|
443
537
|
})
|
|
444
538
|
```
|
|
445
539
|
|
|
540
|
+
---
|
|
541
|
+
|
|
446
542
|
## Extending these and writing your own
|
|
447
543
|
|
|
448
544
|
Most of these validators rely on a function called `getSibling()`. If you’re thinking about picking this apart and writing your own custom validator, take a close look at how these validators use it.
|