Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add placeholder parameter for Prompt dialog #1007

Merged
merged 3 commits into from Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/rpc-methods/jest.config.js
Expand Up @@ -6,10 +6,10 @@ module.exports = deepmerge(baseConfig, {
coveragePathIgnorePatterns: ['./src/index.ts'],
coverageThreshold: {
global: {
branches: 86.84,
branches: 87.09,
functions: 87.27,
lines: 83.18,
statements: 83.18,
lines: 83.27,
statements: 83.27,
},
},
testTimeout: 2500,
Expand Down
52 changes: 52 additions & 0 deletions packages/rpc-methods/src/restricted/dialog.test.ts
Expand Up @@ -108,6 +108,7 @@ describe('implementation', () => {
fields: {
title: 'Foo',
description: 'Bar',
placeholder: 'Baz',
},
},
});
Expand All @@ -116,6 +117,7 @@ describe('implementation', () => {
expect(hooks.showDialog).toHaveBeenCalledWith('foo', DialogType.Prompt, {
title: 'Foo',
description: 'Bar',
placeholder: 'Baz',
});
});
});
Expand Down Expand Up @@ -307,6 +309,31 @@ describe('implementation', () => {
},
);

it.each([true, 2, [], {}, new (class {})()])(
'rejects invalid placeholder contents',
async (value) => {
const hooks = getMockDialogHooks();
const implementation = getDialogImplementation(hooks);

await expect(
implementation({
context: { origin: 'foo' },
method: 'snap_dialog',
params: {
type: DialogType.Prompt,
fields: {
title: 'Foo',
description: 'Bar',
placeholder: value,
} as any,
},
}),
).rejects.toThrow(
/Invalid params: At path: fields\.placeholder -- Expected a string, but received: .*\./u,
);
},
);

it('rejects too long text area contents', async () => {
const hooks = getMockDialogHooks();
const implementation = getDialogImplementation(hooks);
Expand Down Expand Up @@ -349,5 +376,30 @@ describe('implementation', () => {
'Invalid params: Prompts may not specify a "textAreaContent" field.',
);
});

it.each([DialogType.Alert, DialogType.Confirmation])(
'rejects placeholder field for alerts and confirmations',
async (type) => {
const hooks = getMockDialogHooks();
const implementation = getDialogImplementation(hooks);
await expect(
implementation({
context: { origin: 'foo' },
method: 'snap_dialog',
params: {
type,
fields: {
title: 'Foo',
description: 'Bar',
textAreaContent: 'Baz',
placeholder: 'Foobar',
} as any,
},
}),
).rejects.toThrow(
'Invalid params: Alerts or confirmations may not specify a "placeholder" field.',
);
},
);
});
});
20 changes: 16 additions & 4 deletions packages/rpc-methods/src/restricted/dialog.ts
Expand Up @@ -34,18 +34,23 @@ const BaseFieldsStruct = object({
title: size(string(), 1, 40),
description: optional(size(string(), 1, 140)),
textAreaContent: optional(size(string(), 1, 1800)),
placeholder: optional(size(string(), 1, 40)),
});

const PromptFieldsStruct = omit(BaseFieldsStruct, ['textAreaContent']);

const AlertFieldsStruct = omit(BaseFieldsStruct, ['placeholder']);

const ConfirmationFieldsStruct = omit(BaseFieldsStruct, ['placeholder']);

/**
* @property title - The alert title, no greater than 40 characters long.
* @property description - A description, displayed with the title, no greater
* than 140 characters long.
* @property textAreaContent - Free-from text content, no greater than 1800
* characters long.
*/
export type AlertFields = Infer<typeof BaseFieldsStruct>;
export type AlertFields = Infer<typeof AlertFieldsStruct>;

/**
* @property title - A question describing what the user is confirming, no
Expand All @@ -55,7 +60,7 @@ export type AlertFields = Infer<typeof BaseFieldsStruct>;
* @property textAreaContent - Free-from text content, no greater than 1800
* characters long.
*/
export type ConfirmationFields = Infer<typeof BaseFieldsStruct>;
export type ConfirmationFields = Infer<typeof ConfirmationFieldsStruct>;

/**
* @property title - The prompt title, no greater than 40 characters long.
Expand Down Expand Up @@ -139,12 +144,12 @@ const BaseParamsStruct = type({

const AlertParametersStruct = object({
type: literal(DialogType.Alert),
fields: BaseFieldsStruct,
fields: AlertFieldsStruct,
});

const ConfirmationParametersStruct = object({
type: literal(DialogType.Confirmation),
fields: BaseFieldsStruct,
fields: ConfirmationFieldsStruct,
});

const PromptParametersStruct = object({
Expand Down Expand Up @@ -235,6 +240,13 @@ function getValidatedParams(
});
}

if (key === 'placeholder' && errorType === 'never') {
throw ethErrors.rpc.invalidParams({
message:
'Invalid params: Alerts or confirmations may not specify a "placeholder" field.',
});
}

throw ethErrors.rpc.invalidParams({
message: `Invalid params: ${error.message}.`,
});
Expand Down