generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently, all our CAIP structs are being inferred as `string`. This PR adds a new `definePattern` which allows to using a template literal string instead. Also, this new `definePattern` allows to "name" the `pattern`. Here's an example: ```ts const HexStringPattern = pattern(string(), hexPattern); const HexString = definePattern('HexString', hexPattern); assert('foobar', HexStringPattern); // StructError: Expected a string matching `/^0x[0-9a-f]+$/` but received "foobar" assert('foobar', HexString); // StructError: Expected a value of type `HexString`, but received: `"foobar"` ``` If you think the new `definePattern` should go in a separate PR, I can split that up. I do believe this is **BREAKING CHANGE** since the error messages will be different, so we can expect the consumers of this packages to update some of there tests (but I don't really know if we consider this a breaking change in other packages?) --------- Co-authored-by: Daniel Rocha <[email protected]> Co-authored-by: Maarten Zuidhoorn <[email protected]>
- Loading branch information
1 parent
48267f8
commit 9ac7ec2
Showing
6 changed files
with
98 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { assert, is, pattern, string } from '@metamask/superstruct'; | ||
|
||
import { definePattern } from './superstruct'; | ||
|
||
describe('definePattern', () => { | ||
const hexPattern = /^0x[0-9a-f]+$/u; | ||
const HexStringPattern = pattern(string(), hexPattern); | ||
const HexString = definePattern('HexString', hexPattern); | ||
|
||
it('is similar to superstruct.pattern', () => { | ||
expect(is('0xdeadbeef', HexStringPattern)).toBe(true); | ||
expect(is('0xdeadbeef', HexString)).toBe(true); | ||
expect(is('foobar', HexStringPattern)).toBe(false); | ||
expect(is('foobar', HexString)).toBe(false); | ||
}); | ||
|
||
it('throws and error if assert fails', () => { | ||
const value = 'foobar'; | ||
expect(() => assert(value, HexString)).toThrow( | ||
`Expected a value of type \`HexString\`, but received: \`"foobar"\``, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { Struct } from '@metamask/superstruct'; | ||
import { define } from '@metamask/superstruct'; | ||
|
||
/** | ||
* Defines a new string-struct matching a regular expression. | ||
* | ||
* @example | ||
* const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu); | ||
* type EthAddress = Infer<typeof EthAddressStruct>; // string | ||
* | ||
* const CaipChainIdStruct = defineTypedPattern<`${string}:${string}`>( | ||
* 'CaipChainId', | ||
* /^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/u; | ||
* ); | ||
* type CaipChainId = Infer<typeof CaipChainIdStruct>; // `${string}:${string}` | ||
* @param name - Type name. | ||
* @param pattern - Regular expression to match. | ||
* @template Pattern - The pattern type, defaults to `string`. | ||
* @returns A new string-struct that matches the given pattern. | ||
*/ | ||
export function definePattern<Pattern extends string = string>( | ||
name: string, | ||
pattern: RegExp, | ||
): Struct<Pattern, null> { | ||
return define<Pattern>(name, (value: unknown): boolean | string => { | ||
return typeof value === 'string' && pattern.test(value); | ||
}); | ||
} |