Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/snaps-utils/src/snaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ describe('assertIsValidSnapId', () => {
// TODO: Either fix this lint violation or explain why it's necessary to
// ignore.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string
`Invalid snap ID: Expected the value to satisfy a union of \`intersection | string\`, but received: ${value}.`,
`Invalid snap ID: Expected the value to satisfy a union of \`intersection | Base Snap Id\`, but received: ${value}.`,
);
},
);

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

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

it.each(['local:😎', 'local:␡'])(
'disallows non-ASCII symbols #%#',
(value) => {
expect(() => assertIsValidSnapId(value)).toThrow(
`Invalid snap ID: Expected the value to satisfy a union of \`intersection | string\`, but received: "${value}".`,
`Invalid snap ID: Expected the value to satisfy a union of \`intersection | Base Snap Id\`, but received: "${value}".`,
);
},
);
Expand Down
8 changes: 5 additions & 3 deletions packages/snaps-utils/src/snaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import {
enums,
intersection,
literal,
pattern,
refine,
string,
union,
validate,
} from '@metamask/superstruct';
import type { Json } from '@metamask/utils';
import { assert, isObject, assertStruct } from '@metamask/utils';
import { assert, isObject, assertStruct, definePattern } from '@metamask/utils';
import { base64 } from '@scure/base';
import stableStringify from 'fast-json-stable-stringify';
import validateNPMPackage from 'validate-npm-package-name';
Expand Down Expand Up @@ -228,7 +227,10 @@ export async function validateSnapShasum(
export const LOCALHOST_HOSTNAMES = ['localhost', '127.0.0.1', '[::1]'] as const;

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

const LocalSnapIdSubUrlStruct = uri({
protocol: enums(['http:', 'https:']),
Expand Down
7 changes: 3 additions & 4 deletions packages/snaps-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
instance,
is,
optional,
pattern,
refine,
size,
string,
Expand All @@ -12,7 +11,7 @@ import {
} from '@metamask/superstruct';
import type { Infer, Struct } from '@metamask/superstruct';
import type { Json } from '@metamask/utils';
import { VersionStruct } from '@metamask/utils';
import { definePattern, VersionStruct } from '@metamask/utils';

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

export const NameStruct = size(
pattern(
string(),
definePattern(
'Snap Name',
/^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/u,
),
1,
Expand Down
Loading