@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.
Files changed (48) hide show
  1. package/lib/dts/src/Rule.js +317 -312
  2. package/lib/dts/src/ValidationError.js +16 -16
  3. package/lib/dts/src/index.js +7 -7
  4. package/lib/dts/src/inferFromSchema.js +11 -11
  5. package/lib/dts/src/inferFromSchemaType.js +24 -24
  6. package/lib/dts/src/util/convertToValidationMarker.js +44 -53
  7. package/lib/dts/src/util/deepEquals.js +50 -49
  8. package/lib/dts/src/util/escapeRegex.js +4 -4
  9. package/lib/dts/src/util/normalizeValidationRules.js +87 -78
  10. package/lib/dts/src/util/normalizeValidationRules.test.d.ts +2 -2
  11. package/lib/dts/src/util/normalizeValidationRules.test.js +149 -149
  12. package/lib/dts/src/util/pathToString.js +16 -16
  13. package/lib/dts/src/util/typeString.js +17 -14
  14. package/lib/dts/src/util/typeString.test.d.ts +2 -2
  15. package/lib/dts/src/util/typeString.test.js +23 -24
  16. package/lib/dts/src/validateDocument.js +132 -157
  17. package/lib/dts/src/validateDocument.test.d.ts +2 -2
  18. package/lib/dts/src/validateDocument.test.js +664 -678
  19. package/lib/dts/src/validators/arrayValidator.js +75 -75
  20. package/lib/dts/src/validators/booleanValidator.js +11 -11
  21. package/lib/dts/src/validators/dateValidator.js +74 -68
  22. package/lib/dts/src/validators/genericValidator.js +89 -87
  23. package/lib/dts/src/validators/numberValidator.js +46 -48
  24. package/lib/dts/src/validators/objectValidator.js +47 -47
  25. package/lib/dts/src/validators/slugValidator.js +68 -74
  26. package/lib/dts/src/validators/stringValidator.js +93 -93
  27. package/lib/dts/test/array.test.d.ts +2 -2
  28. package/lib/dts/test/array.test.js +54 -78
  29. package/lib/dts/test/children.test.d.ts +2 -2
  30. package/lib/dts/test/children.test.js +69 -87
  31. package/lib/dts/test/createSchema.d.ts +6 -3
  32. package/lib/dts/test/createSchema.js +17 -17
  33. package/lib/dts/test/generics.test.d.ts +2 -2
  34. package/lib/dts/test/generics.test.js +59 -59
  35. package/lib/dts/test/infer.test.d.ts +2 -2
  36. package/lib/dts/test/infer.test.js +236 -285
  37. package/lib/dts/test/mocks/mockSanityClient.d.ts +4 -5
  38. package/lib/dts/test/mocks/mockSanityClient.d.ts.map +1 -1
  39. package/lib/dts/test/mocks/mockSanityClient.js +6 -6
  40. package/lib/dts/test/nullExport.d.ts +3 -3
  41. package/lib/dts/test/nullExport.js +2 -2
  42. package/lib/dts/test/numbers.test.d.ts +2 -2
  43. package/lib/dts/test/numbers.test.js +56 -62
  44. package/lib/dts/test/strings.test.d.ts +2 -2
  45. package/lib/dts/test/strings.test.js +99 -150
  46. package/lib/dts/tsconfig.lib.tsbuildinfo +1 -1
  47. package/lib/dts/tsconfig.tsbuildinfo +1 -1
  48. package/package.json +4 -4
