sanity-advanced-validators 0.6.0 → 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 +20 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,12 +23,18 @@ 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 {
|
|
31
|
+
import { defineType, defineField } from 'sanity'
|
|
32
|
+
import {
|
|
33
|
+
requiredIfSlugEq,
|
|
34
|
+
requiredIfSiblingNeq,
|
|
35
|
+
minDimensions,
|
|
36
|
+
maxDimensions
|
|
37
|
+
} from 'sanity-advanced-validators'
|
|
32
38
|
|
|
33
39
|
const Page = defineType({
|
|
34
40
|
name: "page",
|
|
@@ -43,14 +49,11 @@ const Page = defineType({
|
|
|
43
49
|
type: "file",
|
|
44
50
|
validation: (rule) =>
|
|
45
51
|
rule.custom(
|
|
46
|
-
requiredIfSlugEq(
|
|
47
|
-
'about',
|
|
48
|
-
'A video is required if {slugKey} is {operand}.'
|
|
49
|
-
)
|
|
52
|
+
requiredIfSlugEq('home', 'A video is required on the home page.')
|
|
50
53
|
).custom(
|
|
51
54
|
fileExtension(['mp4', 'mov'])
|
|
52
55
|
)
|
|
53
|
-
})
|
|
56
|
+
}),
|
|
54
57
|
defineField({
|
|
55
58
|
name: "posterImage",
|
|
56
59
|
type: "image",
|
|
@@ -413,8 +416,8 @@ defineType({
|
|
|
413
416
|
Mark a field as `required` for documents with matching slugs.
|
|
414
417
|
|
|
415
418
|
```typescript
|
|
416
|
-
operand: string | number | null | Array<string, number> //
|
|
417
|
-
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"
|
|
418
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.
|
|
419
422
|
```
|
|
420
423
|
|
|
@@ -462,8 +465,8 @@ defineField({
|
|
|
462
465
|
Require fields on pages that don't match one or more slugs.
|
|
463
466
|
|
|
464
467
|
```typescript
|
|
465
|
-
operand: string | number | null | Array<string, number> //
|
|
466
|
-
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"
|
|
467
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.
|
|
468
471
|
```
|
|
469
472
|
|
|
@@ -520,7 +523,7 @@ defineField({
|
|
|
520
523
|
}),
|
|
521
524
|
```
|
|
522
525
|
|
|
523
|
-
|
|
526
|
+
_Custom error messages are highly recommended here._ Without a custom message above, the default error message would be:
|
|
524
527
|
|
|
525
528
|
```
|
|
526
529
|
“me@googlecom” does not match the pattern /^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$/.
|
|
@@ -655,26 +658,26 @@ defineField({
|
|
|
655
658
|
|
|
656
659
|
---
|
|
657
660
|
|
|
658
|
-
## Extending these and writing your own
|
|
659
|
-
|
|
660
|
-
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.
|
|
661
|
-
|
|
662
661
|
## Roadmap
|
|
663
662
|
|
|
664
663
|
### Nested pathfinders
|
|
665
664
|
|
|
666
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')`.
|
|
667
666
|
|
|
668
|
-
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`.
|
|
669
668
|
|
|
670
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.
|
|
671
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
|
+
|
|
672
673
|
### Image and File checks
|
|
673
674
|
|
|
674
675
|
`minDimensions`, `maxDimensions`, and `fileExtension` should check to see if the field is of type `image` or `file`.
|
|
675
676
|
|
|
676
677
|
Some of the other checks should probably make sure the field is _not_ `image` or `file`.
|
|
677
678
|
|
|
679
|
+
An `aspectRatio(n)` might be nice (ex. `aspectRatio(1.6)` or `aspectRatio(1.6/1)`).
|
|
680
|
+
|
|
678
681
|
### new referencedDocumentFieldEq validator
|
|
679
682
|
|
|
680
683
|
```
|