@metamask-previews/keyring-utils 3.0.0-fe8156c → 3.1.0-e017e7a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -1
- package/dist/superstruct.cjs +14 -2
- package/dist/superstruct.cjs.map +1 -1
- package/dist/superstruct.d.cts +11 -2
- package/dist/superstruct.d.cts.map +1 -1
- package/dist/superstruct.d.mts +11 -2
- package/dist/superstruct.d.mts.map +1 -1
- package/dist/superstruct.mjs +14 -3
- package/dist/superstruct.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [3.1.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add `superstruct.type` support for `exactOptional` ([#316](https://github.com/MetaMask/accounts/pull/316))
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- Now supports `unknown[]` array for `Keyring.signTypedData` ([#224](https://github.com/MetaMask/accounts/pull/224))
|
|
19
|
+
|
|
10
20
|
## [3.0.0]
|
|
11
21
|
|
|
12
22
|
### Changed
|
|
@@ -99,7 +109,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
99
109
|
- This new version fixes a bug with CJS re-exports.
|
|
100
110
|
- Initial release ([#24](https://github.com/MetaMask/accounts/pull/24))
|
|
101
111
|
|
|
102
|
-
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-utils@3.
|
|
112
|
+
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-utils@3.1.0...HEAD
|
|
113
|
+
[3.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-utils@3.0.0...@metamask/keyring-utils@3.1.0
|
|
103
114
|
[3.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-utils@2.3.1...@metamask/keyring-utils@3.0.0
|
|
104
115
|
[2.3.1]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-utils@2.3.0...@metamask/keyring-utils@2.3.1
|
|
105
116
|
[2.3.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-utils@2.2.0...@metamask/keyring-utils@2.3.0
|
package/dist/superstruct.cjs
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.object = object;
|
|
4
|
+
exports.type = type;
|
|
4
5
|
exports.exactOptional = exactOptional;
|
|
5
6
|
exports.strictMask = strictMask;
|
|
6
7
|
exports.selectiveUnion = selectiveUnion;
|
|
7
8
|
const superstruct_1 = require("@metamask/superstruct");
|
|
8
9
|
/**
|
|
9
|
-
* Change the return type of a superstruct object
|
|
10
|
-
* optional properties.
|
|
10
|
+
* Change the return type of a superstruct's `object` function to support
|
|
11
|
+
* exact optional properties.
|
|
11
12
|
*
|
|
12
13
|
* @param schema - The object schema.
|
|
13
14
|
* @returns A struct representing an object with a known set of properties.
|
|
@@ -15,6 +16,17 @@ const superstruct_1 = require("@metamask/superstruct");
|
|
|
15
16
|
function object(schema) {
|
|
16
17
|
return (0, superstruct_1.object)(schema);
|
|
17
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Change the return type of a superstruct's `type` function to support
|
|
21
|
+
* exact optional properties.
|
|
22
|
+
*
|
|
23
|
+
* @param schema - The object schema.
|
|
24
|
+
* @returns A struct representing an object with a known set of properties
|
|
25
|
+
* and ignore unknown properties.
|
|
26
|
+
*/
|
|
27
|
+
function type(schema) {
|
|
28
|
+
return (0, superstruct_1.type)(schema);
|
|
29
|
+
}
|
|
18
30
|
/**
|
|
19
31
|
* Check if the current property is present in its parent object.
|
|
20
32
|
*
|
package/dist/superstruct.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.cjs","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"superstruct.cjs","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":";;AAoEA,wBAIC;AAUD,oBAIC;AA4BD,sCAYC;AAaD,gCAOC;AAwBD,wCAyBC;AAnMD,uDAK+B;AAwD/B;;;;;;GAMG;AACH,SAAgB,MAAM,CACpB,MAAc;IAEd,OAAO,IAAA,oBAAQ,EAAC,MAAM,CAAQ,CAAC;AACjC,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,IAAI,CAClB,MAAc;IAEd,OAAO,IAAA,kBAAM,EAAC,MAAM,CAAQ,CAAC;AAC/B,CAAC;AAED;;;;;GAKG;AACH,SAAS,WAAW,CAAC,GAAY;IAC/B,MAAM,QAAQ,GAAW,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACvD,MAAM,MAAM,GAA4B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAE1E,OAAO,QAAQ,IAAI,MAAM,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,aAAa,CAC3B,MAA4B;IAE5B,OAAO,IAAI,oBAAM,CAAC;QAChB,GAAG,MAAM;QAET,SAAS,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CACxB,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC;QAEnD,OAAO,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CACtB,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAa,EAAE,GAAG,CAAC;KAC1D,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CACxB,KAAc,EACd,MAA4B,EAC5B,OAAgB;IAEhB,IAAA,oBAAM,EAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,OAAO,KAAK,CAAC;AACf,CAAC;AAgBD;;;;;;;GAOG;AACH,SAAgB,cAAc,CAC5B,QAAkB;IAElB,OAAO,IAAI,oBAAM,CAAC;QAChB,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,IAAI;QAEZ,CAAC,OAAO,CAAC,KAAU,EAAE,OAAY;YAC/B,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACjD,CAAC;QAED,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO;YACrB,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACjD,CAAC;QAED,OAAO,CAAC,KAAK,EAAE,OAAO;YACpB,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACjD,CAAC;QAED,SAAS,CAAC,KAAK,EAAE,OAAO;YACtB,uEAAuE;YACvE,YAAY;YACZ,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACnD,CAAC;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n Struct,\n assert,\n object as stObject,\n type as stType,\n} from '@metamask/superstruct';\nimport type {\n Infer,\n Context,\n ObjectSchema,\n OmitBy,\n Optionalize,\n PickBy,\n Simplify,\n AnyStruct,\n} from '@metamask/superstruct';\n\nimport type { Equals } from './types';\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\ndeclare const ExactOptionalSymbol: unique symbol;\n\nexport type ExactOptionalTag = {\n type: typeof ExactOptionalSymbol;\n};\n\n/**\n * Exclude type `Type` from the properties of `Obj`.\n *\n * ```ts\n * type Foo = { a: string | null; b: number };\n * type Bar = ExcludeType<Foo, null>;\n * // Bar = { a: string, b: number }\n * ```\n */\nexport type ExcludeType<Obj, Type> = {\n [K in keyof Obj]: Exclude<Obj[K], Type>;\n};\n\n/**\n * Make optional all properties that have the `ExactOptionalTag` type.\n *\n * ```ts\n * type Foo = { a: string | ExactOptionalTag; b: number};\n * type Bar = ExactOptionalize<Foo>;\n * // Bar = { a?: string; b: number}\n * ```\n */\nexport type ExactOptionalize<Schema extends object> = OmitBy<\n Schema,\n ExactOptionalTag\n> &\n Partial<ExcludeType<PickBy<Schema, ExactOptionalTag>, ExactOptionalTag>>;\n\n/**\n * Infer a type from an superstruct object schema.\n */\nexport type ObjectType<Schema extends ObjectSchema> = Simplify<\n ExactOptionalize<Optionalize<{ [K in keyof Schema]: Infer<Schema[K]> }>>\n>;\n\n/**\n * Change the return type of a superstruct's `object` function to support\n * exact optional properties.\n *\n * @param schema - The object schema.\n * @returns A struct representing an object with a known set of properties.\n */\nexport function object<Schema extends ObjectSchema>(\n schema: Schema,\n): Struct<ObjectType<Schema>, Schema> {\n return stObject(schema) as any;\n}\n\n/**\n * Change the return type of a superstruct's `type` function to support\n * exact optional properties.\n *\n * @param schema - The object schema.\n * @returns A struct representing an object with a known set of properties\n * and ignore unknown properties.\n */\nexport function type<Schema extends ObjectSchema>(\n schema: Schema,\n): Struct<ObjectType<Schema>, Schema> {\n return stType(schema) as any;\n}\n\n/**\n * Check if the current property is present in its parent object.\n *\n * @param ctx - The context to check.\n * @returns `true` if the property is present, `false` otherwise.\n */\nfunction hasOptional(ctx: Context): boolean {\n const property: string = ctx.path[ctx.path.length - 1];\n const parent: Record<string, unknown> = ctx.branch[ctx.branch.length - 2];\n\n return property in parent;\n}\n\n/**\n * Augment a struct to allow exact-optional values. Exact-optional values can\n * be omitted but cannot be `undefined`.\n *\n * ```ts\n * const foo = object({ bar: exactOptional(string()) });\n * type Foo = Infer<typeof foo>;\n * // Foo = { bar?: string }\n * ```\n *\n * @param struct - The struct to augment.\n * @returns The augmented struct.\n */\nexport function exactOptional<Type, Schema>(\n struct: Struct<Type, Schema>,\n): Struct<Type | ExactOptionalTag, Schema> {\n return new Struct({\n ...struct,\n\n validator: (value, ctx) =>\n !hasOptional(ctx) || struct.validator(value, ctx),\n\n refiner: (value, ctx) =>\n !hasOptional(ctx) || struct.refiner(value as Type, ctx),\n });\n}\n\n/**\n * Assert that a value is valid according to a struct.\n *\n * It is similar to superstruct's mask function, but it does not ignore extra\n * properties.\n *\n * @param value - Value to check.\n * @param struct - Struct to validate the value against.\n * @param message - Error message to throw if the value is not valid.\n * @returns The value if it is valid.\n */\nexport function strictMask<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n message?: string,\n): Type {\n assert(value, struct, message);\n return value;\n}\n\n/**\n * Extracts the type from a struct definition and asserts that it matches the\n * expected type. If the types do not match, the type `never` is returned.\n *\n * @param StructType - The struct type to infer.\n * @param ExpectedType - The expected type.\n */\nexport type InferEquals<\n StructType extends Struct<any, any>,\n ExpectedType,\n> = Equals<Infer<StructType>, ExpectedType> extends true\n ? Infer<StructType>\n : never;\n\n/**\n * Create a custom union struct that uses a `selector` function for choosing\n * the validation path.\n *\n * @param selector - The selector function choosing the struct to validate with.\n * @returns The `superstruct` struct, which validates that the value satisfies\n * one of the structs.\n */\nexport function selectiveUnion<Selector extends (value: any) => AnyStruct>(\n selector: Selector,\n): Struct<Infer<ReturnType<Selector>>, null> {\n return new Struct({\n type: 'union',\n schema: null,\n\n *entries(value: any, context: any): ReturnType<Struct['entries']> {\n yield* selector(value).entries(value, context);\n },\n\n *refiner(value, context): ReturnType<Struct['refiner']> {\n yield* selector(value).refiner(value, context);\n },\n\n coercer(value, context): ReturnType<Struct['coercer']> {\n return selector(value).coercer(value, context);\n },\n\n validator(value, context): ReturnType<Struct['validator']> {\n // This only validates the root of the struct, entries does the rest of\n // the work.\n return selector(value).validator(value, context);\n },\n });\n}\n"]}
|
package/dist/superstruct.d.cts
CHANGED
|
@@ -34,13 +34,22 @@ export type ObjectType<Schema extends ObjectSchema> = Simplify<ExactOptionalize<
|
|
|
34
34
|
[K in keyof Schema]: Infer<Schema[K]>;
|
|
35
35
|
}>>>;
|
|
36
36
|
/**
|
|
37
|
-
* Change the return type of a superstruct object
|
|
38
|
-
* optional properties.
|
|
37
|
+
* Change the return type of a superstruct's `object` function to support
|
|
38
|
+
* exact optional properties.
|
|
39
39
|
*
|
|
40
40
|
* @param schema - The object schema.
|
|
41
41
|
* @returns A struct representing an object with a known set of properties.
|
|
42
42
|
*/
|
|
43
43
|
export declare function object<Schema extends ObjectSchema>(schema: Schema): Struct<ObjectType<Schema>, Schema>;
|
|
44
|
+
/**
|
|
45
|
+
* Change the return type of a superstruct's `type` function to support
|
|
46
|
+
* exact optional properties.
|
|
47
|
+
*
|
|
48
|
+
* @param schema - The object schema.
|
|
49
|
+
* @returns A struct representing an object with a known set of properties
|
|
50
|
+
* and ignore unknown properties.
|
|
51
|
+
*/
|
|
52
|
+
export declare function type<Schema extends ObjectSchema>(schema: Schema): Struct<ObjectType<Schema>, Schema>;
|
|
44
53
|
/**
|
|
45
54
|
* Augment a struct to allow exact-optional values. Exact-optional values can
|
|
46
55
|
* be omitted but cannot be `undefined`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.d.cts","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"superstruct.d.cts","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EAIP,8BAA8B;AAC/B,OAAO,KAAK,EACV,KAAK,EAEL,YAAY,EACZ,MAAM,EACN,WAAW,EACX,MAAM,EACN,QAAQ,EACR,SAAS,EACV,8BAA8B;AAE/B,OAAO,KAAK,EAAE,MAAM,EAAE,oBAAgB;AAGtC,OAAO,CAAC,MAAM,mBAAmB,EAAE,OAAO,MAAM,CAAC;AA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I,EAAE,MAAM,EACrC,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,EAC5B,OAAO,CAAC,EAAE,MAAM,GACf,IAAI,CAGN;AAED;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,CACrB,UAAU,SAAS,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EACnC,YAAY,IACV,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,YAAY,CAAC,SAAS,IAAI,GACpD,KAAK,CAAC,UAAU,CAAC,GACjB,KAAK,CAAC;AAEV;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,QAAQ,SAAS,CAAC,KAAK,EAAE,GAAG,KAAK,SAAS,EACvE,QAAQ,EAAE,QAAQ,GACjB,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,CAuB3C"}
|
package/dist/superstruct.d.mts
CHANGED
|
@@ -34,13 +34,22 @@ export type ObjectType<Schema extends ObjectSchema> = Simplify<ExactOptionalize<
|
|
|
34
34
|
[K in keyof Schema]: Infer<Schema[K]>;
|
|
35
35
|
}>>>;
|
|
36
36
|
/**
|
|
37
|
-
* Change the return type of a superstruct object
|
|
38
|
-
* optional properties.
|
|
37
|
+
* Change the return type of a superstruct's `object` function to support
|
|
38
|
+
* exact optional properties.
|
|
39
39
|
*
|
|
40
40
|
* @param schema - The object schema.
|
|
41
41
|
* @returns A struct representing an object with a known set of properties.
|
|
42
42
|
*/
|
|
43
43
|
export declare function object<Schema extends ObjectSchema>(schema: Schema): Struct<ObjectType<Schema>, Schema>;
|
|
44
|
+
/**
|
|
45
|
+
* Change the return type of a superstruct's `type` function to support
|
|
46
|
+
* exact optional properties.
|
|
47
|
+
*
|
|
48
|
+
* @param schema - The object schema.
|
|
49
|
+
* @returns A struct representing an object with a known set of properties
|
|
50
|
+
* and ignore unknown properties.
|
|
51
|
+
*/
|
|
52
|
+
export declare function type<Schema extends ObjectSchema>(schema: Schema): Struct<ObjectType<Schema>, Schema>;
|
|
44
53
|
/**
|
|
45
54
|
* Augment a struct to allow exact-optional values. Exact-optional values can
|
|
46
55
|
* be omitted but cannot be `undefined`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.d.mts","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"superstruct.d.mts","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EAIP,8BAA8B;AAC/B,OAAO,KAAK,EACV,KAAK,EAEL,YAAY,EACZ,MAAM,EACN,WAAW,EACX,MAAM,EACN,QAAQ,EACR,SAAS,EACV,8BAA8B;AAE/B,OAAO,KAAK,EAAE,MAAM,EAAE,oBAAgB;AAGtC,OAAO,CAAC,MAAM,mBAAmB,EAAE,OAAO,MAAM,CAAC;AAEjD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,EAAE,OAAO,mBAAmB,CAAC;CAClC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,IAAI;KAClC,CAAC,IAAI,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;CACxC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,MAAM,IAAI,MAAM,CAC1D,MAAM,EACN,gBAAgB,CACjB,GACC,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC;AAE3E;;GAEG;AACH,MAAM,MAAM,UAAU,CAAC,MAAM,SAAS,YAAY,IAAI,QAAQ,CAC5D,gBAAgB,CAAC,WAAW,CAAC;KAAG,CAAC,IAAI,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,CA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, assert, object as stObject } from "@metamask/superstruct";
|
|
1
|
+
import { Struct, assert, object as stObject, type as stType } from "@metamask/superstruct";
|
|
2
2
|
/**
|
|
3
|
-
* Change the return type of a superstruct object
|
|
4
|
-
* optional properties.
|
|
3
|
+
* Change the return type of a superstruct's `object` function to support
|
|
4
|
+
* exact optional properties.
|
|
5
5
|
*
|
|
6
6
|
* @param schema - The object schema.
|
|
7
7
|
* @returns A struct representing an object with a known set of properties.
|
|
@@ -9,6 +9,17 @@ import { Struct, assert, object as stObject } from "@metamask/superstruct";
|
|
|
9
9
|
export function object(schema) {
|
|
10
10
|
return stObject(schema);
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Change the return type of a superstruct's `type` function to support
|
|
14
|
+
* exact optional properties.
|
|
15
|
+
*
|
|
16
|
+
* @param schema - The object schema.
|
|
17
|
+
* @returns A struct representing an object with a known set of properties
|
|
18
|
+
* and ignore unknown properties.
|
|
19
|
+
*/
|
|
20
|
+
export function type(schema) {
|
|
21
|
+
return stType(schema);
|
|
22
|
+
}
|
|
12
23
|
/**
|
|
13
24
|
* Check if the current property is present in its parent object.
|
|
14
25
|
*
|
package/dist/superstruct.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superstruct.mjs","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"superstruct.mjs","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,MAAM,EACN,MAAM,IAAI,QAAQ,EAClB,IAAI,IAAI,MAAM,EACf,8BAA8B;AAwD/B;;;;;;GAMG;AACH,MAAM,UAAU,MAAM,CACpB,MAAc;IAEd,OAAO,QAAQ,CAAC,MAAM,CAAQ,CAAC;AACjC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,IAAI,CAClB,MAAc;IAEd,OAAO,MAAM,CAAC,MAAM,CAAQ,CAA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AO,CAAC,KAAK,EAAE,OAAO;YACrB,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACjD,CAAC;QAED,OAAO,CAAC,KAAK,EAAE,OAAO;YACpB,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACjD,CAAC;QAED,SAAS,CAAC,KAAK,EAAE,OAAO;YACtB,uEAAuE;YACvE,YAAY;YACZ,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACnD,CAAC;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n Struct,\n assert,\n object as stObject,\n type as stType,\n} from '@metamask/superstruct';\nimport type {\n Infer,\n Context,\n ObjectSchema,\n OmitBy,\n Optionalize,\n PickBy,\n Simplify,\n AnyStruct,\n} from '@metamask/superstruct';\n\nimport type { Equals } from './types';\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\ndeclare const ExactOptionalSymbol: unique symbol;\n\nexport type ExactOptionalTag = {\n type: typeof ExactOptionalSymbol;\n};\n\n/**\n * Exclude type `Type` from the properties of `Obj`.\n *\n * ```ts\n * type Foo = { a: string | null; b: number };\n * type Bar = ExcludeType<Foo, null>;\n * // Bar = { a: string, b: number }\n * ```\n */\nexport type ExcludeType<Obj, Type> = {\n [K in keyof Obj]: Exclude<Obj[K], Type>;\n};\n\n/**\n * Make optional all properties that have the `ExactOptionalTag` type.\n *\n * ```ts\n * type Foo = { a: string | ExactOptionalTag; b: number};\n * type Bar = ExactOptionalize<Foo>;\n * // Bar = { a?: string; b: number}\n * ```\n */\nexport type ExactOptionalize<Schema extends object> = OmitBy<\n Schema,\n ExactOptionalTag\n> &\n Partial<ExcludeType<PickBy<Schema, ExactOptionalTag>, ExactOptionalTag>>;\n\n/**\n * Infer a type from an superstruct object schema.\n */\nexport type ObjectType<Schema extends ObjectSchema> = Simplify<\n ExactOptionalize<Optionalize<{ [K in keyof Schema]: Infer<Schema[K]> }>>\n>;\n\n/**\n * Change the return type of a superstruct's `object` function to support\n * exact optional properties.\n *\n * @param schema - The object schema.\n * @returns A struct representing an object with a known set of properties.\n */\nexport function object<Schema extends ObjectSchema>(\n schema: Schema,\n): Struct<ObjectType<Schema>, Schema> {\n return stObject(schema) as any;\n}\n\n/**\n * Change the return type of a superstruct's `type` function to support\n * exact optional properties.\n *\n * @param schema - The object schema.\n * @returns A struct representing an object with a known set of properties\n * and ignore unknown properties.\n */\nexport function type<Schema extends ObjectSchema>(\n schema: Schema,\n): Struct<ObjectType<Schema>, Schema> {\n return stType(schema) as any;\n}\n\n/**\n * Check if the current property is present in its parent object.\n *\n * @param ctx - The context to check.\n * @returns `true` if the property is present, `false` otherwise.\n */\nfunction hasOptional(ctx: Context): boolean {\n const property: string = ctx.path[ctx.path.length - 1];\n const parent: Record<string, unknown> = ctx.branch[ctx.branch.length - 2];\n\n return property in parent;\n}\n\n/**\n * Augment a struct to allow exact-optional values. Exact-optional values can\n * be omitted but cannot be `undefined`.\n *\n * ```ts\n * const foo = object({ bar: exactOptional(string()) });\n * type Foo = Infer<typeof foo>;\n * // Foo = { bar?: string }\n * ```\n *\n * @param struct - The struct to augment.\n * @returns The augmented struct.\n */\nexport function exactOptional<Type, Schema>(\n struct: Struct<Type, Schema>,\n): Struct<Type | ExactOptionalTag, Schema> {\n return new Struct({\n ...struct,\n\n validator: (value, ctx) =>\n !hasOptional(ctx) || struct.validator(value, ctx),\n\n refiner: (value, ctx) =>\n !hasOptional(ctx) || struct.refiner(value as Type, ctx),\n });\n}\n\n/**\n * Assert that a value is valid according to a struct.\n *\n * It is similar to superstruct's mask function, but it does not ignore extra\n * properties.\n *\n * @param value - Value to check.\n * @param struct - Struct to validate the value against.\n * @param message - Error message to throw if the value is not valid.\n * @returns The value if it is valid.\n */\nexport function strictMask<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n message?: string,\n): Type {\n assert(value, struct, message);\n return value;\n}\n\n/**\n * Extracts the type from a struct definition and asserts that it matches the\n * expected type. If the types do not match, the type `never` is returned.\n *\n * @param StructType - The struct type to infer.\n * @param ExpectedType - The expected type.\n */\nexport type InferEquals<\n StructType extends Struct<any, any>,\n ExpectedType,\n> = Equals<Infer<StructType>, ExpectedType> extends true\n ? Infer<StructType>\n : never;\n\n/**\n * Create a custom union struct that uses a `selector` function for choosing\n * the validation path.\n *\n * @param selector - The selector function choosing the struct to validate with.\n * @returns The `superstruct` struct, which validates that the value satisfies\n * one of the structs.\n */\nexport function selectiveUnion<Selector extends (value: any) => AnyStruct>(\n selector: Selector,\n): Struct<Infer<ReturnType<Selector>>, null> {\n return new Struct({\n type: 'union',\n schema: null,\n\n *entries(value: any, context: any): ReturnType<Struct['entries']> {\n yield* selector(value).entries(value, context);\n },\n\n *refiner(value, context): ReturnType<Struct['refiner']> {\n yield* selector(value).refiner(value, context);\n },\n\n coercer(value, context): ReturnType<Struct['coercer']> {\n return selector(value).coercer(value, context);\n },\n\n validator(value, context): ReturnType<Struct['validator']> {\n // This only validates the root of the struct, entries does the rest of\n // the work.\n return selector(value).validator(value, context);\n },\n });\n}\n"]}
|