/** * Copyright 2022 Gravwell, Inc. All rights reserved. * * Contact: [legal@gravwell.io](mailto:legal@gravwell.io) * * This software may be modified and distributed under the terms of the MIT * license. See the LICENSE file for details. */ import { Decoder } from 'decoders'; import { Annotation } from 'decoders/annotate'; import { DecoderType, Scalar } from 'decoders/Decoder'; import { ObjectDecoderType } from 'decoders/lib/objects'; import { DecoderTypes, Values } from 'decoders/lib/unions'; import { Instance, Klass } from 'decoders/lib/utilities'; /** * A `Verifier` is a decoder that performs no transforms to the value it * inspects. * * That is, a `Verifier` can tell you if it's possible to decode an unknown * value `V` into type `T` without changing the type of `V`. As such, a * `Verifier` can work as a `Decoder` AND as a type guard for type `T`. */ export interface Verifier extends Decoder { /** * Returns true if `value` can be decoded to a `T`, otherwise false. * * Because `guard` asserts that `value` already _is_ a `T` (that is, it can be * decoded to a `T` with no type transforms), `guard` can safely be used as a * type guard for type `T`. */ guard(value: unknown): value is T; /** * Adds an extra rejection predicate to the Verifier. * * @see {@link https://decoders.cc/Decoder.html#reject} */ rejectVerifier(rejectFn: (value: T) => string | Annotation | null): Verifier; /** * Adds an extra acceptance predicate to the Verifier. * * @see {@link https://decoders.cc/Decoder.html#refine} */ refineVerifier(predicate: (value: T) => value is N, msg: string): Verifier; refineVerifier(predicate: (value: T) => boolean, msg: string): Verifier; /** * Changes the error message of the Verifier. * * @see {@link https://decoders.cc/Decoder.html#describe} */ describeVerifier(message: string): Verifier; } export declare const string: Verifier; export declare const regex: (r: RegExp, msg: string) => Verifier; export declare const iso8601String: Verifier; export declare const nonEmptyString: Verifier; export declare const number: Verifier; export declare const integer: Verifier; export declare const boolean: Verifier; export declare const null_: Verifier; export declare const undefined_: Verifier; export declare const unknown: Verifier; export declare const mixed: Verifier; export declare const jsonObject: Verifier; export declare const jsonArray: Verifier; export declare const json: Verifier; export declare const instanceOf: >(klass: K) => Verifier>; export declare const constant: (c: T) => Verifier; export declare const optional: (g: Verifier) => Verifier; export declare const nullable: (g: Verifier) => Verifier; export declare const maybe: (g: Verifier) => Verifier; export declare const array: (g: Verifier) => Verifier; export declare const nonEmptyArray: (g: Verifier) => Verifier<[T, ...T[]]>; export declare const tuple1: (a: Verifier) => Verifier<[A]>; export declare const tuple2: (a: Verifier, b: Verifier) => Verifier<[A, B]>; export declare const tuple3: (a: Verifier, b: Verifier, c: Verifier) => Verifier<[A, B, C]>; export declare const tuple4: (a: Verifier, b: Verifier, c: Verifier, d: Verifier) => Verifier<[A, B, C, D]>; export declare const tuple5: (a: Verifier, b: Verifier, c: Verifier, d: Verifier, e: Verifier) => Verifier<[A, B, C, D, E]>; export declare const exact: >>(VerifiersByKey: O) => Verifier extends infer T ? { [K in keyof T]: ObjectDecoderType[K]; } : never>; export declare const object: >>(VerifiersByKey: O) => Verifier extends infer T ? { [K in keyof T]: ObjectDecoderType[K]; } : never>; export declare const inexact: >>(VerifiersByKey: O) => Verifier<(ObjectDecoderType extends infer T ? { [K in keyof T]: ObjectDecoderType[K]; } : never) & Record>; export declare const dict: (v: Verifier) => Verifier>; export declare const either: []>(...vs: T) => Verifier>; export declare const oneOf: (constants: readonly T[]) => Verifier; export declare const taggedUnion: >>(field: string, mapping: O) => Verifier; }>>; export declare const lazy: (decoderFn: () => Verifier) => Verifier;