@sanity/validation 2.30.1-purple-unicorn.964 → 3.0.0-dev-preview.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/lib/dts/src/Rule.js +317 -312
- package/lib/dts/src/ValidationError.js +16 -16
- package/lib/dts/src/index.js +7 -7
- package/lib/dts/src/inferFromSchema.js +11 -11
- package/lib/dts/src/inferFromSchemaType.js +24 -24
- package/lib/dts/src/util/convertToValidationMarker.js +44 -53
- package/lib/dts/src/util/deepEquals.js +50 -49
- package/lib/dts/src/util/escapeRegex.js +4 -4
- package/lib/dts/src/util/normalizeValidationRules.js +87 -78
- package/lib/dts/src/util/normalizeValidationRules.test.d.ts +2 -2
- package/lib/dts/src/util/normalizeValidationRules.test.js +149 -149
- package/lib/dts/src/util/pathToString.js +16 -16
- package/lib/dts/src/util/typeString.js +17 -14
- package/lib/dts/src/util/typeString.test.d.ts +2 -2
- package/lib/dts/src/util/typeString.test.js +23 -24
- package/lib/dts/src/validateDocument.js +132 -157
- package/lib/dts/src/validateDocument.test.d.ts +2 -2
- package/lib/dts/src/validateDocument.test.js +664 -678
- package/lib/dts/src/validators/arrayValidator.js +75 -75
- package/lib/dts/src/validators/booleanValidator.js +11 -11
- package/lib/dts/src/validators/dateValidator.js +74 -68
- package/lib/dts/src/validators/genericValidator.js +89 -87
- package/lib/dts/src/validators/numberValidator.js +46 -48
- package/lib/dts/src/validators/objectValidator.js +47 -47
- package/lib/dts/src/validators/slugValidator.js +68 -74
- package/lib/dts/src/validators/stringValidator.js +93 -93
- package/lib/dts/test/array.test.d.ts +2 -2
- package/lib/dts/test/array.test.js +54 -78
- package/lib/dts/test/children.test.d.ts +2 -2
- package/lib/dts/test/children.test.js +69 -87
- package/lib/dts/test/createSchema.d.ts +6 -3
- package/lib/dts/test/createSchema.js +17 -17
- package/lib/dts/test/generics.test.d.ts +2 -2
- package/lib/dts/test/generics.test.js +59 -59
- package/lib/dts/test/infer.test.d.ts +2 -2
- package/lib/dts/test/infer.test.js +236 -285
- package/lib/dts/test/mocks/mockSanityClient.d.ts +4 -5
- package/lib/dts/test/mocks/mockSanityClient.d.ts.map +1 -1
- package/lib/dts/test/mocks/mockSanityClient.js +6 -6
- package/lib/dts/test/nullExport.d.ts +3 -3
- package/lib/dts/test/nullExport.js +2 -2
- package/lib/dts/test/numbers.test.d.ts +2 -2
- package/lib/dts/test/numbers.test.js +56 -62
- package/lib/dts/test/strings.test.d.ts +2 -2
- package/lib/dts/test/strings.test.js +99 -150
- package/lib/dts/tsconfig.lib.tsbuildinfo +1 -1
- package/lib/dts/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
package/lib/dts/src/Rule.js
CHANGED
|
@@ -1,34 +1,42 @@
|
|
|
1
|
-
var _a
|
|
2
|
-
import {cloneDeep, get} from 'lodash'
|
|
3
|
-
import ValidationErrorClass from './ValidationError'
|
|
4
|
-
import escapeRegex from './util/escapeRegex'
|
|
5
|
-
import {convertToValidationMarker} from './util/convertToValidationMarker'
|
|
6
|
-
import pathToString from './util/pathToString'
|
|
7
|
-
import genericValidator from './validators/genericValidator'
|
|
8
|
-
import booleanValidator from './validators/booleanValidator'
|
|
9
|
-
import numberValidator from './validators/numberValidator'
|
|
10
|
-
import stringValidator from './validators/stringValidator'
|
|
11
|
-
import arrayValidator from './validators/arrayValidator'
|
|
12
|
-
import objectValidator from './validators/objectValidator'
|
|
13
|
-
import dateValidator from './validators/dateValidator'
|
|
1
|
+
var _a;
|
|
2
|
+
import { cloneDeep, get } from 'lodash';
|
|
3
|
+
import ValidationErrorClass from './ValidationError';
|
|
4
|
+
import escapeRegex from './util/escapeRegex';
|
|
5
|
+
import { convertToValidationMarker } from './util/convertToValidationMarker';
|
|
6
|
+
import pathToString from './util/pathToString';
|
|
7
|
+
import genericValidator from './validators/genericValidator';
|
|
8
|
+
import booleanValidator from './validators/booleanValidator';
|
|
9
|
+
import numberValidator from './validators/numberValidator';
|
|
10
|
+
import stringValidator from './validators/stringValidator';
|
|
11
|
+
import arrayValidator from './validators/arrayValidator';
|
|
12
|
+
import objectValidator from './validators/objectValidator';
|
|
13
|
+
import dateValidator from './validators/dateValidator';
|
|
14
14
|
const typeValidators = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
15
|
+
Boolean: booleanValidator,
|
|
16
|
+
Number: numberValidator,
|
|
17
|
+
String: stringValidator,
|
|
18
|
+
Array: arrayValidator,
|
|
19
|
+
Object: objectValidator,
|
|
20
|
+
Date: dateValidator,
|
|
21
|
+
};
|
|
22
22
|
const getBaseType = (type) => {
|
|
23
|
-
|
|
24
|
-
}
|
|
23
|
+
return type && type.type ? getBaseType(type.type) : type;
|
|
24
|
+
};
|
|
25
25
|
const isFieldRef = (constraint) => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
const
|
|
26
|
+
if (typeof constraint !== 'object' || !constraint)
|
|
27
|
+
return false;
|
|
28
|
+
return constraint.type === Rule.FIELD_REF;
|
|
29
|
+
};
|
|
30
|
+
const EMPTY_ARRAY = [];
|
|
31
|
+
const FIELD_REF = Symbol('FIELD_REF');
|
|
32
|
+
const ruleConstraintTypes = [
|
|
33
|
+
'Array',
|
|
34
|
+
'Boolean',
|
|
35
|
+
'Date',
|
|
36
|
+
'Number',
|
|
37
|
+
'Object',
|
|
38
|
+
'String',
|
|
39
|
+
];
|
|
32
40
|
// Note: `RuleClass` and `Rule` are split to fit the current `@sanity/types`
|
|
33
41
|
// setup. Classes are a bit weird in the `@sanity/types` package because classes
|
|
34
42
|
// create an actual javascript class while simultaneously creating a type
|
|
@@ -44,288 +52,285 @@ const ruleConstraintTypes = ['Array', 'Boolean', 'Date', 'Number', 'Object', 'St
|
|
|
44
52
|
// This package exports the RuleClass as a value without implicitly exporting
|
|
45
53
|
// an instance definition. This should help reminder downstream users to import
|
|
46
54
|
// from the `@sanity/types` package.
|
|
47
|
-
const Rule =
|
|
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
|
-
const constraint = {
|
|
232
|
-
options: {
|
|
233
|
-
scheme: schemes.map((scheme) => {
|
|
234
|
-
if (!(scheme instanceof RegExp) && typeof scheme !== 'string') {
|
|
235
|
-
throw new Error('scheme must be a RegExp or a String')
|
|
55
|
+
const Rule = (_a = class Rule {
|
|
56
|
+
constructor(typeDef) {
|
|
57
|
+
this._type = undefined;
|
|
58
|
+
this._level = undefined;
|
|
59
|
+
this._required = undefined;
|
|
60
|
+
this._typeDef = undefined;
|
|
61
|
+
this._message = undefined;
|
|
62
|
+
this._rules = [];
|
|
63
|
+
this._fieldRules = undefined;
|
|
64
|
+
// Alias to static method, since we often have access to an _instance_ of a rule but not the actual Rule class
|
|
65
|
+
this.valueOfField = Rule.valueOfField.bind(Rule);
|
|
66
|
+
this._typeDef = typeDef;
|
|
67
|
+
this.reset();
|
|
68
|
+
}
|
|
69
|
+
_mergeRequired(next) {
|
|
70
|
+
if (this._required === 'required' || next._required === 'required')
|
|
71
|
+
return 'required';
|
|
72
|
+
if (this._required === 'optional' || next._required === 'optional')
|
|
73
|
+
return 'optional';
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
error(message) {
|
|
77
|
+
const rule = this.clone();
|
|
78
|
+
rule._level = 'error';
|
|
79
|
+
rule._message = message || undefined;
|
|
80
|
+
return rule;
|
|
81
|
+
}
|
|
82
|
+
warning(message) {
|
|
83
|
+
const rule = this.clone();
|
|
84
|
+
rule._level = 'warning';
|
|
85
|
+
rule._message = message || undefined;
|
|
86
|
+
return rule;
|
|
87
|
+
}
|
|
88
|
+
info(message) {
|
|
89
|
+
const rule = this.clone();
|
|
90
|
+
rule._level = 'info';
|
|
91
|
+
rule._message = message || undefined;
|
|
92
|
+
return rule;
|
|
93
|
+
}
|
|
94
|
+
reset() {
|
|
95
|
+
this._type = this._type || undefined;
|
|
96
|
+
this._rules = (this._rules || []).filter((rule) => rule.flag === 'type');
|
|
97
|
+
this._message = undefined;
|
|
98
|
+
this._required = undefined;
|
|
99
|
+
this._level = 'error';
|
|
100
|
+
this._fieldRules = undefined;
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
isRequired() {
|
|
104
|
+
return this._required === 'required';
|
|
105
|
+
}
|
|
106
|
+
clone() {
|
|
107
|
+
const rule = new Rule();
|
|
108
|
+
rule._type = this._type;
|
|
109
|
+
rule._message = this._message;
|
|
110
|
+
rule._required = this._required;
|
|
111
|
+
rule._rules = cloneDeep(this._rules);
|
|
112
|
+
rule._level = this._level;
|
|
113
|
+
rule._fieldRules = this._fieldRules;
|
|
114
|
+
rule._typeDef = this._typeDef;
|
|
115
|
+
return rule;
|
|
116
|
+
}
|
|
117
|
+
cloneWithRules(rules) {
|
|
118
|
+
const rule = this.clone();
|
|
119
|
+
const newRules = new Set();
|
|
120
|
+
rules.forEach((curr) => {
|
|
121
|
+
if (curr.flag === 'type') {
|
|
122
|
+
rule._type = curr.constraint;
|
|
123
|
+
}
|
|
124
|
+
newRules.add(curr.flag);
|
|
125
|
+
});
|
|
126
|
+
rule._rules = rule._rules
|
|
127
|
+
.filter((curr) => {
|
|
128
|
+
const disallowDuplicate = ['type', 'uri', 'email'].includes(curr.flag);
|
|
129
|
+
const isDuplicate = newRules.has(curr.flag);
|
|
130
|
+
return !(disallowDuplicate && isDuplicate);
|
|
131
|
+
})
|
|
132
|
+
.concat(rules);
|
|
133
|
+
return rule;
|
|
134
|
+
}
|
|
135
|
+
merge(rule) {
|
|
136
|
+
if (this._type && rule._type && this._type !== rule._type) {
|
|
137
|
+
throw new Error('merge() failed: conflicting types');
|
|
138
|
+
}
|
|
139
|
+
const newRule = this.cloneWithRules(rule._rules);
|
|
140
|
+
newRule._type = this._type || rule._type;
|
|
141
|
+
newRule._message = this._message || rule._message;
|
|
142
|
+
newRule._required = this._mergeRequired(rule);
|
|
143
|
+
newRule._level = this._level === 'error' ? rule._level : this._level;
|
|
144
|
+
return newRule;
|
|
145
|
+
}
|
|
146
|
+
// Validation flag setters
|
|
147
|
+
type(targetType) {
|
|
148
|
+
const type = `${targetType.slice(0, 1).toUpperCase()}${targetType.slice(1)}`;
|
|
149
|
+
if (!ruleConstraintTypes.includes(type)) {
|
|
150
|
+
throw new Error(`Unknown type "${targetType}"`);
|
|
151
|
+
}
|
|
152
|
+
const rule = this.cloneWithRules([{ flag: 'type', constraint: type }]);
|
|
153
|
+
rule._type = type;
|
|
154
|
+
return rule;
|
|
155
|
+
}
|
|
156
|
+
all(children) {
|
|
157
|
+
return this.cloneWithRules([{ flag: 'all', constraint: children }]);
|
|
158
|
+
}
|
|
159
|
+
either(children) {
|
|
160
|
+
return this.cloneWithRules([{ flag: 'either', constraint: children }]);
|
|
161
|
+
}
|
|
162
|
+
// Shared rules
|
|
163
|
+
optional() {
|
|
164
|
+
const rule = this.cloneWithRules([{ flag: 'presence', constraint: 'optional' }]);
|
|
165
|
+
rule._required = 'optional';
|
|
166
|
+
return rule;
|
|
167
|
+
}
|
|
168
|
+
required() {
|
|
169
|
+
const rule = this.cloneWithRules([{ flag: 'presence', constraint: 'required' }]);
|
|
170
|
+
rule._required = 'required';
|
|
171
|
+
return rule;
|
|
172
|
+
}
|
|
173
|
+
custom(fn) {
|
|
174
|
+
return this.cloneWithRules([{ flag: 'custom', constraint: fn }]);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* @deprecated use `Rule.custom` instead
|
|
178
|
+
*/
|
|
179
|
+
block(fn) {
|
|
180
|
+
return this.cloneWithRules([{ flag: 'custom', constraint: fn }]);
|
|
181
|
+
}
|
|
182
|
+
min(len) {
|
|
183
|
+
return this.cloneWithRules([{ flag: 'min', constraint: len }]);
|
|
184
|
+
}
|
|
185
|
+
max(len) {
|
|
186
|
+
return this.cloneWithRules([{ flag: 'max', constraint: len }]);
|
|
187
|
+
}
|
|
188
|
+
length(len) {
|
|
189
|
+
return this.cloneWithRules([{ flag: 'length', constraint: len }]);
|
|
190
|
+
}
|
|
191
|
+
valid(value) {
|
|
192
|
+
const values = Array.isArray(value) ? value : [value];
|
|
193
|
+
return this.cloneWithRules([{ flag: 'valid', constraint: values }]);
|
|
194
|
+
}
|
|
195
|
+
// Numbers only
|
|
196
|
+
integer() {
|
|
197
|
+
return this.cloneWithRules([{ flag: 'integer' }]);
|
|
198
|
+
}
|
|
199
|
+
precision(limit) {
|
|
200
|
+
return this.cloneWithRules([{ flag: 'precision', constraint: limit }]);
|
|
201
|
+
}
|
|
202
|
+
positive() {
|
|
203
|
+
return this.cloneWithRules([{ flag: 'min', constraint: 0 }]);
|
|
204
|
+
}
|
|
205
|
+
negative() {
|
|
206
|
+
return this.cloneWithRules([{ flag: 'lessThan', constraint: 0 }]);
|
|
207
|
+
}
|
|
208
|
+
greaterThan(num) {
|
|
209
|
+
return this.cloneWithRules([{ flag: 'greaterThan', constraint: num }]);
|
|
210
|
+
}
|
|
211
|
+
lessThan(num) {
|
|
212
|
+
return this.cloneWithRules([{ flag: 'lessThan', constraint: num }]);
|
|
213
|
+
}
|
|
214
|
+
// String only
|
|
215
|
+
uppercase() {
|
|
216
|
+
return this.cloneWithRules([{ flag: 'stringCasing', constraint: 'uppercase' }]);
|
|
217
|
+
}
|
|
218
|
+
lowercase() {
|
|
219
|
+
return this.cloneWithRules([{ flag: 'stringCasing', constraint: 'lowercase' }]);
|
|
220
|
+
}
|
|
221
|
+
regex(pattern, a, b) {
|
|
222
|
+
const name = typeof a === 'string' ? a : a?.name ?? b?.name;
|
|
223
|
+
const invert = typeof a === 'string' ? false : a?.invert ?? b?.invert;
|
|
224
|
+
const constraint = {
|
|
225
|
+
pattern,
|
|
226
|
+
name,
|
|
227
|
+
invert: invert || false,
|
|
228
|
+
};
|
|
229
|
+
return this.cloneWithRules([{ flag: 'regex', constraint }]);
|
|
230
|
+
}
|
|
231
|
+
email() {
|
|
232
|
+
return this.cloneWithRules([{ flag: 'email' }]);
|
|
233
|
+
}
|
|
234
|
+
uri(opts) {
|
|
235
|
+
const optsScheme = opts?.scheme || ['http', 'https'];
|
|
236
|
+
const schemes = Array.isArray(optsScheme) ? optsScheme : [optsScheme];
|
|
237
|
+
if (!schemes.length) {
|
|
238
|
+
throw new Error('scheme must have at least 1 scheme specified');
|
|
236
239
|
}
|
|
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
|
-
const
|
|
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
|
-
type
|
|
327
|
-
path
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
240
|
+
const constraint = {
|
|
241
|
+
options: {
|
|
242
|
+
scheme: schemes.map((scheme) => {
|
|
243
|
+
if (!(scheme instanceof RegExp) && typeof scheme !== 'string') {
|
|
244
|
+
throw new Error('scheme must be a RegExp or a String');
|
|
245
|
+
}
|
|
246
|
+
return typeof scheme === 'string' ? new RegExp(`^${escapeRegex(scheme)}$`) : scheme;
|
|
247
|
+
}),
|
|
248
|
+
allowRelative: opts?.allowRelative || false,
|
|
249
|
+
relativeOnly: opts?.relativeOnly || false,
|
|
250
|
+
allowCredentials: opts?.allowCredentials || false,
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
return this.cloneWithRules([{ flag: 'uri', constraint }]);
|
|
254
|
+
}
|
|
255
|
+
// Array only
|
|
256
|
+
unique() {
|
|
257
|
+
return this.cloneWithRules([{ flag: 'unique' }]);
|
|
258
|
+
}
|
|
259
|
+
// Objects only
|
|
260
|
+
reference() {
|
|
261
|
+
return this.cloneWithRules([{ flag: 'reference' }]);
|
|
262
|
+
}
|
|
263
|
+
fields(rules) {
|
|
264
|
+
if (this._type !== 'Object') {
|
|
265
|
+
throw new Error('fields() can only be called on an object type');
|
|
266
|
+
}
|
|
267
|
+
const rule = this.cloneWithRules([]);
|
|
268
|
+
rule._fieldRules = rules;
|
|
269
|
+
return rule;
|
|
270
|
+
}
|
|
271
|
+
assetRequired() {
|
|
272
|
+
const base = getBaseType(this._typeDef);
|
|
273
|
+
let assetType;
|
|
274
|
+
if (base && ['image', 'file'].includes(base.name)) {
|
|
275
|
+
assetType = base.name === 'image' ? 'Image' : 'File';
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
assetType = 'Asset';
|
|
279
|
+
}
|
|
280
|
+
return this.cloneWithRules([{ flag: 'assetRequired', constraint: { assetType } }]);
|
|
281
|
+
}
|
|
282
|
+
async validate(value, context) {
|
|
283
|
+
if (!context) {
|
|
284
|
+
throw new Error('missing context');
|
|
285
|
+
}
|
|
286
|
+
const valueIsEmpty = value === null || value === undefined;
|
|
287
|
+
// Short-circuit on optional, empty fields
|
|
288
|
+
if (valueIsEmpty && this._required === 'optional') {
|
|
289
|
+
return EMPTY_ARRAY;
|
|
290
|
+
}
|
|
291
|
+
const rules =
|
|
292
|
+
// Run only the _custom_ functions if the rule is not set to required or optional
|
|
293
|
+
this._required === undefined && valueIsEmpty
|
|
294
|
+
? this._rules.filter((curr) => curr.flag === 'custom')
|
|
295
|
+
: this._rules;
|
|
296
|
+
const validators = (this._type && typeValidators[this._type]) || genericValidator;
|
|
297
|
+
const results = await Promise.all(rules.map(async (curr) => {
|
|
298
|
+
if (curr.flag === undefined) {
|
|
299
|
+
throw new Error('Invalid rule, did not contain "flag"-property');
|
|
300
|
+
}
|
|
301
|
+
const validator = validators[curr.flag];
|
|
302
|
+
if (!validator) {
|
|
303
|
+
const forType = this._type ? `type "${this._type}"` : 'rule without declared type';
|
|
304
|
+
throw new Error(`Validator for flag "${curr.flag}" not found for ${forType}`);
|
|
305
|
+
}
|
|
306
|
+
let specConstraint = 'constraint' in curr ? curr.constraint : null;
|
|
307
|
+
if (isFieldRef(specConstraint)) {
|
|
308
|
+
specConstraint = get(context.parent, specConstraint.path);
|
|
309
|
+
}
|
|
310
|
+
let result;
|
|
311
|
+
try {
|
|
312
|
+
result = await validator(specConstraint, value, this._message, context);
|
|
313
|
+
}
|
|
314
|
+
catch (err) {
|
|
315
|
+
const errorFromException = new ValidationErrorClass(`${pathToString(context.path)}: Exception occurred while validating value: ${err.message}`);
|
|
316
|
+
return convertToValidationMarker(errorFromException, 'error', context);
|
|
317
|
+
}
|
|
318
|
+
return convertToValidationMarker(result, this._level, context);
|
|
319
|
+
}));
|
|
320
|
+
return results.flat();
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
_a.FIELD_REF = FIELD_REF,
|
|
324
|
+
_a.array = (def) => new _a(def).type('Array'),
|
|
325
|
+
_a.object = (def) => new _a(def).type('Object'),
|
|
326
|
+
_a.string = (def) => new _a(def).type('String'),
|
|
327
|
+
_a.number = (def) => new _a(def).type('Number'),
|
|
328
|
+
_a.boolean = (def) => new _a(def).type('Boolean'),
|
|
329
|
+
_a.dateTime = (def) => new _a(def).type('Date'),
|
|
330
|
+
_a.valueOfField = (path) => ({
|
|
331
|
+
type: FIELD_REF,
|
|
332
|
+
path,
|
|
333
|
+
}),
|
|
334
|
+
_a);
|
|
335
|
+
export default Rule;
|
|
336
|
+
//# sourceMappingURL=Rule.js.map
|