// Copyright 2019 @polkadot/extension-ui authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. 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 const allOf = (...validators: Validator[]): Validator => 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 const isNotShorterThan = (minLength: number, errorText: string): Validator => (value: string): Result => { if (value.length < minLength) { return Result.error(errorText); } return Result.ok(value); }; export const isSameAs = (expectedValue: T, errorText: string): Validator => (value: T): Result => { if (value !== expectedValue) { return Result.error(errorText); } return Result.ok(value); };