frontend-internal: cleaner messaging for invalid opaque type versions
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -131,17 +131,17 @@ describe('OpaqueType', () => {
|
||||
expect(() =>
|
||||
OpaqueMyType.toInternal({ $$type: 'my-type' }),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Invalid opaque type instance, got version 'undefined', expected one of ['v1']"`,
|
||||
`"Invalid opaque type instance, got version undefined, expected 'v1'"`,
|
||||
);
|
||||
expect(() =>
|
||||
OpaqueMyType.toInternal({ $$type: 'my-type', version: 'v3' }),
|
||||
OpaqueMyType.toInternal({ $$type: 'my-type', version: 'v0' }),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Invalid opaque type instance, got version 'v3', expected one of ['v1']"`,
|
||||
`"Invalid opaque type instance, got version 'v0', expected 'v1'"`,
|
||||
);
|
||||
expect(() =>
|
||||
OpaqueMyType.toInternal({ $$type: 'my-type', version: { foo: 'bar' } }),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Invalid opaque type instance, got version '[object Object]', expected one of ['v1']"`,
|
||||
`"Invalid opaque type instance, got version '[object Object]', expected 'v1'"`,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -160,10 +160,14 @@ describe('OpaqueType', () => {
|
||||
| {
|
||||
version: 'v2';
|
||||
bar: string;
|
||||
}
|
||||
| {
|
||||
version: 'v3';
|
||||
baz: string;
|
||||
};
|
||||
}>({
|
||||
type: 'my-type',
|
||||
versions: ['v1', 'v2'],
|
||||
versions: ['v1', 'v2', 'v3'],
|
||||
});
|
||||
|
||||
OpaqueMyType.create({
|
||||
@@ -176,7 +180,7 @@ describe('OpaqueType', () => {
|
||||
OpaqueMyType.create({
|
||||
$$type: 'my-type',
|
||||
// @ts-expect-error - unsupported version
|
||||
version: 'v3',
|
||||
version: 'v0',
|
||||
foo: 'bar',
|
||||
});
|
||||
|
||||
@@ -264,12 +268,12 @@ describe('OpaqueType', () => {
|
||||
expect(() =>
|
||||
OpaqueMyType.toInternal({ $$type: 'my-type' }),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Invalid opaque type instance, got version 'undefined', expected one of ['v1', 'v2']"`,
|
||||
`"Invalid opaque type instance, got version undefined, expected 'v1', 'v2', or 'v3'"`,
|
||||
);
|
||||
expect(() =>
|
||||
OpaqueMyType.toInternal({ $$type: 'my-type', version: 'v3' }),
|
||||
OpaqueMyType.toInternal({ $$type: 'my-type', version: 'v0' }),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Invalid opaque type instance, got version 'v3', expected one of ['v1', 'v2']"`,
|
||||
`"Invalid opaque type instance, got version 'v0', expected 'v1', 'v2', or 'v3'"`,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -339,9 +343,9 @@ describe('OpaqueType', () => {
|
||||
|
||||
expect(OpaqueMyType.toInternal({ $$type: 'my-type' })).toBeDefined();
|
||||
expect(() =>
|
||||
OpaqueMyType.toInternal({ $$type: 'my-type', version: 'v3' }),
|
||||
OpaqueMyType.toInternal({ $$type: 'my-type', version: 'v0' }),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Invalid opaque type instance, got version 'v3', expected undefined"`,
|
||||
`"Invalid opaque type instance, got version 'v0', expected undefined"`,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -363,7 +367,7 @@ describe('OpaqueType', () => {
|
||||
};
|
||||
}>({
|
||||
type: 'my-type',
|
||||
versions: ['v1', undefined],
|
||||
versions: [undefined, 'v1'],
|
||||
});
|
||||
|
||||
OpaqueMyType.create({
|
||||
@@ -465,7 +469,7 @@ describe('OpaqueType', () => {
|
||||
expect(() =>
|
||||
OpaqueMyType.toInternal({ $$type: 'my-type', version: 'v3' }),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Invalid opaque type instance, got version 'v3', expected undefined or one of ['v1']"`,
|
||||
`"Invalid opaque type instance, got version 'v3', expected undefined or 'v1'"`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -99,18 +99,16 @@ export class OpaqueType<
|
||||
}
|
||||
|
||||
if (!this.#versions.has(value.version)) {
|
||||
const expected = [];
|
||||
if (this.#versions.has(undefined)) {
|
||||
expected.push('undefined');
|
||||
}
|
||||
const versions = Array.from(this.#versions).filter(Boolean);
|
||||
if (versions.length > 0) {
|
||||
expected.push(`one of ['${versions.join("', '")}']`);
|
||||
const versions = Array.from(this.#versions).map(this.#stringifyVersion);
|
||||
if (versions.length > 1) {
|
||||
versions[versions.length - 1] = `or ${versions[versions.length - 1]}`;
|
||||
}
|
||||
const expected =
|
||||
versions.length > 2 ? versions.join(', ') : versions.join(' ');
|
||||
throw new TypeError(
|
||||
`Invalid opaque type instance, got version '${
|
||||
value.version
|
||||
}', expected ${expected.join(' or ')}`,
|
||||
`Invalid opaque type instance, got version ${this.#stringifyVersion(
|
||||
value.version,
|
||||
)}, expected ${expected}`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -147,4 +145,8 @@ export class OpaqueType<
|
||||
}
|
||||
return String(value);
|
||||
}
|
||||
|
||||
#stringifyVersion = (version: string | undefined) => {
|
||||
return version ? `'${version}'` : 'undefined';
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user