@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,88 +1,70 @@
|
|
|
1
|
-
import {Rule} from '../src'
|
|
2
|
-
const context = {client: {}}
|
|
1
|
+
import { Rule } from '../src';
|
|
2
|
+
const context = { client: {} };
|
|
3
3
|
describe('child rules', () => {
|
|
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
|
-
await expect(rule.validate('Red', context)).resolves.toMatchSnapshot(
|
|
73
|
-
'either() rules - all match'
|
|
74
|
-
)
|
|
75
|
-
await expect(rule.validate('nope', context)).resolves.toMatchSnapshot(
|
|
76
|
-
'either() rules - no matches'
|
|
77
|
-
)
|
|
78
|
-
})
|
|
79
|
-
test('either() rules - all fail, custom, common error', async () => {
|
|
80
|
-
const rule = Rule.string()
|
|
81
|
-
.either([Rule.string().regex(/^[A-Z]/), Rule.string().regex(/^i[A-Z]/)])
|
|
82
|
-
.error("Needs to start with a capital letter, unless it's an iProduct")
|
|
83
|
-
await expect(rule.validate('mopatis!', context)).resolves.toMatchSnapshot(
|
|
84
|
-
'either() rules - all fail, common error'
|
|
85
|
-
)
|
|
86
|
-
})
|
|
87
|
-
})
|
|
88
|
-
//# sourceMappingURL=children.test.js.map
|
|
4
|
+
// --- ALL ---
|
|
5
|
+
test('all() rules - single failure', async () => {
|
|
6
|
+
const rule = Rule.string().all([
|
|
7
|
+
Rule.string()
|
|
8
|
+
.regex(/^[A-Z]/)
|
|
9
|
+
.error('Must start with an uppercase character'),
|
|
10
|
+
Rule.string()
|
|
11
|
+
.regex(/[a-z]+/)
|
|
12
|
+
.error('Must follow with lowercase characters'),
|
|
13
|
+
]);
|
|
14
|
+
await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('all() rules - match');
|
|
15
|
+
await expect(rule.validate('moop', context)).resolves.toMatchSnapshot('all() rules - single failure, custom message');
|
|
16
|
+
});
|
|
17
|
+
test('all() rules - multiple failures', async () => {
|
|
18
|
+
const rule = Rule.string().all([
|
|
19
|
+
Rule.string()
|
|
20
|
+
.regex(/^[A-Z]/)
|
|
21
|
+
.error('Must start with an uppercase character'),
|
|
22
|
+
Rule.string().min(5).error('5 chars or more'),
|
|
23
|
+
Rule.string()
|
|
24
|
+
.regex(/[a-z]+/)
|
|
25
|
+
.error('Must follow with lowercase characters'),
|
|
26
|
+
]);
|
|
27
|
+
await expect(rule.validate('Sanity', context)).resolves.toMatchSnapshot('all() rules - match');
|
|
28
|
+
await expect(rule.validate('moop', context)).resolves.toMatchSnapshot('all() rules - multiple failures, custom messages');
|
|
29
|
+
});
|
|
30
|
+
test('all() rules - single failure, custom, common error', async () => {
|
|
31
|
+
const rule = Rule.string()
|
|
32
|
+
.all([Rule.string().regex(/^[A-Z]/), Rule.string().regex(/[a-z]+/)])
|
|
33
|
+
.error('Needs to start with a capital letter and then follow with lowercase characters');
|
|
34
|
+
await expect(rule.validate('moop', context)).resolves.toMatchSnapshot('all() rules - single failure, common error');
|
|
35
|
+
});
|
|
36
|
+
test('all() rules - single failure, custom, common error', async () => {
|
|
37
|
+
const rule = Rule.string()
|
|
38
|
+
.all([Rule.string().regex(/^[A-Z]/), Rule.string().min(5), Rule.string().regex(/[a-z]+/)])
|
|
39
|
+
.error('Needs to be a capital letter followed by at least 4 lowercase characters');
|
|
40
|
+
await expect(rule.validate('moop', context)).resolves.toMatchSnapshot('all() rules - multiple failures, common error');
|
|
41
|
+
});
|
|
42
|
+
// --- EITHER ---
|
|
43
|
+
test('either() rules - single failure', async () => {
|
|
44
|
+
const rule = Rule.string().either([
|
|
45
|
+
Rule.string()
|
|
46
|
+
.regex(/^rgb(\d+,\s*\d+,\s*\d+)$/)
|
|
47
|
+
.error('Must be rgb(num, num, num) format'),
|
|
48
|
+
Rule.string()
|
|
49
|
+
.regex(/^#([a-f0-9]{3}|[a-f0-9]{6})$/)
|
|
50
|
+
.error('Must be hex color with #-prefix'),
|
|
51
|
+
]);
|
|
52
|
+
await expect(rule.validate('rgb(16, 22, 133)', context)).resolves.toMatchSnapshot('either() rules - match');
|
|
53
|
+
await expect(rule.validate('#bf', context)).resolves.toMatchSnapshot('either() rules - single failure, custom message');
|
|
54
|
+
});
|
|
55
|
+
test('either() rules - all matches', async () => {
|
|
56
|
+
const rule = Rule.string().either([
|
|
57
|
+
Rule.string().regex(/^R/).error('Must start with a capital R'),
|
|
58
|
+
Rule.string().regex(/ed$/).error('Must end with "ed"'),
|
|
59
|
+
]);
|
|
60
|
+
await expect(rule.validate('Red', context)).resolves.toMatchSnapshot('either() rules - all match');
|
|
61
|
+
await expect(rule.validate('nope', context)).resolves.toMatchSnapshot('either() rules - no matches');
|
|
62
|
+
});
|
|
63
|
+
test('either() rules - all fail, custom, common error', async () => {
|
|
64
|
+
const rule = Rule.string()
|
|
65
|
+
.either([Rule.string().regex(/^[A-Z]/), Rule.string().regex(/^i[A-Z]/)])
|
|
66
|
+
.error("Needs to start with a capital letter, unless it's an iProduct");
|
|
67
|
+
await expect(rule.validate('mopatis!', context)).resolves.toMatchSnapshot('either() rules - all fail, common error');
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
//# sourceMappingURL=children.test.js.map
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import {Schema} from '@sanity/types'
|
|
2
|
-
export declare function createSchema(schemaDef: {
|
|
3
|
-
|
|
1
|
+
import { Schema } from '@sanity/types';
|
|
2
|
+
export declare function createSchema(schemaDef: {
|
|
3
|
+
name?: string;
|
|
4
|
+
types: any[];
|
|
5
|
+
}): Schema;
|
|
6
|
+
//# sourceMappingURL=createSchema.d.ts.map
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import SchemaBuilder from '@sanity/schema'
|
|
2
|
-
import {validateSchema, groupProblems} from '@sanity/schema/_internal'
|
|
1
|
+
import SchemaBuilder from '@sanity/schema';
|
|
2
|
+
import { validateSchema, groupProblems } from '@sanity/schema/_internal';
|
|
3
3
|
export function createSchema(schemaDef) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
4
|
+
const validated = validateSchema(schemaDef.types).getTypes();
|
|
5
|
+
const validation = groupProblems(validated);
|
|
6
|
+
const types = [...schemaDef.types];
|
|
7
|
+
// Add builtin types
|
|
8
|
+
types.push({ type: 'object', name: 'slug' });
|
|
9
|
+
// if (!types.find((t) => t.name === 'slug')) {
|
|
10
|
+
// types.push()
|
|
11
|
+
// }
|
|
12
|
+
const schema = SchemaBuilder.compile({
|
|
13
|
+
name: schemaDef.name || 'default',
|
|
14
|
+
types,
|
|
15
|
+
});
|
|
16
|
+
schema._validation = validation;
|
|
17
|
+
return schema;
|
|
18
18
|
}
|
|
19
|
-
//# sourceMappingURL=createSchema.js.map
|
|
19
|
+
//# sourceMappingURL=createSchema.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {}
|
|
2
|
-
//# sourceMappingURL=generics.test.d.ts.map
|
|
1
|
+
export {};
|
|
2
|
+
//# sourceMappingURL=generics.test.d.ts.map
|
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import {Rule} from '../src'
|
|
2
|
-
const context = {client: {}}
|
|
1
|
+
import { Rule } from '../src';
|
|
2
|
+
const context = { client: {} };
|
|
3
3
|
describe('generics', () => {
|
|
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
|
-
//# sourceMappingURL=generics.test.js.map
|
|
4
|
+
test('should be able to construct an empty rule', () => {
|
|
5
|
+
expect(() => new Rule()).not.toThrow(Error);
|
|
6
|
+
expect(new Rule()).toBeInstanceOf(Rule);
|
|
7
|
+
});
|
|
8
|
+
test('should be able to construct a new typed rule', () => {
|
|
9
|
+
expect(Rule.string()).toBeInstanceOf(Rule);
|
|
10
|
+
});
|
|
11
|
+
test('clones rule when changing generics', () => {
|
|
12
|
+
const baseRule = new Rule();
|
|
13
|
+
const typed = baseRule.type('string');
|
|
14
|
+
expect(baseRule).not.toBe(typed);
|
|
15
|
+
});
|
|
16
|
+
test('clones rule when changing type-specific rules', () => {
|
|
17
|
+
const baseRule = Rule.string();
|
|
18
|
+
const specific = baseRule.min(5);
|
|
19
|
+
expect(baseRule).not.toBe(specific);
|
|
20
|
+
});
|
|
21
|
+
test('returns arrays of errors/warnings by default', async () => {
|
|
22
|
+
await expect(Rule.string().validate(123, context)).resolves.toMatchSnapshot();
|
|
23
|
+
});
|
|
24
|
+
test('can demote errors to warnings', async () => {
|
|
25
|
+
const result = await Rule.string().warning().validate(123, context);
|
|
26
|
+
expect(result).toMatchSnapshot();
|
|
27
|
+
});
|
|
28
|
+
test('can customize error messages', async () => {
|
|
29
|
+
const result = await Rule.string().error('Dude it needs to be a string').validate(123, context);
|
|
30
|
+
expect(result).toMatchSnapshot();
|
|
31
|
+
});
|
|
32
|
+
test('can customize warning messages', async () => {
|
|
33
|
+
const result = await Rule.string()
|
|
34
|
+
.warning('Dude it should probably be a string')
|
|
35
|
+
.validate(123, context);
|
|
36
|
+
expect(result).toMatchSnapshot();
|
|
37
|
+
});
|
|
38
|
+
test('can customize info messages', async () => {
|
|
39
|
+
const result = await Rule.string()
|
|
40
|
+
.info('Dude it should probably be a string')
|
|
41
|
+
.validate(123, context);
|
|
42
|
+
expect(result).toMatchSnapshot();
|
|
43
|
+
});
|
|
44
|
+
test('can merge rules', async () => {
|
|
45
|
+
const rule = new Rule().required();
|
|
46
|
+
const stringRule = Rule.string().min(5);
|
|
47
|
+
const newRule = rule.merge(stringRule);
|
|
48
|
+
expect(rule).not.toBe(stringRule);
|
|
49
|
+
expect(newRule).not.toBe(stringRule);
|
|
50
|
+
expect(rule).not.toBe(newRule);
|
|
51
|
+
const result = await newRule.validate('Hei', context);
|
|
52
|
+
expect(result).toMatchSnapshot();
|
|
53
|
+
});
|
|
54
|
+
test('can merge rules', () => {
|
|
55
|
+
expect(new Rule().required().isRequired()).toBe(true);
|
|
56
|
+
expect(new Rule().isRequired()).toBe(false);
|
|
57
|
+
expect(new Rule().optional().isRequired()).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
//# sourceMappingURL=generics.test.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {}
|
|
2
|
-
//# sourceMappingURL=infer.test.d.ts.map
|
|
1
|
+
export {};
|
|
2
|
+
//# sourceMappingURL=infer.test.d.ts.map
|