core/apis/ApiRef: use domain dot notation

This commit is contained in:
Patrik Oldsberg
2020-03-19 14:31:58 +01:00
parent e1a7f0fb29
commit ec02d83f40
2 changed files with 18 additions and 6 deletions
+16 -4
View File
@@ -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}'`,
);
}
});
+2 -2
View File
@@ -21,9 +21,9 @@ export type ApiRefConfig = {
export default class ApiRef<T> {
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}'`,
);
}
}