From ec02d83f40b3156c18894464c3502edc8fb0ee28 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 19 Mar 2020 14:31:58 +0100 Subject: [PATCH] core/apis/ApiRef: use domain dot notation --- packages/core/src/api/apis/ApiRef.test.ts | 20 ++++++++++++++++---- packages/core/src/api/apis/ApiRef.ts | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/core/src/api/apis/ApiRef.test.ts b/packages/core/src/api/apis/ApiRef.test.ts index 902ab1bfbe..b592e327b0 100644 --- a/packages/core/src/api/apis/ApiRef.test.ts +++ b/packages/core/src/api/apis/ApiRef.test.ts @@ -25,14 +25,26 @@ describe('ApiRef', () => { expect(() => ref.T).toThrow('tried to read ApiRef.T of apiRef{abc}'); }); - it('should require a ascii letters only in id', () => { - for (const id of ['a', 'abc', 'ABC', 'aBC', 'aBc']) { + it('should reject invalid ids', () => { + for (const id of ['a', 'abc', 'a.b.c', 'ab.c', 'abc.abc.abc3']) { expect(new ApiRef({ id, description: '123' }).id).toBe(id); } - for (const id of ['123', 'ab-c', 'ab_c', 'a2c', '', '_']) { + for (const id of [ + '123', + 'ab-c', + 'ab_c', + '.', + '2ac', + 'ab.3a', + '.abc', + 'abc.', + 'ab..s', + '', + '_', + ]) { expect(() => new ApiRef({ id, description: '123' }).id).toThrow( - `API id must only contain ascii letters, got '${id}'`, + `API id must only contain lowercase alphanums separated by dots, got '${id}'`, ); } }); diff --git a/packages/core/src/api/apis/ApiRef.ts b/packages/core/src/api/apis/ApiRef.ts index c28bb100b9..b3f4d48428 100644 --- a/packages/core/src/api/apis/ApiRef.ts +++ b/packages/core/src/api/apis/ApiRef.ts @@ -21,9 +21,9 @@ export type ApiRefConfig = { export default class ApiRef { constructor(private readonly config: ApiRefConfig) { - if (!config.id.match(/^[a-zA-Z]+$/)) { + if (!config.id.match(/^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)*$/)) { throw new Error( - `API id must only contain ascii letters, got '${config.id}'`, + `API id must only contain lowercase alphanums separated by dots, got '${config.id}'`, ); } }