@docusaurus/utils-validation 2.0.0-beta.15d451942 → 2.0.0-beta.16

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.
@@ -4,37 +4,20 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import Joi from './Joi';
8
- import chalk from 'chalk';
9
- import {PluginIdSchema} from './validationSchemas';
10
7
 
11
- // TODO temporary escape hatch for alpha-60: to be removed soon
12
- // Our validation schemas might be buggy at first
13
- // will permit users to bypass validation until we fix all validation errors
14
- // see for example: https://github.com/facebook/docusaurus/pull/3120
15
- // Undocumented on purpose, as we don't want users to keep using it over time
16
- // Maybe we'll make this escape hatch official some day, with a better api?
17
- export const isValidationDisabledEscapeHatch =
18
- process.env.DISABLE_DOCUSAURUS_VALIDATION === 'true';
8
+ import type Joi from './Joi';
9
+ import logger from '@docusaurus/logger';
10
+ import {PluginIdSchema} from './validationSchemas';
19
11
 
20
- if (isValidationDisabledEscapeHatch) {
21
- console.error(
22
- chalk.red(
23
- 'You should avoid using DISABLE_DOCUSAURUS_VALIDATION escape hatch, this will be removed',
24
- ),
25
- );
12
+ export function printWarning(warning?: Joi.ValidationError): void {
13
+ if (warning) {
14
+ const warningMessages = warning.details
15
+ .map(({message}) => message)
16
+ .join('\n');
17
+ logger.warn(warningMessages);
18
+ }
26
19
  }
27
20
 
28
- export const logValidationBugReportHint = (): void => {
29
- console.log(
30
- `\n${chalk.red('A validation error occured.')}${chalk.cyanBright(
31
- '\nThe validation system was added recently to Docusaurus as an attempt to avoid user configuration errors.' +
32
- '\nWe may have made some mistakes.' +
33
- '\nIf you think your configuration is valid and should keep working, please open a bug report.',
34
- )}\n`,
35
- );
36
- };
37
-
38
21
  export function normalizePluginOptions<T extends {id?: string}>(
39
22
  schema: Joi.ObjectSchema<T>,
40
23
  options: Partial<T>,
@@ -44,19 +27,17 @@ export function normalizePluginOptions<T extends {id?: string}>(
44
27
  const finalSchema = schema.append({
45
28
  id: PluginIdSchema,
46
29
  });
47
- const {error, value} = finalSchema.validate(options, {
30
+ const {error, warning, value} = finalSchema.validate(options, {
48
31
  convert: false,
49
32
  });
33
+
34
+ printWarning(warning);
35
+
50
36
  if (error) {
51
- logValidationBugReportHint();
52
- if (isValidationDisabledEscapeHatch) {
53
- console.error(error);
54
- return options as T;
55
- } else {
56
- throw error;
57
- }
37
+ throw error;
58
38
  }
59
- return value;
39
+
40
+ return value!; // TODO remove this ! in TS 4.6, see https://twitter.com/sebastienlorber/status/1481950042277793793
60
41
  }
61
42
 
62
43
  export function normalizeThemeConfig<T>(
@@ -68,40 +49,18 @@ export function normalizeThemeConfig<T>(
68
49
  // otherwise one theme would fail validating the data of another theme
69
50
  const finalSchema = schema.unknown();
70
51
 
71
- const {error, value} = finalSchema.validate(themeConfig, {
52
+ const {error, warning, value} = finalSchema.validate(themeConfig, {
72
53
  convert: false,
73
54
  });
74
55
 
56
+ printWarning(warning);
57
+
75
58
  if (error) {
76
- logValidationBugReportHint();
77
- if (isValidationDisabledEscapeHatch) {
78
- console.error(error);
79
- return themeConfig as T;
80
- } else {
81
- throw error;
82
- }
59
+ throw error;
83
60
  }
84
- return value;
61
+ return value!; // TODO remove this ! in TS 4.6
85
62
  }
86
63
 
87
- // Enhance the default Joi.string() type so that it can convert number to strings
88
- // If user use frontmatter "tag: 2021", we shouldn't need to ask the user to write "tag: '2021'"
89
- // Also yaml tries to convert patterns like "2019-01-01" to dates automatically
90
- // see https://github.com/facebook/docusaurus/issues/4642
91
- // see https://github.com/sideway/joi/issues/1442#issuecomment-823997884
92
- const JoiFrontMatterString: Joi.Extension = {
93
- type: 'string',
94
- base: Joi.string(),
95
- // Fix Yaml that tries to auto-convert many things to string out of the box
96
- prepare: (value) => {
97
- if (typeof value === 'number' || value instanceof Date) {
98
- return {value: value.toString()};
99
- }
100
- return {value};
101
- },
102
- };
103
- export const JoiFrontMatter: typeof Joi = Joi.extend(JoiFrontMatterString);
104
-
105
64
  export function validateFrontMatter<T>(
106
65
  frontMatter: Record<string, unknown>,
107
66
  schema: Joi.ObjectSchema<T>,
@@ -112,32 +71,20 @@ export function validateFrontMatter<T>(
112
71
  abortEarly: false,
113
72
  });
114
73
 
74
+ printWarning(warning);
75
+
115
76
  if (error) {
116
77
  const frontMatterString = JSON.stringify(frontMatter, null, 2);
117
78
  const errorDetails = error.details;
118
79
  const invalidFields = errorDetails.map(({path}) => path).join(', ');
119
- const errorMessages = errorDetails
120
- .map(({message}) => ` - ${message}`)
121
- .join('\n');
122
-
123
- logValidationBugReportHint();
124
80
 
125
- console.error(
126
- chalk.red(
127
- `The following FrontMatter:\n${chalk.yellow(
128
- frontMatterString,
129
- )}\ncontains invalid values for field(s): ${invalidFields}.\n${errorMessages}\n`,
130
- ),
131
- );
81
+ logger.error`The following front matter:
82
+ ${logger.yellow(frontMatterString)}
83
+ contains invalid values for field(s): ${logger.yellow(invalidFields)}.
84
+ ${errorDetails.map(({message}) => message)}
85
+ `;
132
86
  throw error;
133
87
  }
134
88
 
135
- if (warning) {
136
- const warningMessages = warning.details
137
- .map(({message}) => message)
138
- .join('\n');
139
- console.log(chalk.yellow(warningMessages));
140
- }
141
-
142
- return value;
89
+ return value!; // TODO remove this ! in TS 4.6
143
90
  }