// Copyright 2019-2022 @polkadot/extension-ui authors & contributors // SPDX-License-Identifier: Apache-2.0 interface InputError { errorDescription: string; } export type Result = { error: InputError } | { ok: T }; export const Result = { error: (errorDescription: string): Result => ({ error: { errorDescription } }), isError (value: Result): value is ({ error: InputError }) { return Object.hasOwnProperty.call(value, 'error'); }, isOk (value: Result): value is ({ ok: T }) { return Object.hasOwnProperty.call(value, 'ok'); }, ok: (ok: T): Result => ({ ok }) }; export declare type Validator = (value: T) => Result | Promise>; export function allOf (...validators: Validator[]): Validator { return async (value: T): Promise> => { for (const validator of validators) { const validationResult = await validator(value); if (Result.isError(validationResult)) { return validationResult; } } return Result.ok(value); }; } export function isNotShorterThan (minLength: number, errorText: string): Validator { return (value: string): Result => { return value.length < minLength ? Result.error(errorText) : Result.ok(value); }; } export function isSameAs (expectedValue: T, errorText: string): Validator { return (value: T): Result => { return value !== expectedValue ? Result.error(errorText) : Result.ok(value); }; }