Skip to content

Commit

Permalink
use URL rather than regex
Browse files Browse the repository at this point in the history
  • Loading branch information
anshul committed Aug 7, 2020
1 parent 2dccc93 commit 9eee083
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ exports[`validation schemas RemarkPluginsSchema: for value=false 1`] = `"\\"valu

exports[`validation schemas RemarkPluginsSchema: for value=null 1`] = `"\\"value\\" must be an array"`;

exports[`validation schemas URISchema 1`] = `"\\"value\\" does not match any of the allowed types"`;
exports[`validation schemas URISchema: for value="invalidURL" 1`] = `"\\"value\\" does not match any of the allowed types"`;
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,15 @@ describe('validation schemas', () => {
testMarkdownPluginSchemas(RehypePluginsSchema);
});

const useTest = <T>(schema: Joi.Schema) => (value: T) =>
Joi.attempt(value, schema);

test('URISchema', () => {
const validURL = 'https://docusaurus.io';
const doubleHash = 'https://docusaurus.io#github#/:';
const invalidURL = 'https://docusaurus.io?search= ';
const testURL = useTest(URISchema);
expect(testURL(validURL)).toBe(validURL);
expect(testURL(doubleHash)).toBe(doubleHash);
expect(() => testURL(invalidURL)).toThrowErrorMatchingSnapshot();
const invalidURL = 'invalidURL';
const urlFromIssue = 'https://riot.im/app/#/room/#ligo-public:matrix.org';
const {testFail, testOK} = createTestHelpers({schema: URISchema});
testOK(validURL);
testOK(doubleHash);
testFail(invalidURL);
testOK(urlFromIssue);
});
});
16 changes: 12 additions & 4 deletions packages/docusaurus-utils-validation/src/validationSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
*/
import * as Joi from '@hapi/joi';

// it is from Joi core with support of more than one hash
const URLRegex = /^(?=.)(?!https?:\/$)(?:[a-zA-Z][a-zA-Z\d+-.]*:(?:(?:\/\/(?:[\w-.~%\dA-Fa-f!$&'()*+,;=:]*@)?(?:\[(?:(?:(?:[\dA-Fa-f]{1,4}:){6}(?:[\dA-Fa-f]{1,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\d|1\d\d|2[0-4]\d|25[0-5]))|(?:(?:[\dA-Fa-f]{1,4}:){0,4}[\dA-Fa-f]{1,4})?::(?:[\dA-Fa-f]{1,4}:[\dA-Fa-f]{1,4}|(?:(?:0{0,2}\d|0?[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:0{0,2}\d|0?[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(?:(?:[\dA-Fa-f]{1,4}:){0,5}[\dA-Fa-f]{1,4})?::[\dA-Fa-f]{1,4}|(?:(?:[\dA-Fa-f]{1,4}:){0,6}[\dA-Fa-f]{1,4})?::)|v[\dA-Fa-f]+\.[\w-.~!$&'()*+,;=:]+)\]|(?:(?:0{0,2}\d|0?[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:0{0,2}\d|0?[1-9]\d|1\d\d|2[0-4]\d|25[0-5])|[\w-.~%\dA-Fa-f!$&'()*+,;=]{1,255})(?::\d*)?(?:\/[\w-.~%\dA-Fa-f!$&'()*+,;=:@]*)*)|\/(?:[\w-.~%\dA-Fa-f!$&'()*+,;=:@]+(?:\/[\w-.~%\dA-Fa-f!$&'()*+,;=:@]*)*)?|[\w-.~%\dA-Fa-f!$&()*+,;=:@]+(?:\/[\w-.~%\dA-Fa-f!$&'()*+,;=:@]*)*|(?:\/\/\/[\w-.~%\dA-Fa-f!$&'()*+,;=:@]*(?:\/[\w-.~%\dA-Fa-f!$&'()*+,;=:@]*)*)))(?:\?[\w-.~%\dA-Fa-f!$&'()*+,;=:@/?]*(?=#|$))?(?:#[\w-.~%\dA-Fa-f!$&'()*+,;=:@/?#]*)?$/;

export const PluginIdSchema = Joi.string()
.regex(/^[a-zA-Z_\-]+$/)
// duplicate core constant, otherwise cyclic dependency is created :(
Expand All @@ -31,5 +28,16 @@ export const AdmonitionsSchema = Joi.object().default({});

export const URISchema = Joi.alternatives(
Joi.string().uri(),
Joi.string().regex(URLRegex),
Joi.custom((val, helpers) => {
try {
const url = new URL(val);
if (url) {
return val;
} else {
return helpers.error('any.invalid');
}
} catch {
return helpers.error('any.invalid');
}
}),
);

0 comments on commit 9eee083

Please sign in to comment.