sanity-advanced-validators 0.5.3 → 0.6.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 +55 -28
- package/package.json +1 -3
package/README.md
CHANGED
|
@@ -9,10 +9,10 @@ This package includes a set of Sanity validators for aggressive and weird edge c
|
|
|
9
9
|
- [fileExtension](#fileExtension)
|
|
10
10
|
- [minDimensions](#minDimensions)
|
|
11
11
|
- [maxDimensions](#maxDimensions)
|
|
12
|
-
- [minCount](#minCount)
|
|
13
|
-
- [maxCount](#maxCount)
|
|
14
|
-
- [requiredIfSiblingEq](#requiredIfSiblingEq)
|
|
15
|
-
- [requiredIfSiblingNeq](#requiredIfSiblingNeq)
|
|
12
|
+
- [minCount](#minCount)
|
|
13
|
+
- [maxCount](#maxCount)
|
|
14
|
+
- [requiredIfSiblingEq](#requiredIfSiblingEq) 🆕
|
|
15
|
+
- [requiredIfSiblingNeq](#requiredIfSiblingNeq) 🆕
|
|
16
16
|
- [requiredIfSlugEq](#requiredIfSlugEq)
|
|
17
17
|
- [requiredIfSlugNeq](#requiredIfSlugNeq)
|
|
18
18
|
- [regex](#regex)
|
|
@@ -23,11 +23,19 @@ This package includes a set of Sanity validators for aggressive and weird edge c
|
|
|
23
23
|
|
|
24
24
|
Imagine that you’ve got a document that has an optional video file, but…
|
|
25
25
|
|
|
26
|
-
- it’s required on the `/about` page
|
|
27
26
|
- if the video exists, it must be either **MP4** or **MOV**
|
|
28
27
|
- and there must be a poster image that's between **1250x800** and **2500x1600** pixels in size
|
|
28
|
+
- and it’s _always_ required on the `/home` page
|
|
29
29
|
|
|
30
30
|
```typescript
|
|
31
|
+
import { defineType, defineField } from 'sanity'
|
|
32
|
+
import {
|
|
33
|
+
requiredIfSlugEq,
|
|
34
|
+
requiredIfSiblingNeq,
|
|
35
|
+
minDimensions,
|
|
36
|
+
maxDimensions
|
|
37
|
+
} from 'sanity-advanced-validators'
|
|
38
|
+
|
|
31
39
|
const Page = defineType({
|
|
32
40
|
name: "page",
|
|
33
41
|
type: "document",
|
|
@@ -41,14 +49,11 @@ const Page = defineType({
|
|
|
41
49
|
type: "file",
|
|
42
50
|
validation: (rule) =>
|
|
43
51
|
rule.custom(
|
|
44
|
-
requiredIfSlugEq(
|
|
45
|
-
'about',
|
|
46
|
-
'A video is required if {slugKey} is {operand}.'
|
|
47
|
-
)
|
|
52
|
+
requiredIfSlugEq('home', 'A video is required on the home page.')
|
|
48
53
|
).custom(
|
|
49
54
|
fileExtension(['mp4', 'mov'])
|
|
50
55
|
)
|
|
51
|
-
})
|
|
56
|
+
}),
|
|
52
57
|
defineField({
|
|
53
58
|
name: "posterImage",
|
|
54
59
|
type: "image",
|
|
@@ -78,7 +83,7 @@ message?: string // optional custom error message; replaces {validFileExtension}
|
|
|
78
83
|
```
|
|
79
84
|
|
|
80
85
|
```typescript
|
|
81
|
-
import { fileExtension } from "sanity-advanced-
|
|
86
|
+
import { fileExtension } from "sanity-advanced-validators"
|
|
82
87
|
|
|
83
88
|
const Page = defineType({
|
|
84
89
|
name: "page",
|
|
@@ -116,7 +121,7 @@ message?: string // optional custom error message; replaces {x} and {y} with you
|
|
|
116
121
|
```
|
|
117
122
|
|
|
118
123
|
```typescript
|
|
119
|
-
import { minDimensions } from "sanity-advanced-
|
|
124
|
+
import { minDimensions } from "sanity-advanced-validators"
|
|
120
125
|
|
|
121
126
|
const ImageWithCaption = defineType({
|
|
122
127
|
name: "article",
|
|
@@ -223,6 +228,24 @@ defineField({
|
|
|
223
228
|
}),
|
|
224
229
|
```
|
|
225
230
|
|
|
231
|
+
And of course it can be chained.
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
defineField({
|
|
235
|
+
name: "thumbnails",
|
|
236
|
+
type: "array",
|
|
237
|
+
of: [ {type: 'image'} ],
|
|
238
|
+
validation: (rule) =>
|
|
239
|
+
rule.required()
|
|
240
|
+
.custom(
|
|
241
|
+
minCount(1, "At least one thumbnail is required.")
|
|
242
|
+
),
|
|
243
|
+
.custom(
|
|
244
|
+
maxCount(3, "1-3 thumbnails are required.")
|
|
245
|
+
),
|
|
246
|
+
}),
|
|
247
|
+
```
|
|
248
|
+
|
|
226
249
|
---
|
|
227
250
|
|
|
228
251
|
### requiredIfSiblingEq
|
|
@@ -231,6 +254,8 @@ Mark a field as `required` if a sibling field has a particular value. This is th
|
|
|
231
254
|
|
|
232
255
|
This is handy if you have a field that is hidden under some circumstances, but is `required()` when it’s visible.
|
|
233
256
|
|
|
257
|
+
🆕 Previously, this would not work for objects that were members of an array. That is fixed now.
|
|
258
|
+
|
|
234
259
|
_note:_ This does not work for slugs, because they have to match a nested `.current` value. Use the [requiredIfSlugEq validator](#requiredIfSlugEq) instead.
|
|
235
260
|
|
|
236
261
|
```typescript
|
|
@@ -240,7 +265,7 @@ message?: string // optional custom error message; replaces {key} and {operand}
|
|
|
240
265
|
```
|
|
241
266
|
|
|
242
267
|
```typescript
|
|
243
|
-
import {requiredIfSiblingEq} from 'sanity-advanced-
|
|
268
|
+
import {requiredIfSiblingEq} from 'sanity-advanced-validators'
|
|
244
269
|
|
|
245
270
|
defineType({
|
|
246
271
|
name: 'person',
|
|
@@ -343,6 +368,8 @@ defineType({
|
|
|
343
368
|
|
|
344
369
|
For a given object that has multiple fields, mark a field as `required` if a sibling does _not_ have a particular value (or member of an array of values).
|
|
345
370
|
|
|
371
|
+
🆕 Previously, this would not work for objects that were members of an array. That is fixed now.
|
|
372
|
+
|
|
346
373
|
_note:_ This does not work for slugs, because they have to match a nested `.current` value. Use the [requiredIfSlugNeq validator](#requiredIfSlugNeq) instead.
|
|
347
374
|
|
|
348
375
|
```typescript
|
|
@@ -352,7 +379,7 @@ message?: string // optional custom error message; replaces {key} and {operand}
|
|
|
352
379
|
```
|
|
353
380
|
|
|
354
381
|
```typescript
|
|
355
|
-
import {requiredIfSiblingNeq} from 'sanity-advanced-
|
|
382
|
+
import {requiredIfSiblingNeq} from 'sanity-advanced-validators'
|
|
356
383
|
|
|
357
384
|
defineType({
|
|
358
385
|
name: 'person',
|
|
@@ -389,13 +416,13 @@ defineType({
|
|
|
389
416
|
Mark a field as `required` for documents with matching slugs.
|
|
390
417
|
|
|
391
418
|
```typescript
|
|
392
|
-
operand: string | number | null | Array<string, number> //
|
|
393
|
-
key?: string, // name of
|
|
419
|
+
operand: string | number | null | Array<string, number> // value or values that you’re testing for
|
|
420
|
+
key?: string, // name of slug field if not "slug"
|
|
394
421
|
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.
|
|
395
422
|
```
|
|
396
423
|
|
|
397
424
|
```typescript
|
|
398
|
-
import { requiredIfSlugEq } from "sanity-advanced-
|
|
425
|
+
import { requiredIfSlugEq } from "sanity-advanced-validators"
|
|
399
426
|
|
|
400
427
|
defineType({
|
|
401
428
|
name: "page",
|
|
@@ -438,13 +465,13 @@ defineField({
|
|
|
438
465
|
Require fields on pages that don't match one or more slugs.
|
|
439
466
|
|
|
440
467
|
```typescript
|
|
441
|
-
operand: string | number | null | Array<string, number> //
|
|
442
|
-
key?: string, // name of
|
|
468
|
+
operand: string | number | null | Array<string, number> // value or values that you’re testing
|
|
469
|
+
key?: string, // name of slug field if not "slug"
|
|
443
470
|
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.
|
|
444
471
|
```
|
|
445
472
|
|
|
446
473
|
```typescript
|
|
447
|
-
import { requiredIfSlugNeq } from "sanity-advanced-
|
|
474
|
+
import { requiredIfSlugNeq } from "sanity-advanced-validators"
|
|
448
475
|
|
|
449
476
|
defineType({
|
|
450
477
|
name: "page",
|
|
@@ -496,7 +523,7 @@ defineField({
|
|
|
496
523
|
}),
|
|
497
524
|
```
|
|
498
525
|
|
|
499
|
-
|
|
526
|
+
_Custom error messages are highly recommended here._ Without a custom message above, the default error message would be:
|
|
500
527
|
|
|
501
528
|
```
|
|
502
529
|
“me@googlecom” does not match the pattern /^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$/.
|
|
@@ -584,12 +611,12 @@ const navLink = defineType({
|
|
|
584
611
|
|
|
585
612
|
```typescript
|
|
586
613
|
maxDepth: number // maximum "depth" of embedding (including parent)
|
|
587
|
-
key: string, // name of the field that includes the
|
|
614
|
+
key: string, // name of the field that includes the recursive value (i.e. the field’s own name)
|
|
588
615
|
message?: string // optional custom error message; replaces {maxDepth} and {key} with your input
|
|
589
616
|
```
|
|
590
617
|
|
|
591
618
|
```typescript
|
|
592
|
-
import { maxDepth } from "sanity-advanced-
|
|
619
|
+
import { maxDepth } from "sanity-advanced-validators"
|
|
593
620
|
|
|
594
621
|
const navLink = defineType({
|
|
595
622
|
// …
|
|
@@ -631,26 +658,26 @@ defineField({
|
|
|
631
658
|
|
|
632
659
|
---
|
|
633
660
|
|
|
634
|
-
## Extending these and writing your own
|
|
635
|
-
|
|
636
|
-
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.
|
|
637
|
-
|
|
638
661
|
## Roadmap
|
|
639
662
|
|
|
640
663
|
### Nested pathfinders
|
|
641
664
|
|
|
642
665
|
Since building these validator, I took to putting my slugs in a metadata object. I need to update `requiredIfSlugEq` to accept a path, like `requiredIfSlugEq('metadata.slug', 'some-values')`.
|
|
643
666
|
|
|
644
|
-
This pathfinding should be added to any validator that takes a sibling, like `requiredIfSiblingEq`. It can probably be snapped into `getSibling`.
|
|
667
|
+
This pathfinding should be added to any validator that takes a sibling, like `requiredIfSiblingEq('metadata.slug.current', 'home')`. It can probably be snapped into `getSibling`.
|
|
645
668
|
|
|
646
669
|
While I’m at it, there’s a possibility that `getSibling` could detect the target type. If that type is `slug`, then it could add `current` to the path, and then I can deprecate `requiredIfSlugEq` altogether.
|
|
647
670
|
|
|
671
|
+
On a related note, `requiredIfSlugEq` does not work in an object nested inside an array. If we can deprecate `requiredIfSlugEq`, then this would be automatically resolved.
|
|
672
|
+
|
|
648
673
|
### Image and File checks
|
|
649
674
|
|
|
650
675
|
`minDimensions`, `maxDimensions`, and `fileExtension` should check to see if the field is of type `image` or `file`.
|
|
651
676
|
|
|
652
677
|
Some of the other checks should probably make sure the field is _not_ `image` or `file`.
|
|
653
678
|
|
|
679
|
+
An `aspectRatio(n)` might be nice (ex. `aspectRatio(1.6)` or `aspectRatio(1.6/1)`).
|
|
680
|
+
|
|
654
681
|
### new referencedDocumentFieldEq validator
|
|
655
682
|
|
|
656
683
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"package-name": "sanity-advanced-validators",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Advanced input validation tools for Sanity CMS.",
|
|
5
5
|
"author": "Eric_WVGG",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,11 +22,9 @@
|
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@sanity/asset-utils": "^2.2.1",
|
|
25
|
-
"lodash-es": "^4.17.0",
|
|
26
25
|
"sanity": "^4.9.0"
|
|
27
26
|
},
|
|
28
27
|
"devDependencies": {
|
|
29
|
-
"@types/lodash-es": "^4.17.0",
|
|
30
28
|
"tsup": "^8.5.0",
|
|
31
29
|
"typescript": "^5.8.0",
|
|
32
30
|
"vitest": "^3.2.4"
|