This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsuperstruct.ts
154 lines (141 loc) · 3.76 KB
/
superstruct.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import {
Struct,
assert,
define,
object as stObject,
} from '@metamask/superstruct';
import type {
Infer,
Context,
ObjectSchema,
OmitBy,
Optionalize,
PickBy,
Simplify,
} from '@metamask/superstruct';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
declare const ExactOptionalSymbol: unique symbol;
export type ExactOptionalTag = {
type: typeof ExactOptionalSymbol;
};
/**
* Exclude type `Type` from the properties of `Obj`.
*
* ```ts
* type Foo = { a: string | null; b: number };
* type Bar = ExcludeType<Foo, null>;
* // Bar = { a: string, b: number }
* ```
*/
export type ExcludeType<Obj, Type> = {
[K in keyof Obj]: Exclude<Obj[K], Type>;
};
/**
* Make optional all properties that have the `ExactOptionalTag` type.
*
* ```ts
* type Foo = { a: string | ExactOptionalTag; b: number};
* type Bar = ExactOptionalize<Foo>;
* // Bar = { a?: string; b: number}
* ```
*/
export type ExactOptionalize<Schema extends object> = OmitBy<
Schema,
ExactOptionalTag
> &
Partial<ExcludeType<PickBy<Schema, ExactOptionalTag>, ExactOptionalTag>>;
/**
* Infer a type from an superstruct object schema.
*/
export type ObjectType<Schema extends ObjectSchema> = Simplify<
ExactOptionalize<Optionalize<{ [K in keyof Schema]: Infer<Schema[K]> }>>
>;
/**
* Change the return type of a superstruct object struct to support exact
* optional properties.
*
* @param schema - The object schema.
* @returns A struct representing an object with a known set of properties.
*/
export function object<Schema extends ObjectSchema>(
schema: Schema,
): Struct<ObjectType<Schema>, Schema> {
return stObject(schema) as any;
}
/**
* Check if the current property is present in its parent object.
*
* @param ctx - The context to check.
* @returns `true` if the property is present, `false` otherwise.
*/
function hasOptional(ctx: Context): boolean {
const property: string = ctx.path[ctx.path.length - 1];
const parent: Record<string, unknown> = ctx.branch[ctx.branch.length - 2];
return property in parent;
}
/**
* Augment a struct to allow exact-optional values. Exact-optional values can
* be omitted but cannot be `undefined`.
*
* ```ts
* const foo = object({ bar: exactOptional(string()) });
* type Foo = Infer<typeof foo>;
* // Foo = { bar?: string }
* ```
*
* @param struct - The struct to augment.
* @returns The augmented struct.
*/
export function exactOptional<Type, Schema>(
struct: Struct<Type, Schema>,
): Struct<Type | ExactOptionalTag, Schema> {
return new Struct({
...struct,
validator: (value, ctx) =>
!hasOptional(ctx) || struct.validator(value, ctx),
refiner: (value, ctx) =>
!hasOptional(ctx) || struct.refiner(value as Type, ctx),
});
}
/**
* Defines a new string-struct matching a regular expression.
*
* Example:
*
* ```ts
* const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);
* ```
*
* @param name - Type name.
* @param pattern - Regular expression to match.
* @returns A new string-struct that matches the given pattern.
*/
export function definePattern(
name: string,
pattern: RegExp,
): Struct<string, null> {
return define<string>(
name,
(value: unknown): boolean =>
typeof value === 'string' && pattern.test(value),
);
}
/**
* Assert that a value is valid according to a struct.
*
* It is similar to superstruct's mask function, but it does not ignore extra
* properties.
*
* @param value - Value to check.
* @param struct - Struct to validate the value against.
* @param message - Error message to throw if the value is not valid.
* @returns The value if it is valid.
*/
export function strictMask<Type, Schema>(
value: unknown,
struct: Struct<Type, Schema>,
message?: string,
): Type {
assert(value, struct, message);
return value;
}