@docusaurus/utils-validation 0.0.0-4759 → 0.0.0-4760
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/JoiFrontMatter.d.ts +2 -1
- package/lib/JoiFrontMatter.d.ts.map +1 -1
- package/lib/JoiFrontMatter.js +2 -1
- package/lib/JoiFrontMatter.js.map +1 -1
- package/lib/validationUtils.d.ts +13 -0
- package/lib/validationUtils.d.ts.map +1 -1
- package/lib/validationUtils.js +18 -4
- package/lib/validationUtils.js.map +1 -1
- package/package.json +5 -4
- package/src/JoiFrontMatter.ts +3 -1
- package/src/validationUtils.ts +18 -4
package/lib/JoiFrontMatter.d.ts
CHANGED
|
@@ -6,10 +6,11 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import Joi from './Joi';
|
|
8
8
|
/**
|
|
9
|
-
* Enhance the default Joi.string() type so that it can convert number to
|
|
9
|
+
* Enhance the default `Joi.string()` type so that it can convert number to
|
|
10
10
|
* strings. If user use front matter "tag: 2021", we shouldn't need to ask her
|
|
11
11
|
* to write "tag: '2021'". Also yaml tries to convert patterns like "2019-01-01"
|
|
12
12
|
* to dates automatically.
|
|
13
|
+
*
|
|
13
14
|
* @see https://github.com/facebook/docusaurus/issues/4642
|
|
14
15
|
* @see https://github.com/sideway/joi/issues/1442#issuecomment-823997884
|
|
15
16
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JoiFrontMatter.d.ts","sourceRoot":"","sources":["../src/JoiFrontMatter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,GAAG,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"JoiFrontMatter.d.ts","sourceRoot":"","sources":["../src/JoiFrontMatter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,GAAG,MAAM,OAAO,CAAC;AAcxB;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,EAAE,OAAO,GAAsC,CAAC"}
|
package/lib/JoiFrontMatter.js
CHANGED
|
@@ -21,10 +21,11 @@ const JoiFrontMatterString = {
|
|
|
21
21
|
},
|
|
22
22
|
};
|
|
23
23
|
/**
|
|
24
|
-
* Enhance the default Joi.string() type so that it can convert number to
|
|
24
|
+
* Enhance the default `Joi.string()` type so that it can convert number to
|
|
25
25
|
* strings. If user use front matter "tag: 2021", we shouldn't need to ask her
|
|
26
26
|
* to write "tag: '2021'". Also yaml tries to convert patterns like "2019-01-01"
|
|
27
27
|
* to dates automatically.
|
|
28
|
+
*
|
|
28
29
|
* @see https://github.com/facebook/docusaurus/issues/4642
|
|
29
30
|
* @see https://github.com/sideway/joi/issues/1442#issuecomment-823997884
|
|
30
31
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JoiFrontMatter.js","sourceRoot":"","sources":["../src/JoiFrontMatter.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,wDAAwB;AAExB,MAAM,oBAAoB,GAAkB;IAC1C,IAAI,EAAE,QAAQ;IACd,IAAI,EAAE,aAAG,CAAC,MAAM,EAAE;IAClB,2EAA2E;IAC3E,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;QACjB,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,YAAY,IAAI,EAAE;YACtD,OAAO,EAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE,EAAC,CAAC;SAClC;QACD,OAAO,EAAC,KAAK,EAAC,CAAC;IACjB,CAAC;CACF,CAAC;
|
|
1
|
+
{"version":3,"file":"JoiFrontMatter.js","sourceRoot":"","sources":["../src/JoiFrontMatter.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,wDAAwB;AAExB,MAAM,oBAAoB,GAAkB;IAC1C,IAAI,EAAE,QAAQ;IACd,IAAI,EAAE,aAAG,CAAC,MAAM,EAAE;IAClB,2EAA2E;IAC3E,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;QACjB,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,YAAY,IAAI,EAAE;YACtD,OAAO,EAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE,EAAC,CAAC;SAClC;QACD,OAAO,EAAC,KAAK,EAAC,CAAC;IACjB,CAAC;CACF,CAAC;AAEF;;;;;;;;GAQG;AACU,QAAA,cAAc,GAAe,aAAG,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC"}
|
package/lib/validationUtils.d.ts
CHANGED
|
@@ -5,10 +5,23 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
import type Joi from './Joi';
|
|
8
|
+
/** Print warnings returned from Joi validation. */
|
|
8
9
|
export declare function printWarning(warning?: Joi.ValidationError): void;
|
|
10
|
+
/**
|
|
11
|
+
* The callback that should be used to validate plugin options. Handles plugin
|
|
12
|
+
* IDs on a generic level: no matter what the schema declares, this callback
|
|
13
|
+
* would require a string ID or default to "default".
|
|
14
|
+
*/
|
|
9
15
|
export declare function normalizePluginOptions<T extends {
|
|
10
16
|
id?: string;
|
|
11
17
|
}>(schema: Joi.ObjectSchema<T>, options?: Partial<T>): T;
|
|
18
|
+
/**
|
|
19
|
+
* The callback that should be used to validate theme config. No matter what the
|
|
20
|
+
* schema declares, this callback would allow unknown attributes.
|
|
21
|
+
*/
|
|
12
22
|
export declare function normalizeThemeConfig<T>(schema: Joi.ObjectSchema<T>, themeConfig: Partial<T>): T;
|
|
23
|
+
/**
|
|
24
|
+
* Validate front matter with better error message
|
|
25
|
+
*/
|
|
13
26
|
export declare function validateFrontMatter<T>(frontMatter: Record<string, unknown>, schema: Joi.ObjectSchema<T>): T;
|
|
14
27
|
//# sourceMappingURL=validationUtils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validationUtils.d.ts","sourceRoot":"","sources":["../src/validationUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"validationUtils.d.ts","sourceRoot":"","sources":["../src/validationUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAK7B,mDAAmD;AACnD,wBAAgB,YAAY,CAAC,OAAO,CAAC,EAAE,GAAG,CAAC,eAAe,GAAG,IAAI,CAOhE;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,SAAS;IAAC,EAAE,CAAC,EAAE,MAAM,CAAA;CAAC,EAC5D,MAAM,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,EAE3B,OAAO,GAAE,OAAO,CAAC,CAAC,CAAM,GACvB,CAAC,CAiBH;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,MAAM,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,EAC3B,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC,GACtB,CAAC,CAgBH;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EACnC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpC,MAAM,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,GAC1B,CAAC,CAuBH"}
|
package/lib/validationUtils.js
CHANGED
|
@@ -9,7 +9,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
9
9
|
exports.validateFrontMatter = exports.normalizeThemeConfig = exports.normalizePluginOptions = exports.printWarning = void 0;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
11
|
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
12
|
+
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
|
|
12
13
|
const validationSchemas_1 = require("./validationSchemas");
|
|
14
|
+
/** Print warnings returned from Joi validation. */
|
|
13
15
|
function printWarning(warning) {
|
|
14
16
|
if (warning) {
|
|
15
17
|
const warningMessages = warning.details
|
|
@@ -19,8 +21,13 @@ function printWarning(warning) {
|
|
|
19
21
|
}
|
|
20
22
|
}
|
|
21
23
|
exports.printWarning = printWarning;
|
|
24
|
+
/**
|
|
25
|
+
* The callback that should be used to validate plugin options. Handles plugin
|
|
26
|
+
* IDs on a generic level: no matter what the schema declares, this callback
|
|
27
|
+
* would require a string ID or default to "default".
|
|
28
|
+
*/
|
|
22
29
|
function normalizePluginOptions(schema,
|
|
23
|
-
// This allows us to automatically normalize undefined to {id:
|
|
30
|
+
// This allows us to automatically normalize undefined to { id: "default" }
|
|
24
31
|
options = {}) {
|
|
25
32
|
// All plugins can be provided an "id" option (multi-instance support)
|
|
26
33
|
// we add schema validation automatically
|
|
@@ -37,6 +44,10 @@ options = {}) {
|
|
|
37
44
|
return value;
|
|
38
45
|
}
|
|
39
46
|
exports.normalizePluginOptions = normalizePluginOptions;
|
|
47
|
+
/**
|
|
48
|
+
* The callback that should be used to validate theme config. No matter what the
|
|
49
|
+
* schema declares, this callback would allow unknown attributes.
|
|
50
|
+
*/
|
|
40
51
|
function normalizeThemeConfig(schema, themeConfig) {
|
|
41
52
|
// A theme should only validate its "slice" of the full themeConfig,
|
|
42
53
|
// not the whole object, so we allow unknown attributes
|
|
@@ -52,6 +63,9 @@ function normalizeThemeConfig(schema, themeConfig) {
|
|
|
52
63
|
return value;
|
|
53
64
|
}
|
|
54
65
|
exports.normalizeThemeConfig = normalizeThemeConfig;
|
|
66
|
+
/**
|
|
67
|
+
* Validate front matter with better error message
|
|
68
|
+
*/
|
|
55
69
|
function validateFrontMatter(frontMatter, schema) {
|
|
56
70
|
const { value, error, warning } = schema.validate(frontMatter, {
|
|
57
71
|
convert: true,
|
|
@@ -60,12 +74,12 @@ function validateFrontMatter(frontMatter, schema) {
|
|
|
60
74
|
});
|
|
61
75
|
printWarning(warning);
|
|
62
76
|
if (error) {
|
|
63
|
-
const frontMatterString = JSON.stringify(frontMatter, null, 2);
|
|
64
77
|
const errorDetails = error.details;
|
|
65
78
|
const invalidFields = errorDetails.map(({ path }) => path).join(', ');
|
|
66
79
|
logger_1.default.error `The following front matter:
|
|
67
|
-
|
|
68
|
-
|
|
80
|
+
---
|
|
81
|
+
${js_yaml_1.default.dump(frontMatter)}---
|
|
82
|
+
contains invalid values for field(s): code=${invalidFields}.
|
|
69
83
|
${errorDetails.map(({ message }) => message)}
|
|
70
84
|
`;
|
|
71
85
|
throw error;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validationUtils.js","sourceRoot":"","sources":["../src/validationUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAGH,wEAAwC;AACxC,2DAAmD;AAEnD,SAAgB,YAAY,CAAC,OAA6B;IACxD,IAAI,OAAO,EAAE;QACX,MAAM,eAAe,GAAG,OAAO,CAAC,OAAO;aACpC,GAAG,CAAC,CAAC,EAAC,OAAO,EAAC,EAAE,EAAE,CAAC,OAAO,CAAC;aAC3B,IAAI,CAAC,IAAI,CAAC,CAAC;QACd,gBAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;KAC9B;AACH,CAAC;AAPD,oCAOC;AAED,SAAgB,sBAAsB,CACpC,MAA2B;AAC3B,
|
|
1
|
+
{"version":3,"file":"validationUtils.js","sourceRoot":"","sources":["../src/validationUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAGH,wEAAwC;AACxC,8DAA2B;AAC3B,2DAAmD;AAEnD,mDAAmD;AACnD,SAAgB,YAAY,CAAC,OAA6B;IACxD,IAAI,OAAO,EAAE;QACX,MAAM,eAAe,GAAG,OAAO,CAAC,OAAO;aACpC,GAAG,CAAC,CAAC,EAAC,OAAO,EAAC,EAAE,EAAE,CAAC,OAAO,CAAC;aAC3B,IAAI,CAAC,IAAI,CAAC,CAAC;QACd,gBAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;KAC9B;AACH,CAAC;AAPD,oCAOC;AAED;;;;GAIG;AACH,SAAgB,sBAAsB,CACpC,MAA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C,MAAM,aAAa,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,EAAC,IAAI,EAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAEpE,gBAAM,CAAC,KAAK,CAAA;;EAEd,iBAAI,CAAC,IAAI,CAAC,WAAW,CAAC;6CACqB,aAAa;EACxD,YAAY,CAAC,GAAG,CAAC,CAAC,EAAC,OAAO,EAAC,EAAE,EAAE,CAAC,OAAO,CAAC;CACzC,CAAC;QACE,MAAM,KAAK,CAAC;KACb;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AA1BD,kDA0BC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/utils-validation",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-4760",
|
|
4
4
|
"description": "Node validation utility functions for Docusaurus packages.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -18,13 +18,14 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/logger": "0.0.0-
|
|
22
|
-
"@docusaurus/utils": "0.0.0-
|
|
21
|
+
"@docusaurus/logger": "0.0.0-4760",
|
|
22
|
+
"@docusaurus/utils": "0.0.0-4760",
|
|
23
23
|
"joi": "^17.6.0",
|
|
24
|
+
"js-yaml": "^4.1.0",
|
|
24
25
|
"tslib": "^2.3.1"
|
|
25
26
|
},
|
|
26
27
|
"engines": {
|
|
27
28
|
"node": ">=14"
|
|
28
29
|
},
|
|
29
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "ca58c5f867aec349fb405a2056d4bdb4d140a0b5"
|
|
30
31
|
}
|
package/src/JoiFrontMatter.ts
CHANGED
|
@@ -18,11 +18,13 @@ const JoiFrontMatterString: Joi.Extension = {
|
|
|
18
18
|
return {value};
|
|
19
19
|
},
|
|
20
20
|
};
|
|
21
|
+
|
|
21
22
|
/**
|
|
22
|
-
* Enhance the default Joi.string() type so that it can convert number to
|
|
23
|
+
* Enhance the default `Joi.string()` type so that it can convert number to
|
|
23
24
|
* strings. If user use front matter "tag: 2021", we shouldn't need to ask her
|
|
24
25
|
* to write "tag: '2021'". Also yaml tries to convert patterns like "2019-01-01"
|
|
25
26
|
* to dates automatically.
|
|
27
|
+
*
|
|
26
28
|
* @see https://github.com/facebook/docusaurus/issues/4642
|
|
27
29
|
* @see https://github.com/sideway/joi/issues/1442#issuecomment-823997884
|
|
28
30
|
*/
|
package/src/validationUtils.ts
CHANGED
|
@@ -7,8 +7,10 @@
|
|
|
7
7
|
|
|
8
8
|
import type Joi from './Joi';
|
|
9
9
|
import logger from '@docusaurus/logger';
|
|
10
|
+
import Yaml from 'js-yaml';
|
|
10
11
|
import {PluginIdSchema} from './validationSchemas';
|
|
11
12
|
|
|
13
|
+
/** Print warnings returned from Joi validation. */
|
|
12
14
|
export function printWarning(warning?: Joi.ValidationError): void {
|
|
13
15
|
if (warning) {
|
|
14
16
|
const warningMessages = warning.details
|
|
@@ -18,9 +20,14 @@ export function printWarning(warning?: Joi.ValidationError): void {
|
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
/**
|
|
24
|
+
* The callback that should be used to validate plugin options. Handles plugin
|
|
25
|
+
* IDs on a generic level: no matter what the schema declares, this callback
|
|
26
|
+
* would require a string ID or default to "default".
|
|
27
|
+
*/
|
|
21
28
|
export function normalizePluginOptions<T extends {id?: string}>(
|
|
22
29
|
schema: Joi.ObjectSchema<T>,
|
|
23
|
-
// This allows us to automatically normalize undefined to {id:
|
|
30
|
+
// This allows us to automatically normalize undefined to { id: "default" }
|
|
24
31
|
options: Partial<T> = {},
|
|
25
32
|
): T {
|
|
26
33
|
// All plugins can be provided an "id" option (multi-instance support)
|
|
@@ -41,6 +48,10 @@ export function normalizePluginOptions<T extends {id?: string}>(
|
|
|
41
48
|
return value;
|
|
42
49
|
}
|
|
43
50
|
|
|
51
|
+
/**
|
|
52
|
+
* The callback that should be used to validate theme config. No matter what the
|
|
53
|
+
* schema declares, this callback would allow unknown attributes.
|
|
54
|
+
*/
|
|
44
55
|
export function normalizeThemeConfig<T>(
|
|
45
56
|
schema: Joi.ObjectSchema<T>,
|
|
46
57
|
themeConfig: Partial<T>,
|
|
@@ -62,6 +73,9 @@ export function normalizeThemeConfig<T>(
|
|
|
62
73
|
return value;
|
|
63
74
|
}
|
|
64
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Validate front matter with better error message
|
|
78
|
+
*/
|
|
65
79
|
export function validateFrontMatter<T>(
|
|
66
80
|
frontMatter: Record<string, unknown>,
|
|
67
81
|
schema: Joi.ObjectSchema<T>,
|
|
@@ -75,13 +89,13 @@ export function validateFrontMatter<T>(
|
|
|
75
89
|
printWarning(warning);
|
|
76
90
|
|
|
77
91
|
if (error) {
|
|
78
|
-
const frontMatterString = JSON.stringify(frontMatter, null, 2);
|
|
79
92
|
const errorDetails = error.details;
|
|
80
93
|
const invalidFields = errorDetails.map(({path}) => path).join(', ');
|
|
81
94
|
|
|
82
95
|
logger.error`The following front matter:
|
|
83
|
-
|
|
84
|
-
|
|
96
|
+
---
|
|
97
|
+
${Yaml.dump(frontMatter)}---
|
|
98
|
+
contains invalid values for field(s): code=${invalidFields}.
|
|
85
99
|
${errorDetails.map(({message}) => message)}
|
|
86
100
|
`;
|
|
87
101
|
throw error;
|