UNPKG

awesome-ajv-errors

Version:
47 lines (46 loc) 2.04 kB
import type { ErrorObject } from "ajv"; import type { ParsedJson } from "jsonpos"; import type { DataPath } from "./data-path.js"; import type { StyleManager } from "./style/interface.js"; import type { PrintCode } from "./code/types.js"; import type { ErrorParameters, TypeParams } from "./ajv-types.js"; export interface ErrorContext { } export type ValidationError = ErrorObject & { typeErrors?: Array<SpecificAjvError<TypeParams>>; }; export interface PrettificationCore { errors: ValidationError | ArrayLike<ValidationError>; schema: {}; data: any; } export interface PrettifyContext<Params = any> extends PrettificationCore { styleManager: StyleManager; printCode: PrintCode; error: SpecificAjvError<Params>; dataPath: DataPath; parsedJson: ParsedJson; } export interface PrettyResult { title: string; codeFrame?: string; } export type CustomPrettify = (context: PrettifyContext) => PrettyResult; export type EnsureErrorType<T> = T extends ErrorParameters ? T : never; export type SpecificAjvError<T> = Omit<ValidationError, 'params'> & { params: T; }; export declare function getError<T>(context: PrettifyContext): SpecificAjvError<EnsureErrorType<T>>; export declare function getTypedContext<T>(context: PrettifyContext): PrettifyContext<EnsureErrorType<T>>; export type SimpleValueType = null | boolean | number | string; export type SimpleValueTypeName = 'null' | 'boolean' | 'number' | 'string'; export interface TypedValue { value: SimpleValueType; type: 'boolean' | 'number' | 'null' | 'string' | 'object' | 'array'; isSimple: boolean; } export declare const getValueType: (value: SimpleValueType) => TypedValue['type']; export declare const getTypedValue: (value: any) => TypedValue; export declare const getTypedValueKey: ({ type, value }: TypedValue) => string; export declare const isSimpleValue: <T extends SimpleValueType>(possible: any) => possible is T; export declare function suggestAnotherType(value: any, toType: SimpleValueTypeName): TypedValue | undefined;