/* * Copyright (c) 2022. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ export type ValidationResult = { success: true, value: T } | { success: false, message: string } export interface IValidator { go(value: unknown, key?: string) : ValidationResult; } type ResultCallback = (value: unknown, key?: string) => ValidationResult | IValidator | boolean; export type PropertyValidator = IValidator | ResultCallback; /** * Takes the type and requires all of its properties to be a PropertyValidator. */ export type Shape = Record>; export function isValidator(value: unknown): value is IValidator { // Check if the value has a .go function. If so, it's an IValidator return typeof (value as IValidator).go === "function"; } export function hasOwnProperty(obj: X, prop: Y) : obj is X & Record { return obj.hasOwnProperty(prop); } export function isString(val: unknown) : val is X { return typeof val === 'string'; } export function isBoolean(val: unknown) : val is X { return typeof val === 'boolean'; } export function isNumber(val: unknown) : val is X { return typeof val === 'number'; }