@@ -1,63 +1,57 @@
1
- import {Rule} from '../src'
2
- const context = {client: {}}
1
+ import { Rule } from '../src';
2
+ const context = { client: {} };
3
3
  describe('number', () => {
4
- test('required constraint', async () => {
5
- const rule = Rule.number().required()
6
- await expect(rule.validate(undefined, context)).resolves.toMatchSnapshot('required: undefined')
7
- await expect(rule.validate(13, context)).resolves.toMatchSnapshot('required: valid')
8
- })
9
- test('min constraint', async () => {
10
- const rule = Rule.number().min(10)
11
- await expect(rule.validate(3, context)).resolves.toMatchSnapshot('min: too low')
12
- await expect(rule.validate(10, context)).resolves.toMatchSnapshot('min: valid, at limit')
13
- await expect(rule.validate(20, context)).resolves.toMatchSnapshot('min: valid')
14
- })
15
- test('greater than constraint', async () => {
16
- const rule = Rule.number().greaterThan(10)
17
- await expect(rule.validate(3, context)).resolves.toMatchSnapshot('gt: too low')
18
- await expect(rule.validate(10, context)).resolves.toMatchSnapshot('gt: too low, at limit')
19
- await expect(rule.validate(20, context)).resolves.toMatchSnapshot('gt: valid')
20
- })
21
- test('max constraint', async () => {
22
- const rule = Rule.number().max(10)
23
- await expect(rule.validate(20, context)).resolves.toMatchSnapshot('max: too large')
24
- await expect(rule.validate(10, context)).resolves.toMatchSnapshot('max: valid, at limit')
25
- await expect(rule.validate(5, context)).resolves.toMatchSnapshot('max: valid')
26
- })
27
- test('less than constraint', async () => {
28
- const rule = Rule.number().lessThan(10)
29
- await expect(rule.validate(20, context)).resolves.toMatchSnapshot('lt: too high')
30
- await expect(rule.validate(10, context)).resolves.toMatchSnapshot('lt: too high, at limit')
31
- await expect(rule.validate(3, context)).resolves.toMatchSnapshot('lt: valid')
32
- })
33
- test('integer constraint', async () => {
34
- const rule = Rule.number().integer()
35
- await expect(rule.validate(31.14, context)).resolves.toMatchSnapshot('integer: invalid (float)')
36
- await expect(rule.validate(31, context)).resolves.toMatchSnapshot('integer: valid')
37
- })
38
- test('precision constraint', async () => {
39
- const rule = Rule.number().precision(3)
40
- await expect(rule.validate(Math.PI, context)).resolves.toMatchSnapshot(
41
- 'precision: invalid (pi)'
42
- )
43
- await expect(rule.validate(31.133, context)).resolves.toMatchSnapshot(
44
- 'precision: valid (at limit)'
45
- )
46
- await expect(rule.validate(31.3, context)).resolves.toMatchSnapshot(
47
- 'precision: valid (below limit)'
48
- )
49
- })
50
- test('positive constraint', async () => {
51
- const rule = Rule.number().positive()
52
- await expect(rule.validate(-31.14, context)).resolves.toMatchSnapshot('positive: invalid')
53
- await expect(rule.validate(0, context)).resolves.toMatchSnapshot('positive: valid (zero)')
54
- await expect(rule.validate(13, context)).resolves.toMatchSnapshot('positive: valid')
55
- })
56
- test('negative constraint', async () => {
57
- const rule = Rule.number().negative()
58
- await expect(rule.validate(31.14, context)).resolves.toMatchSnapshot('negative: invalid')
59
- await expect(rule.validate(0, context)).resolves.toMatchSnapshot('negative: invalid (zero)')
60
- await expect(rule.validate(-13, context)).resolves.toMatchSnapshot('negative: valid')
61
- })
62
- })
63
- //# sourceMappingURL=numbers.test.js.map
4
+ test('required constraint', async () => {
5
+ const rule = Rule.number().required();
6
+ await expect(rule.validate(undefined, context)).resolves.toMatchSnapshot('required: undefined');
7
+ await expect(rule.validate(13, context)).resolves.toMatchSnapshot('required: valid');
8
+ });
9
+ test('min constraint', async () => {
10
+ const rule = Rule.number().min(10);
11
+ await expect(rule.validate(3, context)).resolves.toMatchSnapshot('min: too low');
12
+ await expect(rule.validate(10, context)).resolves.toMatchSnapshot('min: valid, at limit');
13
+ await expect(rule.validate(20, context)).resolves.toMatchSnapshot('min: valid');
14
+ });
15
+ test('greater than constraint', async () => {
16
+ const rule = Rule.number().greaterThan(10);
17
+ await expect(rule.validate(3, context)).resolves.toMatchSnapshot('gt: too low');
18
+ await expect(rule.validate(10, context)).resolves.toMatchSnapshot('gt: too low, at limit');
19
+ await expect(rule.validate(20, context)).resolves.toMatchSnapshot('gt: valid');
20
+ });
21
+ test('max constraint', async () => {
22
+ const rule = Rule.number().max(10);
23
+ await expect(rule.validate(20, context)).resolves.toMatchSnapshot('max: too large');
24
+ await expect(rule.validate(10, context)).resolves.toMatchSnapshot('max: valid, at limit');
25
+ await expect(rule.validate(5, context)).resolves.toMatchSnapshot('max: valid');
26
+ });
27
+ test('less than constraint', async () => {
28
+ const rule = Rule.number().lessThan(10);
29
+ await expect(rule.validate(20, context)).resolves.toMatchSnapshot('lt: too high');
30
+ await expect(rule.validate(10, context)).resolves.toMatchSnapshot('lt: too high, at limit');
31
+ await expect(rule.validate(3, context)).resolves.toMatchSnapshot('lt: valid');
32
+ });
33
+ test('integer constraint', async () => {
34
+ const rule = Rule.number().integer();
35
+ await expect(rule.validate(31.14, context)).resolves.toMatchSnapshot('integer: invalid (float)');
36
+ await expect(rule.validate(31, context)).resolves.toMatchSnapshot('integer: valid');
37
+ });
38
+ test('precision constraint', async () => {
39
+ const rule = Rule.number().precision(3);
40
+ await expect(rule.validate(Math.PI, context)).resolves.toMatchSnapshot('precision: invalid (pi)');
41
+ await expect(rule.validate(31.133, context)).resolves.toMatchSnapshot('precision: valid (at limit)');
42
+ await expect(rule.validate(31.3, context)).resolves.toMatchSnapshot('precision: valid (below limit)');
43
+ });
44
+ test('positive constraint', async () => {
45
+ const rule = Rule.number().positive();
46
+ await expect(rule.validate(-31.14, context)).resolves.toMatchSnapshot('positive: invalid');
47
+ await expect(rule.validate(0, context)).resolves.toMatchSnapshot('positive: valid (zero)');
48
+ await expect(rule.validate(13, context)).resolves.toMatchSnapshot('positive: valid');
49
+ });
50
+ test('negative constraint', async () => {
51
+ const rule = Rule.number().negative();
52
+ await expect(rule.validate(31.14, context)).resolves.toMatchSnapshot('negative: invalid');
53
+ await expect(rule.validate(0, context)).resolves.toMatchSnapshot('negative: invalid (zero)');
54
+ await expect(rule.validate(-13, context)).resolves.toMatchSnapshot('negative: valid');
55
+ });
56
+ });
57
+ //# sourceMappingURL=numbers.test.js.map
@@ -1,2 +1,2 @@
1
- export {}
2
- //# sourceMappingURL=strings.test.d.ts.map
1
+ export {};
2
+ //# sourceMappingURL=strings.test.d.ts.map
@@ -1,151 +1,100 @@
1
- import {Rule} from '../src'
2
- const context = {client: {}}
1
+ import { Rule } from '../src';
2
+ const context = { client: {} };
3
3
  describe('string', () => {
4
- test('required constraint', async () => {
5
- const rule = Rule.string().required()
6
- await expect(rule.validate('', context)).resolves.toMatchSnapshot('required: empty string')
7
- await expect(rule.validate(null, context)).resolves.toMatchSnapshot('required: null')
8
- await expect(rule.validate(undefined, context)).resolves.toMatchSnapshot('required: undefined')
9
- await expect(rule.validate('abc', context)).resolves.toMatchSnapshot('required: valid')
10
- })
11
- test('min length constraint', async () => {
12
- const rule = Rule.string().min(2)
13
- await expect(rule.validate('a', context)).resolves.toMatchSnapshot('min length: too short')
14
- await expect(rule.validate('abc', context)).resolves.toMatchSnapshot('min length: valid')
15
- })
16
- test('max length constraint', async () => {
17
- const rule = Rule.string().max(5)
18
- await expect(rule.validate('abcdefg', context)).resolves.toMatchSnapshot('max length: too long')
19
- await expect(rule.validate('abc', context)).resolves.toMatchSnapshot('max length: valid')
20
- })
21
- test('exact length constraint', async () => {
22
- const rule = Rule.string().length(5)
23
- await expect(rule.validate('abcdefgh', context)).resolves.toMatchSnapshot(
24
- 'exact length: too long'
25
- )
26
- await expect(rule.validate('abc', context)).resolves.toMatchSnapshot('exact length: too short')
27
- await expect(rule.validate('abcde', context)).resolves.toMatchSnapshot('exact length: valid')
28
- })
29
- test('uppercase constraint', async () => {
30
- const rule = Rule.string().uppercase()
31
- await expect(rule.validate('sanity', context)).resolves.toMatchSnapshot(
32
- 'uppercase: all lowercase'
33
- )
34
- await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot(
35
- 'uppercase: some lowercase'
36
- )
37
- await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot(
38
- 'uppercase: some lowercase'
39
- )
40
- await expect(rule.validate('SäNITY', context)).resolves.toMatchSnapshot(
41
- 'uppercase: locale characters'
42
- )
43
- await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot('uppercase: valid')
44
- })
45
- test('lowercase constraint', async () => {
46
- const rule = Rule.string().lowercase()
47
- await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot(
48
- 'lowercase: all uppercase'
49
- )
50
- await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot(
51
- 'lowercase: some uppercase'
52
- )
53
- await expect(rule.validate('sÄnity', context)).resolves.toMatchSnapshot(
54
- 'lowercase: locale characters'
55
- )
56
- await expect(rule.validate('sanity', context)).resolves.toMatchSnapshot('lowercase: valid')
57
- })
58
- test('regex constraint', async () => {
59
- const rule = Rule.string().regex(/^[A-Z][a-z]+$/)
60
- await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot('regex: non-match')
61
- await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('regex: match')
62
- })
63
- test('regex constraint (inverted)', async () => {
64
- const rule = Rule.string().regex(/^[A-Z][a-z]+$/, {invert: true})
65
- await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot(
66
- 'regex: inverted non-match'
67
- )
68
- await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('regex: inverted match')
69
- })
70
- test('regex constraint (custom pattern name)', async () => {
71
- const rule = Rule.string().regex(/^[A-Z][a-z]+$/, 'PascalCase')
72
- await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot(
73
- 'regex: non-match w/ custom pattern name'
74
- )
75
- await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot(
76
- 'regex: match w/ custom pattern name'
77
- )
78
- })
79
- test('regex constraint (custom pattern name, as options)', async () => {
80
- const rule = Rule.string().regex(/^[A-Z][a-z]+$/, {name: 'PascalCase'})
81
- await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot(
82
- 'regex: non-match w/ custom pattern name (opt)'
83
- )
84
- await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot(
85
- 'regex: match w/ custom pattern name (opt)'
86
- )
87
- })
88
- test('uri constraint', async () => {
89
- const rule = Rule.string().uri()
90
- await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot('uri: non-match')
91
- await expect(rule.validate('https://sanity.io/', context)).resolves.toHaveLength(0)
92
- })
93
- test('uri constraint (with unicode chars)', async () => {
94
- const rule = Rule.string().uri()
95
- await expect(rule.validate('Blåbærsyltetøy', context)).resolves.toMatchSnapshot(
96
- 'uri: non-match'
97
- )
98
- await expect(
99
- rule.validate('https://en.wikipedia.org/wiki/San_&_Søn', context)
100
- ).resolves.toHaveLength(0)
101
- await expect(
102
- rule.validate('https://zh.wikipedia.org/wiki/心形符號', context)
103
- ).resolves.toHaveLength(0)
104
- await expect(
105
- rule.validate('https://ru.wikipedia.org/wiki/Зонтичные', context)
106
- ).resolves.toHaveLength(0)
107
- await expect(rule.validate('https://påtapp.no/oslo', context)).resolves.toHaveLength(0)
108
- })
109
- test('uri constraint (invalid protocol)', async () => {
110
- const rule = Rule.string().uri({scheme: ['http', 'ftp']})
111
- await expect(rule.validate('https://sanity.io/', context)).resolves.toMatchSnapshot(
112
- 'uri: protocol non-match'
113
- )
114
- await expect(rule.validate('ftp://code.sanity.io/', context)).resolves.toHaveLength(0)
115
- })
116
- test('uri constraint (credentials)', async () => {
117
- let rule = Rule.string().uri({allowCredentials: true})
118
- await expect(rule.validate('http://foo:bar@sanity.io/', context)).resolves.toHaveLength(0)
119
- await expect(rule.validate('http://sanity.io/', context)).resolves.toHaveLength(0)
120
- rule = Rule.string().uri({allowCredentials: false})
121
- await expect(rule.validate('http://sanity.io/', context)).resolves.toHaveLength(0)
122
- await expect(rule.validate('http://foo:bar@sanity.io/', context)).resolves.toMatchSnapshot(
123
- 'uri: credentials specified but not allowed'
124
- )
125
- await expect(rule.validate('http://espen@sanity.io/', context)).resolves.toMatchSnapshot(
126
- 'uri: username specified but not allowed'
127
- )
128
- })
129
- test('custom rule with string', async () => {
130
- const rule = Rule.string().custom((val) =>
131
- val.split('').reverse().join('') === val ? true : 'Must be a palindrome!'
132
- )
133
- await expect(rule.validate('hei', context)).resolves.toMatchSnapshot('not a palindrome')
134
- await expect(rule.validate('madam', context)).resolves.toHaveLength(0)
135
- })
136
- test('custom async rule with string', async () => {
137
- const rule = Rule.string().custom(
138
- (val) =>
139
- new Promise((resolve) =>
140
- setTimeout(
141
- () =>
142
- resolve(val.split('').reverse().join('') === val ? true : 'Must be a palindrome!'),
143
- 50
144
- )
145
- )
146
- )
147
- await expect(rule.validate('hei', context)).resolves.toMatchSnapshot('not a palindrome')
148
- await expect(rule.validate('madam', context)).resolves.toHaveLength(0)
149
- })
150
- })
151
- //# sourceMappingURL=strings.test.js.map
4
+ test('required constraint', async () => {
5
+ const rule = Rule.string().required();
6
+ await expect(rule.validate('', context)).resolves.toMatchSnapshot('required: empty string');
7
+ await expect(rule.validate(null, context)).resolves.toMatchSnapshot('required: null');
8
+ await expect(rule.validate(undefined, context)).resolves.toMatchSnapshot('required: undefined');
9
+ await expect(rule.validate('abc', context)).resolves.toMatchSnapshot('required: valid');
10
+ });
11
+ test('min length constraint', async () => {
12
+ const rule = Rule.string().min(2);
13
+ await expect(rule.validate('a', context)).resolves.toMatchSnapshot('min length: too short');
14
+ await expect(rule.validate('abc', context)).resolves.toMatchSnapshot('min length: valid');
15
+ });
16
+ test('max length constraint', async () => {
17
+ const rule = Rule.string().max(5);
18
+ await expect(rule.validate('abcdefg', context)).resolves.toMatchSnapshot('max length: too long');
19
+ await expect(rule.validate('abc', context)).resolves.toMatchSnapshot('max length: valid');
20
+ });
21
+ test('exact length constraint', async () => {
22
+ const rule = Rule.string().length(5);
23
+ await expect(rule.validate('abcdefgh', context)).resolves.toMatchSnapshot('exact length: too long');
24
+ await expect(rule.validate('abc', context)).resolves.toMatchSnapshot('exact length: too short');
25
+ await expect(rule.validate('abcde', context)).resolves.toMatchSnapshot('exact length: valid');
26
+ });
27
+ test('uppercase constraint', async () => {
28
+ const rule = Rule.string().uppercase();
29
+ await expect(rule.validate('sanity', context)).resolves.toMatchSnapshot('uppercase: all lowercase');
30
+ await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('uppercase: some lowercase');
31
+ await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('uppercase: some lowercase');
32
+ await expect(rule.validate('SäNITY', context)).resolves.toMatchSnapshot('uppercase: locale characters');
33
+ await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot('uppercase: valid');
34
+ });
35
+ test('lowercase constraint', async () => {
36
+ const rule = Rule.string().lowercase();
37
+ await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot('lowercase: all uppercase');
38
+ await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('lowercase: some uppercase');
39
+ await expect(rule.validate('sÄnity', context)).resolves.toMatchSnapshot('lowercase: locale characters');
40
+ await expect(rule.validate('sanity', context)).resolves.toMatchSnapshot('lowercase: valid');
41
+ });
42
+ test('regex constraint', async () => {
43
+ const rule = Rule.string().regex(/^[A-Z][a-z]+$/);
44
+ await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot('regex: non-match');
45
+ await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('regex: match');
46
+ });
47
+ test('regex constraint (inverted)', async () => {
48
+ const rule = Rule.string().regex(/^[A-Z][a-z]+$/, { invert: true });
49
+ await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot('regex: inverted non-match');
50
+ await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('regex: inverted match');
51
+ });
52
+ test('regex constraint (custom pattern name)', async () => {
53
+ const rule = Rule.string().regex(/^[A-Z][a-z]+$/, 'PascalCase');
54
+ await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot('regex: non-match w/ custom pattern name');
55
+ await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('regex: match w/ custom pattern name');
56
+ });
57
+ test('regex constraint (custom pattern name, as options)', async () => {
58
+ const rule = Rule.string().regex(/^[A-Z][a-z]+$/, { name: 'PascalCase' });
59
+ await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot('regex: non-match w/ custom pattern name (opt)');
60
+ await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('regex: match w/ custom pattern name (opt)');
61
+ });
62
+ test('uri constraint', async () => {
63
+ const rule = Rule.string().uri();
64
+ await expect(rule.validate('SANITY', context)).resolves.toMatchSnapshot('uri: non-match');
65
+ await expect(rule.validate('https://sanity.io/', context)).resolves.toHaveLength(0);
66
+ });
67
+ test('uri constraint (with unicode chars)', async () => {
68
+ const rule = Rule.string().uri();
69
+ await expect(rule.validate('Blåbærsyltetøy', context)).resolves.toMatchSnapshot('uri: non-match');
70
+ await expect(rule.validate('https://en.wikipedia.org/wiki/San_&_Søn', context)).resolves.toHaveLength(0);
71
+ await expect(rule.validate('https://zh.wikipedia.org/wiki/心形符號', context)).resolves.toHaveLength(0);
72
+ await expect(rule.validate('https://ru.wikipedia.org/wiki/Зонтичные', context)).resolves.toHaveLength(0);
73
+ await expect(rule.validate('https://påtapp.no/oslo', context)).resolves.toHaveLength(0);
74
+ });
75
+ test('uri constraint (invalid protocol)', async () => {
76
+ const rule = Rule.string().uri({ scheme: ['http', 'ftp'] });
77
+ await expect(rule.validate('https://sanity.io/', context)).resolves.toMatchSnapshot('uri: protocol non-match');
78
+ await expect(rule.validate('ftp://code.sanity.io/', context)).resolves.toHaveLength(0);
79
+ });
80
+ test('uri constraint (credentials)', async () => {
81
+ let rule = Rule.string().uri({ allowCredentials: true });
82
+ await expect(rule.validate('http://foo:bar@sanity.io/', context)).resolves.toHaveLength(0);
83
+ await expect(rule.validate('http://sanity.io/', context)).resolves.toHaveLength(0);
84
+ rule = Rule.string().uri({ allowCredentials: false });
85
+ await expect(rule.validate('http://sanity.io/', context)).resolves.toHaveLength(0);
86
+ await expect(rule.validate('http://foo:bar@sanity.io/', context)).resolves.toMatchSnapshot('uri: credentials specified but not allowed');
87
+ await expect(rule.validate('http://espen@sanity.io/', context)).resolves.toMatchSnapshot('uri: username specified but not allowed');
88
+ });
89
+ test('custom rule with string', async () => {
90
+ const rule = Rule.string().custom((val) => val.split('').reverse().join('') === val ? true : 'Must be a palindrome!');
91
+ await expect(rule.validate('hei', context)).resolves.toMatchSnapshot('not a palindrome');
92
+ await expect(rule.validate('madam', context)).resolves.toHaveLength(0);
93
+ });
94
+ test('custom async rule with string', async () => {
95
+ const rule = Rule.string().custom((val) => new Promise((resolve) => setTimeout(() => resolve(val.split('').reverse().join('') === val ? true : 'Must be a palindrome!'), 50)));
96
+ await expect(rule.validate('hei', context)).resolves.toMatchSnapshot('not a palindrome');
97
+ await expect(rule.validate('madam', context)).resolves.toHaveLength(0);
98
+ });
99
+ });
100
+ //# sourceMappingURL=strings.test.js.map