@docusaurus/utils-validation 2.0.0-beta.fc64c12e4 → 2.0.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/Joi.d.ts +1 -0
- package/lib/Joi.d.ts.map +1 -0
- package/lib/Joi.js +1 -0
- package/lib/Joi.js.map +1 -0
- package/lib/JoiFrontMatter.d.ts +18 -0
- package/lib/JoiFrontMatter.d.ts.map +1 -0
- package/lib/JoiFrontMatter.js +33 -0
- package/lib/JoiFrontMatter.js.map +1 -0
- package/lib/index.d.ts +4 -2
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +19 -5
- package/lib/index.js.map +1 -0
- package/lib/validationSchemas.d.ts +7 -1
- package/lib/validationSchemas.d.ts.map +1 -0
- package/lib/validationSchemas.js +67 -22
- package/lib/validationSchemas.js.map +1 -0
- package/lib/validationUtils.d.ts +19 -6
- package/lib/validationUtils.d.ts.map +1 -0
- package/lib/validationUtils.js +32 -63
- package/lib/validationUtils.js.map +1 -0
- package/package.json +8 -7
- package/src/JoiFrontMatter.ts +31 -0
- package/src/index.ts +17 -2
- package/src/validationSchemas.ts +82 -18
- package/src/validationUtils.ts +30 -78
- package/lib/.tsbuildinfo +0 -1
- package/src/__tests__/__snapshots__/validationSchemas.test.ts.snap +0 -63
- package/src/__tests__/validationSchemas.test.ts +0 -131
- package/src/__tests__/validationUtils.test.ts +0 -64
- package/tsconfig.json +0 -9
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import Joi from '../Joi';
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
AdmonitionsSchema,
|
|
12
|
-
RehypePluginsSchema,
|
|
13
|
-
RemarkPluginsSchema,
|
|
14
|
-
PluginIdSchema,
|
|
15
|
-
URISchema,
|
|
16
|
-
} from '../validationSchemas';
|
|
17
|
-
|
|
18
|
-
function createTestHelpers({
|
|
19
|
-
schema,
|
|
20
|
-
defaultValue,
|
|
21
|
-
}: {
|
|
22
|
-
schema: Joi.Schema;
|
|
23
|
-
defaultValue?: unknown;
|
|
24
|
-
}) {
|
|
25
|
-
function testOK(value: unknown) {
|
|
26
|
-
expect(Joi.attempt(value, schema)).toEqual(value ?? defaultValue);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function testFail(value: unknown) {
|
|
30
|
-
expect(() => Joi.attempt(value, schema)).toThrowErrorMatchingSnapshot(
|
|
31
|
-
`for value=${JSON.stringify(value)}`,
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return {testOK, testFail};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function testMarkdownPluginSchemas(schema: Joi.Schema) {
|
|
39
|
-
const {testOK, testFail} = createTestHelpers({
|
|
40
|
-
schema,
|
|
41
|
-
defaultValue: [],
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
testOK(undefined);
|
|
45
|
-
testOK([function () {}]);
|
|
46
|
-
testOK([[function () {}, {attr: 'val'}]]);
|
|
47
|
-
testOK([
|
|
48
|
-
[function () {}, {attr: 'val'}],
|
|
49
|
-
function () {},
|
|
50
|
-
[function () {}, {attr: 'val'}],
|
|
51
|
-
]);
|
|
52
|
-
|
|
53
|
-
testFail(null);
|
|
54
|
-
testFail(false);
|
|
55
|
-
testFail(3);
|
|
56
|
-
testFail([null]);
|
|
57
|
-
testFail([false]);
|
|
58
|
-
testFail([3]);
|
|
59
|
-
testFail([[]]);
|
|
60
|
-
testFail([[function () {}, undefined]]);
|
|
61
|
-
testFail([[function () {}, true]]);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
describe('validation schemas', () => {
|
|
65
|
-
test('PluginIdSchema', () => {
|
|
66
|
-
const {testOK, testFail} = createTestHelpers({
|
|
67
|
-
schema: PluginIdSchema,
|
|
68
|
-
defaultValue: 'default',
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
testOK(undefined);
|
|
72
|
-
testOK('docs');
|
|
73
|
-
testOK('default');
|
|
74
|
-
testOK('plugin-id_with-simple-special-chars');
|
|
75
|
-
|
|
76
|
-
testFail('/docs');
|
|
77
|
-
testFail('docs/');
|
|
78
|
-
testFail('do/cs');
|
|
79
|
-
testFail('do cs');
|
|
80
|
-
testFail(null);
|
|
81
|
-
testFail(3);
|
|
82
|
-
testFail(true);
|
|
83
|
-
testFail([]);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test('AdmonitionsSchema', () => {
|
|
87
|
-
const {testOK, testFail} = createTestHelpers({
|
|
88
|
-
schema: AdmonitionsSchema,
|
|
89
|
-
defaultValue: {},
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
testOK(undefined);
|
|
93
|
-
testOK({});
|
|
94
|
-
testOK({attr: 'val'});
|
|
95
|
-
|
|
96
|
-
testFail(null);
|
|
97
|
-
testFail(3);
|
|
98
|
-
testFail(true);
|
|
99
|
-
testFail([]);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test('RemarkPluginsSchema', () => {
|
|
103
|
-
testMarkdownPluginSchemas(RemarkPluginsSchema);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test('RehypePluginsSchema', () => {
|
|
107
|
-
testMarkdownPluginSchemas(RehypePluginsSchema);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
test('URISchema', () => {
|
|
111
|
-
const {testFail, testOK} = createTestHelpers({schema: URISchema});
|
|
112
|
-
|
|
113
|
-
const validURL = 'https://docusaurus.io';
|
|
114
|
-
const doubleHash = 'https://docusaurus.io#github#/:';
|
|
115
|
-
const invalidURL = 'spaces are invalid in a URL';
|
|
116
|
-
const relativeURL = 'relativeURL';
|
|
117
|
-
const relativeURLWithParent = '../relativeURLWithParent';
|
|
118
|
-
const urlFromIssue = 'https://riot.im/app/#/room/#ligo-public:matrix.org';
|
|
119
|
-
testOK(validURL);
|
|
120
|
-
testOK(doubleHash);
|
|
121
|
-
testFail(invalidURL);
|
|
122
|
-
testOK(relativeURL);
|
|
123
|
-
testOK(relativeURLWithParent);
|
|
124
|
-
testOK(urlFromIssue);
|
|
125
|
-
|
|
126
|
-
const protocolRelativeUrl1 = '//docusaurus.io/path';
|
|
127
|
-
const protocolRelativeUrl2 = '//docusaurus.io/docs/doc1#hash';
|
|
128
|
-
testOK(protocolRelativeUrl1);
|
|
129
|
-
testOK(protocolRelativeUrl2);
|
|
130
|
-
});
|
|
131
|
-
});
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import Joi from '../Joi';
|
|
9
|
-
import {JoiFrontMatter, validateFrontMatter} from '../validationUtils';
|
|
10
|
-
|
|
11
|
-
describe('validateFrontMatter', () => {
|
|
12
|
-
test('should accept good values', () => {
|
|
13
|
-
const schema = Joi.object<{test: string}>({
|
|
14
|
-
test: Joi.string(),
|
|
15
|
-
});
|
|
16
|
-
const frontMatter = {
|
|
17
|
-
test: 'hello',
|
|
18
|
-
};
|
|
19
|
-
expect(validateFrontMatter(frontMatter, schema)).toEqual(frontMatter);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('should reject bad values', () => {
|
|
23
|
-
const consoleError = jest.spyOn(console, 'error').mockImplementation();
|
|
24
|
-
const schema = Joi.object<{test: string}>({
|
|
25
|
-
test: Joi.string(),
|
|
26
|
-
});
|
|
27
|
-
const frontMatter = {
|
|
28
|
-
test: true,
|
|
29
|
-
};
|
|
30
|
-
expect(() =>
|
|
31
|
-
validateFrontMatter(frontMatter, schema),
|
|
32
|
-
).toThrowErrorMatchingInlineSnapshot(`"\\"test\\" must be a string"`);
|
|
33
|
-
expect(consoleError).toHaveBeenCalledWith(
|
|
34
|
-
expect.stringContaining('The following frontmatter'),
|
|
35
|
-
);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// Fix Yaml trying to convert strings to numbers automatically
|
|
39
|
-
// We only want to deal with a single type in the final frontmatter (not string | number)
|
|
40
|
-
test('should convert number values to string when string schema', () => {
|
|
41
|
-
const schema = Joi.object<{test: string}>({
|
|
42
|
-
test: JoiFrontMatter.string(),
|
|
43
|
-
});
|
|
44
|
-
const frontMatter = {
|
|
45
|
-
test: 42,
|
|
46
|
-
};
|
|
47
|
-
expect(validateFrontMatter(frontMatter, schema)).toEqual({test: '42'});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
// Helps to fix Yaml trying to convert strings to dates automatically
|
|
51
|
-
// We only want to deal with a single type in the final frontmatter (not string | Date)
|
|
52
|
-
test('should convert date values when string schema', () => {
|
|
53
|
-
const schema = Joi.object<{test: string}>({
|
|
54
|
-
test: JoiFrontMatter.string(),
|
|
55
|
-
});
|
|
56
|
-
const date = new Date();
|
|
57
|
-
const frontMatter = {
|
|
58
|
-
test: date,
|
|
59
|
-
};
|
|
60
|
-
expect(validateFrontMatter(frontMatter, schema)).toEqual({
|
|
61
|
-
test: date.toString(),
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
});
|