bguard
Version:
**bguard** is a powerful, flexible, and type-safe validation library for TypeScript. It allows developers to define validation schemas for their data structures and ensures that data conforms to the expected types and constraints.
57 lines (55 loc) • 1.76 kB
TypeScript
interface TranslationErrorMap {
'c:optional': string;
'c:nullable': string;
'c:array': string;
'c:objectType': string;
'c:objectTypeAsArray': string;
'c:unrecognizedProperty': string;
'c:requiredProperty': string;
'c:invalidType': string;
'c:isBoolean': string;
'c:date': string;
[val: string]: string;
}
type TypeMapping = {
number: number;
string: string;
boolean: boolean;
undefined: undefined;
object: object;
function: Function;
symbol: symbol;
bigint: bigint;
};
type MapMixTypes<T extends BaseType[]> = T extends (infer U)[] ? U extends keyof TypeMapping ? TypeMapping[U] : never : never;
interface ValidationErrorData {
message: string;
expected: unknown;
received: unknown;
pathToError: string;
meta?: MetaContext;
}
interface MetaContext {
id?: string;
description?: string;
}
type BaseType = 'number' | 'string' | 'boolean' | 'undefined' | 'object' | 'function' | 'symbol' | 'bigint';
type WithBGuardType<T, Y> = T & {
validation_bguard: Y;
};
type ExtractFromBGuardType<T> = T extends WithBGuardType<unknown, infer Y> ? Y : never;
type WithNull<T> = T & {
validation_null: true;
};
type WithUndefined<T> = T & {
validation_undefined: true;
};
type WithArray<T, Y> = T & {
validation_array: Y;
};
type ExtractFromArray<T> = T extends WithArray<unknown, infer X> ? X : never;
type WithObject<T, Y> = T & {
validation_object: Y;
};
type TransformCallback<In = unknown, Out = unknown> = (val: In) => Out;
export type { BaseType, ExtractFromArray, ExtractFromBGuardType, MapMixTypes, MetaContext, TransformCallback, TranslationErrorMap, ValidationErrorData, WithArray, WithBGuardType, WithNull, WithObject, WithUndefined };