/
Rule.ts
424 lines (356 loc) · 12.7 KB
/
Rule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import {
SchemaType,
Rule as IRule,
RuleClass,
CustomValidator,
RuleSpecConstraint,
FieldRules,
ValidationContext,
RuleSpec,
ValidationMarker,
RuleTypeConstraint,
Validator,
} from '@sanity/types'
import {cloneDeep, get} from 'lodash'
import ValidationErrorClass from './ValidationError'
import escapeRegex from './util/escapeRegex'
import {convertToValidationMarker} from './util/convertToValidationMarker'
import pathToString from './util/pathToString'
import genericValidator from './validators/genericValidator'
import booleanValidator from './validators/booleanValidator'
import numberValidator from './validators/numberValidator'
import stringValidator from './validators/stringValidator'
import arrayValidator from './validators/arrayValidator'
import objectValidator from './validators/objectValidator'
import dateValidator from './validators/dateValidator'
const typeValidators = {
Boolean: booleanValidator,
Number: numberValidator,
String: stringValidator,
Array: arrayValidator,
Object: objectValidator,
Date: dateValidator,
}
const getBaseType = (type: SchemaType | undefined): SchemaType | undefined => {
return type && type.type ? getBaseType(type.type) : type
}
const isFieldRef = (constraint: unknown): constraint is {type: symbol; path: string | string[]} => {
if (typeof constraint !== 'object' || !constraint) return false
return (constraint as Record<string, unknown>).type === Rule.FIELD_REF
}
const EMPTY_ARRAY: unknown[] = []
const FIELD_REF = Symbol('FIELD_REF')
const ruleConstraintTypes: RuleTypeConstraint[] = [
'Array',
'Boolean',
'Date',
'Number',
'Object',
'String',
]
// Note: `RuleClass` and `Rule` are split to fit the current `@sanity/types`
// setup. Classes are a bit weird in the `@sanity/types` package because classes
// create an actual javascript class while simultaneously creating a type
// definition.
//
// This implicitly creates two types:
// 1. the instance type — `Rule` and
// 2. the static/class type - `RuleClass`
//
// The `RuleClass` type contains the static methods and the `Rule` instance
// contains the instance methods.
//
// This package exports the RuleClass as a value without implicitly exporting
// an instance definition. This should help reminder downstream users to import
// from the `@sanity/types` package.
const Rule: RuleClass = class Rule implements IRule {
static readonly FIELD_REF = FIELD_REF
static array = (def?: SchemaType): Rule => new Rule(def).type('Array')
static object = (def?: SchemaType): Rule => new Rule(def).type('Object')
static string = (def?: SchemaType): Rule => new Rule(def).type('String')
static number = (def?: SchemaType): Rule => new Rule(def).type('Number')
static boolean = (def?: SchemaType): Rule => new Rule(def).type('Boolean')
static dateTime = (def?: SchemaType): Rule => new Rule(def).type('Date')
static valueOfField = (path: string | string[]): {type: symbol; path: string | string[]} => ({
type: FIELD_REF,
path,
})
_type: RuleTypeConstraint | undefined = undefined
_level: 'error' | 'warning' | 'info' | undefined = undefined
_required: 'required' | 'optional' | undefined = undefined
_typeDef: SchemaType | undefined = undefined
_message: string | undefined = undefined
_rules: RuleSpec[] = []
_fieldRules: FieldRules | undefined = undefined
constructor(typeDef?: SchemaType) {
this._typeDef = typeDef
this.reset()
}
private _mergeRequired(next: Rule) {
if (this._required === 'required' || next._required === 'required') return 'required'
if (this._required === 'optional' || next._required === 'optional') return 'optional'
return undefined
}
// Alias to static method, since we often have access to an _instance_ of a rule but not the actual Rule class
valueOfField = Rule.valueOfField.bind(Rule)
error(message?: string): Rule {
const rule = this.clone()
rule._level = 'error'
rule._message = message || undefined
return rule
}
warning(message?: string): Rule {
const rule = this.clone()
rule._level = 'warning'
rule._message = message || undefined
return rule
}
info(message?: string): Rule {
const rule = this.clone()
rule._level = 'info'
rule._message = message || undefined
return rule
}
reset(): this {
this._type = this._type || undefined
this._rules = (this._rules || []).filter((rule) => rule.flag === 'type')
this._message = undefined
this._required = undefined
this._level = 'error'
this._fieldRules = undefined
return this
}
isRequired(): boolean {
return this._required === 'required'
}
clone(): Rule {
const rule = new Rule()
rule._type = this._type
rule._message = this._message
rule._required = this._required
rule._rules = cloneDeep(this._rules)
rule._level = this._level
rule._fieldRules = this._fieldRules
rule._typeDef = this._typeDef
return rule
}
cloneWithRules(rules: RuleSpec[]): Rule {
const rule = this.clone()
const newRules = new Set()
rules.forEach((curr) => {
if (curr.flag === 'type') {
rule._type = curr.constraint
}
newRules.add(curr.flag)
})
rule._rules = rule._rules
.filter((curr) => {
const disallowDuplicate = ['type', 'uri', 'email'].includes(curr.flag)
const isDuplicate = newRules.has(curr.flag)
return !(disallowDuplicate && isDuplicate)
})
.concat(rules)
return rule
}
merge(rule: Rule): Rule {
if (this._type && rule._type && this._type !== rule._type) {
throw new Error('merge() failed: conflicting types')
}
const newRule = this.cloneWithRules(rule._rules)
newRule._type = this._type || rule._type
newRule._message = this._message || rule._message
newRule._required = this._mergeRequired(rule)
newRule._level = this._level === 'error' ? rule._level : this._level
return newRule
}
// Validation flag setters
type(targetType: RuleTypeConstraint | Lowercase<RuleTypeConstraint>): Rule {
const type = `${targetType.slice(0, 1).toUpperCase()}${targetType.slice(1)}` as Capitalize<
typeof targetType
>
if (!ruleConstraintTypes.includes(type)) {
throw new Error(`Unknown type "${targetType}"`)
}
const rule = this.cloneWithRules([{flag: 'type', constraint: type}])
rule._type = type
return rule
}
all(children: Rule[]): Rule {
return this.cloneWithRules([{flag: 'all', constraint: children}])
}
either(children: Rule[]): Rule {
return this.cloneWithRules([{flag: 'either', constraint: children}])
}
// Shared rules
optional(): Rule {
const rule = this.cloneWithRules([{flag: 'presence', constraint: 'optional'}])
rule._required = 'optional'
return rule
}
required(): Rule {
const rule = this.cloneWithRules([{flag: 'presence', constraint: 'required'}])
rule._required = 'required'
return rule
}
custom<T = unknown>(fn: CustomValidator<T>): Rule {
return this.cloneWithRules([{flag: 'custom', constraint: fn as CustomValidator}])
}
min(len: number): Rule {
return this.cloneWithRules([{flag: 'min', constraint: len}])
}
max(len: number): Rule {
return this.cloneWithRules([{flag: 'max', constraint: len}])
}
length(len: number): Rule {
return this.cloneWithRules([{flag: 'length', constraint: len}])
}
valid(value: unknown | unknown[]): Rule {
const values = Array.isArray(value) ? value : [value]
return this.cloneWithRules([{flag: 'valid', constraint: values}])
}
// Numbers only
integer(): Rule {
return this.cloneWithRules([{flag: 'integer'}])
}
precision(limit: number): Rule {
return this.cloneWithRules([{flag: 'precision', constraint: limit}])
}
positive(): Rule {
return this.cloneWithRules([{flag: 'min', constraint: 0}])
}
negative(): Rule {
return this.cloneWithRules([{flag: 'lessThan', constraint: 0}])
}
greaterThan(num: number): Rule {
return this.cloneWithRules([{flag: 'greaterThan', constraint: num}])
}
lessThan(num: number): Rule {
return this.cloneWithRules([{flag: 'lessThan', constraint: num}])
}
// String only
uppercase(): Rule {
return this.cloneWithRules([{flag: 'stringCasing', constraint: 'uppercase'}])
}
lowercase(): Rule {
return this.cloneWithRules([{flag: 'stringCasing', constraint: 'lowercase'}])
}
regex(pattern: RegExp, name: string, options: {name?: string; invert?: boolean}): Rule
regex(pattern: RegExp, options: {name?: string; invert?: boolean}): Rule
regex(pattern: RegExp, name: string): Rule
regex(pattern: RegExp): Rule
regex(
pattern: RegExp,
a?: string | {name?: string; invert?: boolean},
b?: {name?: string; invert?: boolean}
): Rule {
const name = typeof a === 'string' ? a : a?.name ?? b?.name
const invert = typeof a === 'string' ? false : a?.invert ?? b?.invert
const constraint: RuleSpecConstraint<'regex'> = {
pattern,
name,
invert: invert || false,
}
return this.cloneWithRules([{flag: 'regex', constraint}])
}
email(): Rule {
return this.cloneWithRules([{flag: 'email'}])
}
uri(opts?: {
scheme?: (string | RegExp) | Array<string | RegExp>
allowRelative?: boolean
relativeOnly?: boolean
allowCredentials?: boolean
}): Rule {
const optsScheme = opts?.scheme || ['http', 'https']
const schemes = Array.isArray(optsScheme) ? optsScheme : [optsScheme]
if (!schemes.length) {
throw new Error('scheme must have at least 1 scheme specified')
}
const constraint: RuleSpecConstraint<'uri'> = {
options: {
scheme: schemes.map((scheme) => {
if (!(scheme instanceof RegExp) && typeof scheme !== 'string') {
throw new Error('scheme must be a RegExp or a String')
}
return typeof scheme === 'string' ? new RegExp(`^${escapeRegex(scheme)}$`) : scheme
}),
allowRelative: opts?.allowRelative || false,
relativeOnly: opts?.relativeOnly || false,
allowCredentials: opts?.allowCredentials || false,
},
}
return this.cloneWithRules([{flag: 'uri', constraint}])
}
// Array only
unique(): Rule {
return this.cloneWithRules([{flag: 'unique'}])
}
// Objects only
reference(): Rule {
return this.cloneWithRules([{flag: 'reference'}])
}
fields(rules: FieldRules): Rule {
if (this._type !== 'Object') {
throw new Error('fields() can only be called on an object type')
}
const rule = this.cloneWithRules([])
rule._fieldRules = rules
return rule
}
assetRequired(): Rule {
const base = getBaseType(this._typeDef)
let assetType: 'Asset' | 'Image' | 'File'
if (base && ['image', 'file'].includes(base.name)) {
assetType = base.name === 'image' ? 'Image' : 'File'
} else {
assetType = 'Asset'
}
return this.cloneWithRules([{flag: 'assetRequired', constraint: {assetType}}])
}
async validate(value: unknown, context: ValidationContext): Promise<ValidationMarker[]> {
if (!context) {
throw new Error('missing context')
}
const valueIsEmpty = value === null || value === undefined
// Short-circuit on optional, empty fields
if (valueIsEmpty && this._required === 'optional') {
return EMPTY_ARRAY as ValidationMarker[]
}
const rules =
// Run only the _custom_ functions if the rule is not set to required or optional
this._required === undefined && valueIsEmpty
? this._rules.filter((curr) => curr.flag === 'custom')
: this._rules
const validators = (this._type && typeValidators[this._type]) || genericValidator
const results = await Promise.all(
rules.map(async (curr) => {
if (curr.flag === undefined) {
throw new Error('Invalid rule, did not contain "flag"-property')
}
const validator: Validator | undefined = validators[curr.flag]
if (!validator) {
const forType = this._type ? `type "${this._type}"` : 'rule without declared type'
throw new Error(`Validator for flag "${curr.flag}" not found for ${forType}`)
}
let specConstraint = 'constraint' in curr ? curr.constraint : null
if (isFieldRef(specConstraint)) {
specConstraint = get(context.parent, specConstraint.path)
}
let result
try {
result = await validator(specConstraint, value, this._message, context)
} catch (err) {
const errorFromException = new ValidationErrorClass(
`${pathToString(context.path)}: Exception occurred while validating value: ${
err.message
}`
)
return convertToValidationMarker(errorFromException, 'error', context)
}
return convertToValidationMarker(result, this._level, context)
})
)
return results.flat()
}
}
export default Rule