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

chore(v2): use joi for config validation #2987

Merged
merged 7 commits into from
Jun 26, 2020

Conversation

anshulrgoyal
Copy link
Contributor

@anshulrgoyal anshulrgoyal commented Jun 24, 2020

Motivation

More robust validation for config. Validating more fields and types. It fixes #2978.

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

I have added tests for validation and update old ones

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jun 24, 2020
@anshulrgoyal anshulrgoyal changed the title use joi for validation chore(v2): use joi for config validation Jun 24, 2020
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Jun 24, 2020

Deploy preview for docusaurus-2 ready!

Built with commit 16f4ea0

https://deploy-preview-2987--docusaurus-2.netlify.app

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good

Would just factorize a bit the tests, and test for empty config

baseUrl: '/',
favicon: 'some.ico',
title: 'my site',
url: 'https://mysite.com',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about extracting this in a "baseConfig" variable?

Maybe it could be more convenient to have such method to make tests less "verbose"

const testConfig = (config) => Joi.attempt({...baseConfig,...config})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea , I will fix it.

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments

import {ConfigSchema, DEFAULT_CONFIG} from '../configValidation';

const testConfig = (config) =>
Joi.attempt({...DEFAULT_CONFIG, ...config}, ConfigSchema);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really what I meant :p

The idea is that you don't need to mention the 4 required attributes in every test to avoid unnecessary boilerplate.

There are many ways to do here, here's an idea:

const baseTestConfig = {
  baseUrl: "/",
  favicon: "some.ico",
  title: "my site",
  url: "https://mysite.com"
};

const testConfig = config =>
  Joi.attempt({...baseTestConfig,...config}, ConfigSchema);

const expectConfig = config =>     expect(config).toEqual({
      ...DEFAULT_CONFIG,
      ...baseTestConfig,
      ...config,
    });

  test('custom fields', () => {
    const value = testConfig({
      customFields: {
        author: 'anshul',
      },
    });
    expectConfig({
      customFields: {
        author: 'anshul',
      },
    });
}

}

return config;
return Joi.attempt(loadedConfig, ConfigSchema);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here it would be better to abortEarly: false

so you need to catch the potential validationError, extract all the errors, and rethrow as a well-formatted regular error (with line breaks etc...)

@anshulrgoyal
Copy link
Contributor Author

pr failed due to weird "no internet connection".
image

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@slorber slorber merged commit 3213955 into facebook:master Jun 26, 2020
@slorber slorber added pr: maintenance This PR does not produce any behavior differences to end users when upgrading. pr: polish This PR adds a very minor behavior improvement that users will enjoy. labels Jun 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: maintenance This PR does not produce any behavior differences to end users when upgrading. pr: polish This PR adds a very minor behavior improvement that users will enjoy.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validate baseURL
4 participants