UNPKG

apiful

Version:
111 lines (97 loc) 3.65 kB
import type { JSONSchema7 } from 'json-schema'; type JsonObject = { [Key in string]: JsonValue; } & { [Key in string]?: JsonValue | undefined; }; type JsonArray = JsonValue[] | readonly JsonValue[]; type JsonPrimitive = string | number | boolean | null; type JsonValue = JsonPrimitive | JsonObject | JsonArray; interface TypeDefinitionOptions { /** @default 'Root' */ typeName?: string; /** @default false */ strictProperties?: boolean; } type ResolvedTypeDefinitionOptions = Required<TypeDefinitionOptions>; declare function jsonToTypeDefinition(data: JsonValue, options?: TypeDefinitionOptions): Promise<string>; declare const validatorSymbol: unique symbol; type ValidationResult<T, E = Error> = { success: true; value: T; } | { success: false; error: E; }; interface Validator<T = unknown> { [validatorSymbol]: true; /** * Optional. Validates that the structure of a value matches this schema, * and returns a typed version of the value if it does. */ readonly validate?: (value: unknown) => ValidationResult<T>; } declare class TypeValidationError extends Error { readonly value: unknown; readonly cause?: unknown; constructor({ value, cause, }: { value: unknown; cause?: unknown; }); } /** * Create a validator. * * @param validate A validation function for the schema. */ declare function validator<T>(validate?: ((value: unknown) => ValidationResult<T>)): Validator<T>; /** * Validates the types of an unknown object using a schema and * return a strongly-typed object. * * @template T - The type of the object to validate. * @param {string} options.value - The object to validate. * @param {Validator<T>} options.schema - The schema to use for validating the JSON. * @returns {T} - The typed object. */ declare function validateTypes<T>({ value, schema: inputSchema, }: { value: unknown; schema: Validator<T>; }): T; /** * Safely validates the types of an unknown object using a schema and * return a strongly-typed object. * * @template T - The type of the object to validate. * @param {string} options.value - The JSON object to validate. * @param {Validator<T>} options.schema - The schema to use for validating the JSON. * @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object. */ declare function safeValidateTypes<T>({ value, schema, }: { value: unknown; schema: Validator<T>; }): ValidationResult<T, TypeValidationError>; declare function isValidator(value: unknown): value is Validator; declare const schemaSymbol: unique symbol; interface Schema<T = unknown> extends Validator<T> { [schemaSymbol]: true; /** * Schema type for inference. */ _type: T; /** * The JSON Schema for the schema. */ readonly jsonSchema: JSONSchema7; } /** * Create a schema using a JSON Schema. */ declare function jsonSchema<T = unknown>(jsonSchema: JSONSchema7, { validate }?: { validate?: (value: unknown) => ValidationResult<T>; }): Schema<T>; declare function isSchema(value: unknown): value is Schema; declare function base64ToUint8Array(base64String: string): Uint8Array<ArrayBuffer>; declare function uint8ArrayToBase64(array: Uint8Array): string; export { TypeValidationError, base64ToUint8Array, isSchema, isValidator, jsonSchema, jsonToTypeDefinition, safeValidateTypes, uint8ArrayToBase64, validateTypes, validator, validatorSymbol }; export type { JsonArray, JsonObject, JsonPrimitive, JsonValue, ResolvedTypeDefinitionOptions, Schema, TypeDefinitionOptions, ValidationResult, Validator };