UNPKG

ts-data-objects

Version:

Simple lightweight library for enhancing plain JS objects with TypeScript wrappers enabling easy API data assertion and validation

118 lines (105 loc) 3.66 kB
/** * Scaffolder for valid${Type} function * @see {@link DeepGuardAssertionFunction} * @see {@link DeepGuardInnerAssertionFunction} * * @category Experimental Implementation */ declare type AssertionFunction<Type extends object, TypeName extends string> = { [DataTypeName in TypeName as `valid${DataTypeName}`]: DeepGuardAssertionFunction<Type>; }; /** * Type signature for data object constructor functions. * Creates a function that accepts partial data and returns complete typed objects. * @template T - The object type to be constructed * @param [o] - Optional partial input data * @returns A complete object of type T * @category Core Implementation */ declare type DataConstructor<T extends object> = (o?: Expect<T>) => T; /** * data deep functionality * @category Experimental Implementation * * @param typeName * @param modelRules * @returns */ export declare const dataDeepGuard: <Type extends object, TypeName extends string>(typeName: TypeName, modelRules: DataModelRules<Type>) => DataModelGuards<Type, TypeName>; /** * ... * @category Experimental Implementation * * @param dataType * @param constructorFunction * @param validationFunction * @returns */ export declare const dataDeepParser: <T extends object>(dataType: string, constructorFunction: DataConstructor<T>, validationFunction: DeepGuardAssertionFunction<T>) => (data?: Expect<T>) => T; /** * TODO:? * holder of guard and validator function * * @category Experimental Implementation */ declare type DataModelGuards<Type extends object, TypeName extends string> = IsFunction<Type, TypeName> & AssertionFunction<Type, TypeName> & DataModelRulesObject<Type, TypeName>; /** * TODO: Single rule * * Named tuple * * @category Experimental Implementation */ declare type DataModelRule = [ type: 'string' | 'number' | 'boolean' | DataModelRules<any>, array?: boolean ]; /** * TODO:... * @category Experimental Implementation */ declare type DataModelRules<T extends object> = // Either Array of rules, or mapped object of rules { [K in keyof T]: DataModelRule; }; /** * Scaffolder for ${Type}Rules object. * This object is basically just a convenience returning of the same object passed into DeepGuard generator. * @see {@link DataModelRules} * * @category Experimental Implementation */ declare type DataModelRulesObject<Type extends object, TypeName extends string> = { [DataTypeName in TypeName as `${DataTypeName}Rules`]: DataModelRules<Type>; }; /** * TODO: ... * (tbd) return undefined if validation passed, otherwise return self-ref * usable by parser to determine innermost problem of data model * @category Experimental Implementation */ declare type DeepGuardAssertionFunction<T extends object> = (o?: Expect<T>) => string | undefined; /** * TODO~ * @category Experimental Implementation */ declare type DeepGuardIsFunction<T extends object> = (dataObject: Expect<T>) => dataObject is T; /** * A semantic alias for TypeScript's `Partial<T>` utility type. * Makes code more readable by explicitly indicating expected partial input. * * @template T - The type to make partial * @category Common Utils */ declare type Expect<T> = Partial<T>; /** * Scaffolder for is${Type} function. * @see {@link DeepGuardIsFunction} * @see {@link DeepGuardInnerIsFunction} * * @category Experimental Implementation */ declare type IsFunction<Type extends object, TypeName extends string> = { [DataTypeName in TypeName as `is${DataTypeName}`]: DeepGuardIsFunction<Type>; }; export { }