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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "2WLcpc3RXg2Up0zeSMxWS/2rsVI1bC1iHCHAhzc3DOA=",
"shasum": "03Y0FecvVHqC2DG+GpXwwNPtWFl2b5tA3PLjMK7d3hw=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "LQYiFCEu2fQXCuzfYHVpFfqffCbzzUjaqH7kPwgBfNc=",
"shasum": "I/C34T7c3uxkDeFC91tMUg3raVe104ocTkr5F4ciGyQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
1 change: 1 addition & 0 deletions packages/snaps-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export * from './errors';
export * from './error-wrappers';
export * from './images';
export * from './types';
export * from './structs';
export * from './ui';
20 changes: 20 additions & 0 deletions packages/snaps-sdk/src/structs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { any, is, string } from '@metamask/superstruct';

import { nonEmptyRecord } from './structs';

describe('nonEmptyRecord', () => {
it.each([[1, 2, 3], { foo: 'bar' }])('validates "%p"', (value) => {
const struct = nonEmptyRecord(string(), any());

expect(is(value, struct)).toBe(true);
});

it.each(['foo', 42, null, undefined, [], {}])(
'does not validate "%p"',
(value) => {
const struct = nonEmptyRecord(string(), any());

expect(is(value, struct)).toBe(false);
},
);
});
21 changes: 21 additions & 0 deletions packages/snaps-sdk/src/structs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Struct } from '@metamask/superstruct';
import { record, refine } from '@metamask/superstruct';

/**
* Refine a struct to be a non-empty record.
*
* @param Key - The struct for the record key.
* @param Value - The struct for the record value.
* @returns The refined struct.
*/
export function nonEmptyRecord<Key extends string, Value>(
Key: Struct<Key>,
Value: Struct<Value>,
) {
return refine(record(Key, Value), 'Non-empty record', (value) => {
return (
(Array.isArray(value) && value.length > 0) ||
Object.keys(value).length > 0
);
});
}
Loading