Skip to content

Commit 38d95fc

Browse files
committed
remove pattern in favour of definePattern
1 parent f5e359f commit 38d95fc

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

packages/snaps-utils/src/snaps.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ describe('assertIsValidSnapId', () => {
5252
// TODO: Either fix this lint violation or explain why it's necessary to
5353
// ignore.
5454
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string
55-
`Invalid snap ID: Expected the value to satisfy a union of \`intersection | string\`, but received: ${value}.`,
55+
`Invalid snap ID: Expected the value to satisfy a union of \`intersection | Base Snap Id\`, but received: ${value}.`,
5656
);
5757
},
5858
);
5959

6060
it('throws for invalid snap id', () => {
6161
expect(() => assertIsValidSnapId('foo:bar')).toThrow(
62-
`Invalid snap ID: Expected the value to satisfy a union of \`intersection | string\`, but received: "foo:bar".`,
62+
`Invalid snap ID: Expected the value to satisfy a union of \`intersection | Base Snap Id\`, but received: "foo:bar".`,
6363
);
6464
});
6565

@@ -82,15 +82,15 @@ describe('assertIsValidSnapId', () => {
8282
'local:http://localhost:8000\r',
8383
])('disallows whitespace #%#', (value) => {
8484
expect(() => assertIsValidSnapId(value)).toThrow(
85-
/Invalid snap ID: Expected the value to satisfy a union of `intersection \| string`, but received: .+\./u,
85+
/Invalid snap ID: Expected the value to satisfy a union of `intersection \| Base Snap Id`, but received: .+\./u,
8686
);
8787
});
8888

8989
it.each(['local:😎', 'local:␡'])(
9090
'disallows non-ASCII symbols #%#',
9191
(value) => {
9292
expect(() => assertIsValidSnapId(value)).toThrow(
93-
`Invalid snap ID: Expected the value to satisfy a union of \`intersection | string\`, but received: "${value}".`,
93+
`Invalid snap ID: Expected the value to satisfy a union of \`intersection | Base Snap Id\`, but received: "${value}".`,
9494
);
9595
},
9696
);

packages/snaps-utils/src/snaps.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ import {
1212
enums,
1313
intersection,
1414
literal,
15-
pattern,
1615
refine,
1716
string,
1817
union,
1918
validate,
2019
} from '@metamask/superstruct';
2120
import type { Json } from '@metamask/utils';
22-
import { assert, isObject, assertStruct } from '@metamask/utils';
21+
import { assert, isObject, assertStruct, definePattern } from '@metamask/utils';
2322
import { base64 } from '@scure/base';
2423
import stableStringify from 'fast-json-stable-stringify';
2524
import validateNPMPackage from 'validate-npm-package-name';
@@ -228,7 +227,10 @@ export async function validateSnapShasum(
228227
export const LOCALHOST_HOSTNAMES = ['localhost', '127.0.0.1', '[::1]'] as const;
229228

230229
// Require snap ids to only consist of printable ASCII characters
231-
export const BaseSnapIdStruct = pattern(string(), /^[\x21-\x7E]*$/u);
230+
export const BaseSnapIdStruct = definePattern(
231+
'Base Snap Id',
232+
/^[\x21-\x7E]*$/u,
233+
);
232234

233235
const LocalSnapIdSubUrlStruct = uri({
234236
protocol: enums(['http:', 'https:']),

packages/snaps-utils/src/types.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
instance,
33
is,
44
optional,
5-
pattern,
65
refine,
76
size,
87
string,
@@ -12,7 +11,7 @@ import {
1211
} from '@metamask/superstruct';
1312
import type { Infer, Struct } from '@metamask/superstruct';
1413
import type { Json } from '@metamask/utils';
15-
import { VersionStruct } from '@metamask/utils';
14+
import { definePattern, VersionStruct } from '@metamask/utils';
1615

1716
import type { SnapCaveatType } from './caveats';
1817
import type { SnapFunctionExports, SnapRpcHookArgs } from './handlers';
@@ -26,8 +25,8 @@ export enum NpmSnapFileNames {
2625
}
2726

2827
export const NameStruct = size(
29-
pattern(
30-
string(),
28+
definePattern(
29+
'Snap Name',
3130
/^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/u,
3231
),
3332
1,

0 commit comments

Comments
 (0)