Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): add themeConfig validation to algolia theme #3133

Merged
merged 3 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/docusaurus-theme-search-algolia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"license": "MIT",
"dependencies": {
"@docsearch/react": "^1.0.0-alpha.24",
"@hapi/joi": "^17.1.1",
"algoliasearch": "^4.0.0",
"algoliasearch-helper": "^3.1.1",
"clsx": "^1.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const {validateThemeConfig, DEFAULT_CONFIG} = require('../validateThemeConfig');

function testValidateThemeConfig(themeConfig) {
function validate(schema, cfg) {
const {value, error} = schema.validate(cfg, {
convert: false,
});
if (error) {
throw error;
}
return value;
}

return validateThemeConfig({themeConfig, validate});
}

describe('validateThemeConfig', () => {
test('minimal config', () => {
const algolia = {
indexName: 'index',
apiKey: 'apiKey',
};
expect(testValidateThemeConfig({algolia})).toEqual({
algolia: {
...DEFAULT_CONFIG,
...algolia,
},
});
});

test('unknown attributes', () => {
const algolia = {
indexName: 'index',
apiKey: 'apiKey',
unknownKey: 'unknownKey',
};
expect(testValidateThemeConfig({algolia})).toEqual({
algolia: {
...DEFAULT_CONFIG,
...algolia,
},
});
});

test('undefined config', () => {
const algolia = undefined;
expect(() =>
testValidateThemeConfig({algolia}),
).toThrowErrorMatchingInlineSnapshot(
`"\\"themeConfig.algolia\\" is required"`,
);
});

test('undefined config 2', () => {
expect(() =>
testValidateThemeConfig({}),
).toThrowErrorMatchingInlineSnapshot(
`"\\"themeConfig.algolia\\" is required"`,
);
});

test('empty config', () => {
const algolia = {};
expect(() =>
testValidateThemeConfig({algolia}),
).toThrowErrorMatchingInlineSnapshot(`"\\"algolia.apiKey\\" is required"`);
});

test('missing indexName config', () => {
const algolia = {apiKey: 'apiKey'};
expect(() =>
testValidateThemeConfig({algolia}),
).toThrowErrorMatchingInlineSnapshot(
`"\\"algolia.indexName\\" is required"`,
);
});

test('missing apiKey config', () => {
const algolia = {indexName: 'indexName'};
expect(() =>
testValidateThemeConfig({algolia}),
).toThrowErrorMatchingInlineSnapshot(`"\\"algolia.apiKey\\" is required"`);
});
});
9 changes: 7 additions & 2 deletions packages/docusaurus-theme-search-algolia/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ const fs = require('fs');
const eta = require('eta');
const {normalizeUrl} = require('@docusaurus/utils');
const openSearchTemplate = require('./templates/opensearch');
const {validateThemeConfig} = require('./validateThemeConfig');

const OPEN_SEARCH_FILENAME = 'opensearch.xml';

module.exports = function (context) {
function theme(context) {
const {
baseUrl,
siteConfig: {title, url, favicon},
Expand Down Expand Up @@ -70,4 +71,8 @@ module.exports = function (context) {
};
},
};
};
}

module.exports = theme;

theme.validateThemeConfig = validateThemeConfig;
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,7 @@ function DocSearch(props) {
}

function SearchBar() {
const {siteConfig = {}} = useDocusaurusContext();

if (!siteConfig.themeConfig.algolia) {
// eslint-disable-next-line no-console
console.warn(`DocSearch requires an \`algolia\` field in your \`themeConfig\`.

See: https://v2.docusaurus.io/docs/search/#using-algolia-docsearch`);

return null;
}

const {siteConfig} = useDocusaurusContext();
return <DocSearch {...siteConfig.themeConfig.algolia} />;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const Joi = require('@hapi/joi');

const DEFAULT_CONFIG = {
// By default, all Docusaurus sites are using the same AppId
// This has been designed on purpose with Algolia.
appId: 'BH4D9OD16A',
};
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;

const Schema = Joi.object({
algolia: Joi.object({
appId: Joi.string().default(DEFAULT_CONFIG.appId),
apiKey: Joi.string().required(),
indexName: Joi.string().required(),
})
.label('themeConfig.algolia')
.required()
.unknown(), // DocSearch 3 is still alpha: don't validate the rest for now
});
exports.Schema = Schema;

exports.validateThemeConfig = function validateThemeConfig({
validate,
themeConfig,
}) {
return validate(Schema, themeConfig);
};