UNPKG

@metamask/keyring-utils

Version:
104 lines 3.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.object = object; exports.type = type; exports.exactOptional = exactOptional; exports.strictMask = strictMask; exports.selectiveUnion = selectiveUnion; const superstruct_1 = require("@metamask/superstruct"); /** * Change the return type of a superstruct's `object` function to support * exact optional properties. * * @param schema - The object schema. * @returns A struct representing an object with a known set of properties. */ function object(schema) { return (0, superstruct_1.object)(schema); } /** * Change the return type of a superstruct's `type` function to support * exact optional properties. * * @param schema - The object schema. * @returns A struct representing an object with a known set of properties * and ignore unknown properties. */ function type(schema) { return (0, superstruct_1.type)(schema); } /** * 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) { const property = ctx.path[ctx.path.length - 1]; const parent = 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. */ function exactOptional(struct) { return new superstruct_1.Struct({ ...struct, validator: (value, ctx) => !hasOptional(ctx) || struct.validator(value, ctx), refiner: (value, ctx) => !hasOptional(ctx) || struct.refiner(value, ctx), }); } /** * 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. */ function strictMask(value, struct, message) { (0, superstruct_1.assert)(value, struct, message); return value; } /** * Create a custom union struct that uses a `selector` function for choosing * the validation path. * * @param selector - The selector function choosing the struct to validate with. * @returns The `superstruct` struct, which validates that the value satisfies * one of the structs. */ function selectiveUnion(selector) { return new superstruct_1.Struct({ type: 'union', schema: null, *entries(value, context) { yield* selector(value).entries(value, context); }, *refiner(value, context) { yield* selector(value).refiner(value, context); }, coercer(value, context) { return selector(value).coercer(value, context); }, validator(value, context) { // This only validates the root of the struct, entries does the rest of // the work. return selector(value).validator(value, context); }, }); } //# sourceMappingURL=superstruct.cjs.map