sanity-advanced-validators 0.4.1 → 0.5.0
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 +137 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,11 +9,13 @@ 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) 🆕
|
|
12
14
|
- [requiredIfSiblingEq](#requiredIfSiblingEq)
|
|
13
15
|
- [requiredIfSiblingNeq](#requiredIfSiblingNeq)
|
|
14
16
|
- [requiredIfSlugEq](#requiredIfSlugEq)
|
|
15
17
|
- [requiredIfSlugNeq](#requiredIfSlugNeq)
|
|
16
|
-
- [regex](#regex)
|
|
18
|
+
- [regex](#regex)
|
|
17
19
|
- [referencedDocumentRequires](#referencedDocumentRequires)
|
|
18
20
|
- [maxDepth](#maxDepth)
|
|
19
21
|
|
|
@@ -38,17 +40,27 @@ const Page = defineType({
|
|
|
38
40
|
name: "someVideoFile",
|
|
39
41
|
type: "file",
|
|
40
42
|
validation: (rule) =>
|
|
41
|
-
rule.custom(
|
|
42
|
-
|
|
43
|
+
rule.custom(
|
|
44
|
+
requiredIfSlugEq(
|
|
45
|
+
'about',
|
|
46
|
+
'A video is required if {slugKey} is {operand}.'
|
|
47
|
+
)
|
|
48
|
+
).custom(
|
|
49
|
+
fileExtension(['mp4', 'mov'])
|
|
50
|
+
)
|
|
43
51
|
})
|
|
44
52
|
defineField({
|
|
45
53
|
name: "posterImage",
|
|
46
54
|
type: "image",
|
|
47
55
|
hidden: ({ parent }) => parent.someVideoFile === null,
|
|
48
56
|
validation: (rule) =>
|
|
49
|
-
rule.custom(
|
|
50
|
-
|
|
51
|
-
|
|
57
|
+
rule.custom(
|
|
58
|
+
requiredIfSiblingNeq('someVideoFile', null)
|
|
59
|
+
).custom(
|
|
60
|
+
minDimensions({ x: 1250, y: 800 })
|
|
61
|
+
).custom(
|
|
62
|
+
maxDimensions({ x: 2500, y: 1600 })
|
|
63
|
+
),
|
|
52
64
|
})
|
|
53
65
|
]
|
|
54
66
|
})
|
|
@@ -75,12 +87,18 @@ const Page = defineType({
|
|
|
75
87
|
defineField({
|
|
76
88
|
name: "catalog",
|
|
77
89
|
type: "file",
|
|
78
|
-
validation: (rule) =>
|
|
90
|
+
validation: (rule) =>
|
|
91
|
+
rule.custom(
|
|
92
|
+
fileExtension("pdf")
|
|
93
|
+
),
|
|
79
94
|
}),
|
|
80
95
|
defineField({
|
|
81
96
|
name: "video",
|
|
82
97
|
type: "file",
|
|
83
|
-
validation: (rule) =>
|
|
98
|
+
validation: (rule) =>
|
|
99
|
+
rule.custom(
|
|
100
|
+
fileExtension(["mp4", "mov", "webm"])
|
|
101
|
+
),
|
|
84
102
|
}),
|
|
85
103
|
],
|
|
86
104
|
})
|
|
@@ -108,7 +126,10 @@ const ImageWithCaption = defineType({
|
|
|
108
126
|
defineField({
|
|
109
127
|
name: "heroImage",
|
|
110
128
|
type: "image",
|
|
111
|
-
validation: (rule) =>
|
|
129
|
+
validation: (rule) =>
|
|
130
|
+
rule.custom(
|
|
131
|
+
minDimensions({ x: 1200, y: 800 })
|
|
132
|
+
),
|
|
112
133
|
}),
|
|
113
134
|
],
|
|
114
135
|
})
|
|
@@ -129,7 +150,10 @@ message?: string // optional custom error message; replaces {x} and {y} with you
|
|
|
129
150
|
defineField({
|
|
130
151
|
name: "heroImage",
|
|
131
152
|
type: "image",
|
|
132
|
-
validation: (rule) =>
|
|
153
|
+
validation: (rule) =>
|
|
154
|
+
rule.custom(
|
|
155
|
+
maxDimensions({ x: 2400, y: 1600 })
|
|
156
|
+
),
|
|
133
157
|
}),
|
|
134
158
|
```
|
|
135
159
|
|
|
@@ -141,15 +165,66 @@ defineField({
|
|
|
141
165
|
type: "image",
|
|
142
166
|
description: "Min: 1200x800, max: 2400x1600.",
|
|
143
167
|
validation: (rule) =>
|
|
144
|
-
rule
|
|
145
|
-
.
|
|
146
|
-
|
|
147
|
-
.custom(
|
|
168
|
+
rule.required()
|
|
169
|
+
.custom(
|
|
170
|
+
minDimensions({ x: 1200, y: 800 })
|
|
171
|
+
).custom(
|
|
172
|
+
maxDimensions({ x: 2400, y: 1600 })
|
|
173
|
+
),
|
|
148
174
|
})
|
|
149
175
|
```
|
|
150
176
|
|
|
151
177
|
---
|
|
152
178
|
|
|
179
|
+
### minCount
|
|
180
|
+
|
|
181
|
+
Enforces that an array contains at least _n_ items.
|
|
182
|
+
|
|
183
|
+
Note that null values are fine; use `rule.required()` to enforce non-nulls.
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
n: number,
|
|
187
|
+
message?: string // optional custom error message; replaces {n} with your minimum count
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
defineField({
|
|
192
|
+
name: "thumbnails",
|
|
193
|
+
type: "array",
|
|
194
|
+
of: [ {type: 'image'} ],
|
|
195
|
+
validation: (rule) =>
|
|
196
|
+
rule.required()
|
|
197
|
+
.custom(
|
|
198
|
+
minCount(3, "At least {n} thumbnails are required.")
|
|
199
|
+
),
|
|
200
|
+
}),
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
### maxCount
|
|
206
|
+
|
|
207
|
+
Enforces that an array contains at most _n_ items.
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
n: number,
|
|
211
|
+
message?: string // optional custom error message; replaces {n} with your maximum count
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
defineField({
|
|
216
|
+
name: "thumbnails",
|
|
217
|
+
type: "array",
|
|
218
|
+
of: [ {type: 'image'} ],
|
|
219
|
+
validation: (rule) =>
|
|
220
|
+
rule.custom(
|
|
221
|
+
maxCount(3, "No more than {n} thumbnails.")
|
|
222
|
+
),
|
|
223
|
+
}),
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
153
228
|
### requiredIfSiblingEq
|
|
154
229
|
|
|
155
230
|
Mark a field as `required` if a sibling field has a particular value. This is the validator we use most. _It’s super effective!_
|
|
@@ -190,7 +265,10 @@ defineType({
|
|
|
190
265
|
'typescript', 'rust', 'python', 'swift'
|
|
191
266
|
]
|
|
192
267
|
},
|
|
193
|
-
validation: rule =>
|
|
268
|
+
validation: rule =>
|
|
269
|
+
rule.custom(
|
|
270
|
+
requiredIfSiblingEq('occupation', 'software engineer')
|
|
271
|
+
),
|
|
194
272
|
hidden: ({parent}) => parent.occuption !== 'software engineer',
|
|
195
273
|
}),
|
|
196
274
|
],
|
|
@@ -218,14 +296,17 @@ defineType({
|
|
|
218
296
|
options: {
|
|
219
297
|
list: ["typescript", "rust", "python", "swift", "latin", "urdu", "klingon"],
|
|
220
298
|
},
|
|
221
|
-
validation: (rule) =>
|
|
299
|
+
validation: (rule) =>
|
|
300
|
+
rule.custom(
|
|
301
|
+
requiredIfSiblingEq("occupation", ["software engineer", "linguist"])
|
|
302
|
+
),
|
|
222
303
|
hidden: ({ parent }) => !["software engineer", "linguist"].includes(parent.occupation),
|
|
223
304
|
}),
|
|
224
305
|
],
|
|
225
306
|
})
|
|
226
307
|
```
|
|
227
308
|
|
|
228
|
-
|
|
309
|
+
It even works for null.
|
|
229
310
|
|
|
230
311
|
```typescript
|
|
231
312
|
defineType({
|
|
@@ -243,11 +324,14 @@ defineType({
|
|
|
243
324
|
defineField({
|
|
244
325
|
name: 'phone',
|
|
245
326
|
type: 'string',
|
|
246
|
-
validation: rule =>
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
327
|
+
validation: rule =>
|
|
328
|
+
rule.custom(
|
|
329
|
+
requiredIfSiblingEq(
|
|
330
|
+
'email',
|
|
331
|
+
null,
|
|
332
|
+
"If you don’t have an email address, a phone number is required."
|
|
333
|
+
)
|
|
334
|
+
)
|
|
251
335
|
})
|
|
252
336
|
],
|
|
253
337
|
})
|
|
@@ -289,7 +373,10 @@ defineType({
|
|
|
289
373
|
name: "explanation",
|
|
290
374
|
description: "Why are you wasting your life this way?",
|
|
291
375
|
type: "text",
|
|
292
|
-
validation: (rule) =>
|
|
376
|
+
validation: (rule) =>
|
|
377
|
+
rule.custom(
|
|
378
|
+
requiredIfSiblingNeq("occupation", "software engineer")
|
|
379
|
+
),
|
|
293
380
|
hidden: ({ parent }) => parent.occuption === "software engineer",
|
|
294
381
|
}), ],
|
|
295
382
|
})
|
|
@@ -322,7 +409,10 @@ defineType({
|
|
|
322
409
|
name: "questionsAndAnswers",
|
|
323
410
|
type: "array",
|
|
324
411
|
of: [{ type: "qaItem" }],
|
|
325
|
-
validation: (rule) =>
|
|
412
|
+
validation: (rule) =>
|
|
413
|
+
rule.custom(
|
|
414
|
+
requiredIfSlugEq("faq")
|
|
415
|
+
),
|
|
326
416
|
hidden: ({ parent }) => parent.slug.current !== "faq",
|
|
327
417
|
}),
|
|
328
418
|
],
|
|
@@ -334,7 +424,10 @@ And this can apply to multiple slugs…
|
|
|
334
424
|
```typescript
|
|
335
425
|
defineField({
|
|
336
426
|
name: "questionsAndAnswers",
|
|
337
|
-
validation: (rule) =>
|
|
427
|
+
validation: (rule) =>
|
|
428
|
+
rule.custom(
|
|
429
|
+
requiredIfSlugEq(["faq", "about"])
|
|
430
|
+
),
|
|
338
431
|
}),
|
|
339
432
|
```
|
|
340
433
|
|
|
@@ -366,7 +459,10 @@ defineType({
|
|
|
366
459
|
description: `Subnav is required on documents that aren’t '/home'`,
|
|
367
460
|
type: "array",
|
|
368
461
|
of: [{ type: "navLink" }],
|
|
369
|
-
validation: (rule) =>
|
|
462
|
+
validation: (rule) =>
|
|
463
|
+
rule.custom(
|
|
464
|
+
requiredIfSlugNeq("home")
|
|
465
|
+
),
|
|
370
466
|
hidden: ({ parent }) => parent.slug.current !== "home",
|
|
371
467
|
}),
|
|
372
468
|
],
|
|
@@ -390,12 +486,13 @@ message?: string // optional custom error message; replaces {pattern} with your
|
|
|
390
486
|
defineField({
|
|
391
487
|
name: 'email',
|
|
392
488
|
type: 'string',
|
|
393
|
-
validation: (rule) =>
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
489
|
+
validation: (rule) =>
|
|
490
|
+
rule.custom(
|
|
491
|
+
regex(
|
|
492
|
+
/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$/,
|
|
493
|
+
"“{value}” is not a valid email address."
|
|
494
|
+
)
|
|
495
|
+
),
|
|
399
496
|
}),
|
|
400
497
|
```
|
|
401
498
|
|
|
@@ -423,7 +520,10 @@ defineField({
|
|
|
423
520
|
description: 'An article (must include a valid poster image)',
|
|
424
521
|
type: 'reference',
|
|
425
522
|
to: [{type: 'article'}],
|
|
426
|
-
validation: (rule) =>
|
|
523
|
+
validation: (rule) =>
|
|
524
|
+
rule.custom(
|
|
525
|
+
referencedDocumentRequires('article', 'poster')
|
|
526
|
+
),
|
|
427
527
|
}),
|
|
428
528
|
```
|
|
429
529
|
|
|
@@ -499,7 +599,10 @@ const navLink = defineType({
|
|
|
499
599
|
name: "subnav",
|
|
500
600
|
type: "array",
|
|
501
601
|
of: [{ type: navigation }],
|
|
502
|
-
validation: (rule) =>
|
|
602
|
+
validation: (rule) =>
|
|
603
|
+
rule.custom(
|
|
604
|
+
maxDepth(3, "subnav")
|
|
605
|
+
),
|
|
503
606
|
}),
|
|
504
607
|
],
|
|
505
608
|
})
|