@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
|
@@ -1,150 +1,150 @@
|
|
|
1
|
-
import RuleClass from '../Rule'
|
|
2
|
-
import normalizeValidationRules from './normalizeValidationRules'
|
|
1
|
+
import RuleClass from '../Rule';
|
|
2
|
+
import normalizeValidationRules from './normalizeValidationRules';
|
|
3
3
|
describe('normalizeValidationRules', () => {
|
|
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
|
-
//# sourceMappingURL=normalizeValidationRules.test.js.map
|
|
4
|
+
// see `infer.test.ts` for more related tests.
|
|
5
|
+
// note the Schema.compile runs this function indirectly via `inferFromSchema`
|
|
6
|
+
it('utilizes schema types to infer base rules', () => {
|
|
7
|
+
const coolNumberType = {
|
|
8
|
+
jsonType: 'number',
|
|
9
|
+
name: 'coolNumber',
|
|
10
|
+
};
|
|
11
|
+
const rules = normalizeValidationRules(coolNumberType);
|
|
12
|
+
expect(rules).toHaveLength(1);
|
|
13
|
+
const [rule] = rules;
|
|
14
|
+
expect(rule).toBeInstanceOf(RuleClass);
|
|
15
|
+
expect(rule._rules).toMatchObject([
|
|
16
|
+
{
|
|
17
|
+
constraint: 'Number',
|
|
18
|
+
flag: 'type',
|
|
19
|
+
},
|
|
20
|
+
]);
|
|
21
|
+
});
|
|
22
|
+
it('follows the type chain to determine the base rule', () => {
|
|
23
|
+
const sickDatetime = {
|
|
24
|
+
type: {
|
|
25
|
+
type: {
|
|
26
|
+
jsonType: 'string',
|
|
27
|
+
},
|
|
28
|
+
name: 'datetime',
|
|
29
|
+
},
|
|
30
|
+
name: 'sickDatetime',
|
|
31
|
+
};
|
|
32
|
+
const rules = normalizeValidationRules(sickDatetime);
|
|
33
|
+
expect(rules).toHaveLength(1);
|
|
34
|
+
const [rule] = rules;
|
|
35
|
+
// type chain is applied from inner to outer so the resulting type should be
|
|
36
|
+
// date instead of string
|
|
37
|
+
expect(rule._rules).toMatchObject([
|
|
38
|
+
{
|
|
39
|
+
constraint: 'Date',
|
|
40
|
+
flag: 'type',
|
|
41
|
+
},
|
|
42
|
+
]);
|
|
43
|
+
});
|
|
44
|
+
it('converts a validation function to a rule instance', () => {
|
|
45
|
+
const coolStringType = {
|
|
46
|
+
jsonType: 'string',
|
|
47
|
+
name: 'coolString',
|
|
48
|
+
validation: (rule) => rule.uppercase(),
|
|
49
|
+
};
|
|
50
|
+
const rules = normalizeValidationRules(coolStringType);
|
|
51
|
+
expect(rules).toHaveLength(1);
|
|
52
|
+
const [rule] = rules;
|
|
53
|
+
expect(rule).toBeInstanceOf(RuleClass);
|
|
54
|
+
expect(rule._rules).toMatchObject([
|
|
55
|
+
{
|
|
56
|
+
constraint: 'String',
|
|
57
|
+
flag: 'type',
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
constraint: 'uppercase',
|
|
61
|
+
flag: 'stringCasing',
|
|
62
|
+
},
|
|
63
|
+
]);
|
|
64
|
+
});
|
|
65
|
+
it('converts falsy values to an empty array', () => {
|
|
66
|
+
expect(normalizeValidationRules(undefined)).toEqual([]);
|
|
67
|
+
});
|
|
68
|
+
it('converts schema list options with titles to `rule.valid` constraints', () => {
|
|
69
|
+
const stringTypeWithOptions = {
|
|
70
|
+
jsonType: 'string',
|
|
71
|
+
name: 'stringTypeWithOptions',
|
|
72
|
+
options: {
|
|
73
|
+
list: [
|
|
74
|
+
{ title: 'Blue', value: 'blue' },
|
|
75
|
+
{ title: 'Red', value: 'red' },
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
const rules = normalizeValidationRules(stringTypeWithOptions);
|
|
80
|
+
expect(rules).toHaveLength(1);
|
|
81
|
+
const [rule] = rules;
|
|
82
|
+
expect(rule).toBeInstanceOf(RuleClass);
|
|
83
|
+
expect(rule._rules).toMatchObject([
|
|
84
|
+
{
|
|
85
|
+
constraint: 'String',
|
|
86
|
+
flag: 'type',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
constraint: ['blue', 'red'],
|
|
90
|
+
flag: 'valid',
|
|
91
|
+
},
|
|
92
|
+
]);
|
|
93
|
+
});
|
|
94
|
+
it('converts schema list options with strings only to `rule.valid` constraints', () => {
|
|
95
|
+
const stringTypeWithOptions = {
|
|
96
|
+
jsonType: 'string',
|
|
97
|
+
name: 'stringTypeWithOptions',
|
|
98
|
+
options: {
|
|
99
|
+
list: ['blue', 'red'],
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
const rules = normalizeValidationRules(stringTypeWithOptions);
|
|
103
|
+
expect(rules).toHaveLength(1);
|
|
104
|
+
const [rule] = rules;
|
|
105
|
+
expect(rule).toBeInstanceOf(RuleClass);
|
|
106
|
+
expect(rule._rules).toMatchObject([
|
|
107
|
+
{
|
|
108
|
+
constraint: 'String',
|
|
109
|
+
flag: 'type',
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
constraint: ['blue', 'red'],
|
|
113
|
+
flag: 'valid',
|
|
114
|
+
},
|
|
115
|
+
]);
|
|
116
|
+
});
|
|
117
|
+
it('converts arrays of validation', () => {
|
|
118
|
+
const coolNumberType = {
|
|
119
|
+
jsonType: 'number',
|
|
120
|
+
name: 'coolNumber',
|
|
121
|
+
validation: [(rule) => rule.greaterThan(3), RuleClass.number().lessThan(5)],
|
|
122
|
+
};
|
|
123
|
+
const rules = normalizeValidationRules(coolNumberType);
|
|
124
|
+
expect(rules).toHaveLength(2);
|
|
125
|
+
const [first, second] = rules;
|
|
126
|
+
expect(first).toBeInstanceOf(RuleClass);
|
|
127
|
+
expect(first._rules).toMatchObject([
|
|
128
|
+
{
|
|
129
|
+
constraint: 'Number',
|
|
130
|
+
flag: 'type',
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
constraint: 3,
|
|
134
|
+
flag: 'greaterThan',
|
|
135
|
+
},
|
|
136
|
+
]);
|
|
137
|
+
expect(second).toBeInstanceOf(RuleClass);
|
|
138
|
+
expect(second._rules).toMatchObject([
|
|
139
|
+
{
|
|
140
|
+
constraint: 'Number',
|
|
141
|
+
flag: 'type',
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
constraint: 5,
|
|
145
|
+
flag: 'lessThan',
|
|
146
|
+
},
|
|
147
|
+
]);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
//# sourceMappingURL=normalizeValidationRules.test.js.map
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {isKeyedObject} from '@sanity/types'
|
|
1
|
+
import { isKeyedObject } from '@sanity/types';
|
|
2
2
|
export default function pathToString(path = []) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
return path.reduce((target, segment, i) => {
|
|
4
|
+
const segmentType = typeof segment;
|
|
5
|
+
if (segmentType === 'number') {
|
|
6
|
+
return `${target}[${segment}]`;
|
|
7
|
+
}
|
|
8
|
+
if (segmentType === 'string') {
|
|
9
|
+
const separator = i === 0 ? '' : '.';
|
|
10
|
+
return `${target}${separator}${segment}`;
|
|
11
|
+
}
|
|
12
|
+
if (isKeyedObject(segment)) {
|
|
13
|
+
return `${target}[_key=="${segment._key}"]`;
|
|
14
|
+
}
|
|
15
|
+
throw new Error(`Unsupported path segment "${segment}"`);
|
|
16
|
+
}, '');
|
|
17
17
|
}
|
|
18
|
-
//# sourceMappingURL=pathToString.js.map
|
|
18
|
+
//# sourceMappingURL=pathToString.js.map
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
// this file was adapted from a previous dependency `type-of-is`
|
|
2
2
|
// https://github.com/stephenhandley/type-of-is/blob/7138a7e79f5af7c286bf8123f60843a91aaebf38/index.js
|
|
3
|
-
const _toString = {}.toString
|
|
4
|
-
const builtIns = [Object, Function, Array, String, Boolean, Number, Date, RegExp, Error]
|
|
3
|
+
const _toString = {}.toString;
|
|
4
|
+
const builtIns = [Object, Function, Array, String, Boolean, Number, Date, RegExp, Error];
|
|
5
5
|
function isBuiltIn(_constructor) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
for (let i = 0; i < builtIns.length; i++) {
|
|
7
|
+
if (builtIns[i] === _constructor)
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
return false;
|
|
10
11
|
}
|
|
11
12
|
export default function typeString(obj) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
// [object Blah] -> Blah
|
|
14
|
+
const stringType = _toString.call(obj).slice(8, -1);
|
|
15
|
+
if (obj === null || obj === undefined)
|
|
16
|
+
return stringType.toLowerCase();
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
18
|
+
const constructorType = obj.constructor;
|
|
19
|
+
if (constructorType && !isBuiltIn(constructorType))
|
|
20
|
+
return constructorType.name;
|
|
21
|
+
return stringType;
|
|
19
22
|
}
|
|
20
|
-
//# sourceMappingURL=typeString.js.map
|
|
23
|
+
//# sourceMappingURL=typeString.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {}
|
|
2
|
-
//# sourceMappingURL=typeString.test.d.ts.map
|
|
1
|
+
export {};
|
|
2
|
+
//# sourceMappingURL=typeString.test.d.ts.map
|
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
import typeString from './typeString'
|
|
1
|
+
import typeString from './typeString';
|
|
2
2
|
describe('typeString', () => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
//# sourceMappingURL=typeString.test.js.map
|
|
3
|
+
it('returns the a type string of built in types', () => {
|
|
4
|
+
expect(typeString({})).toBe('Object');
|
|
5
|
+
expect(typeString(function () {
|
|
6
|
+
// intentionally blank
|
|
7
|
+
})).toBe('Function');
|
|
8
|
+
expect(typeString(['hey'])).toBe('Array');
|
|
9
|
+
expect(typeString('some string')).toBe('String');
|
|
10
|
+
expect(typeString(false)).toBe('Boolean');
|
|
11
|
+
expect(typeString(5)).toBe('Number');
|
|
12
|
+
expect(typeString(new Date())).toBe('Date');
|
|
13
|
+
});
|
|
14
|
+
it('returns a type string string using the constructor', () => {
|
|
15
|
+
class ExampleClass {
|
|
16
|
+
}
|
|
17
|
+
expect(typeString(new ExampleClass())).toBe('ExampleClass');
|
|
18
|
+
});
|
|
19
|
+
it('returns a type string for null or undefined', () => {
|
|
20
|
+
expect(typeString(null)).toBe('null');
|
|
21
|
+
expect(typeString(undefined)).toBe('undefined');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
//# sourceMappingURL=typeString.test.js.map
|