@nrfcloud/ts-json-schema-transformer
Version:
A TypeScript transformer that generates JSON schemas and validators from TypeScript interfaces
130 lines (129 loc) • 4.83 kB
TypeScript
import type { JSONSchemaType, ValidateFunction } from "ajv";
/**
* Get the JSON schema for the provided type
* @transformer ts-json-schema-transformer
*/
export declare function getSchema<T>(): JSONSchemaType<T>;
/**
* Validate an object according to the provided type
* Returns the object back if it is valid or undefined if it is not
* @transformer ts-json-schema-transformer
*/
export declare function validate<T>(_obj: unknown): T | undefined;
/**
* Get a reusable validator for the provided type
* Returns the object back if it is valid or undefined if it is not
* @transformer ts-json-schema-transformer
*/
export declare function createValidateFn<T>(): (obj: unknown) => T | undefined;
/**
* Validate an object according to the provided type
* Acts as a type guard
* @transformer ts-json-schema-transformer
*/
export declare function guard<T>(_obj: unknown): ReturnType<ValidateFunction<T>>;
/**
* Get a reusable validator for the provided type
* This function is an AJV validator, so errors are stored in the `errors` property
* Acts as a type guard
* @transformer ts-json-schema-transformer
*/
export declare function createGuardFn<T>(): ValidateFunction<T>;
/**
* Get a validator for the provided type
* @deprecated use `createValidateFn`
* @transformer ts-json-schema-transformer
*/
export declare function createValidator<T>(): ValidateFunction<T>;
/**
* Get a mock object for the provided type
* @deprecated use `mock`
* @transformer ts-json-schema-transformer
*/
export declare function getMockObject<T, const Seed extends string = "none">(): IfEquals<Seed, string, never, T>;
/**
* Get a mock object for the provided type
* @transformer ts-json-schema-transformer
*/
export declare function mock<T, const Seed extends string = "none">(): IfEquals<Seed, string, never, T>;
/**
* @transformer ts-json-schema-transformer
*/
export declare function createMockFn<T, const Seed extends string = "none">(): () => IfEquals<Seed, string, never, T>;
/**
* Assert that an object is valid according to the provided type
* Acts as a type guard
* @deprecated use `assertGuard`
* @transformer ts-json-schema-transformer
*/
export declare function assertValid<T = never>(_obj: unknown): asserts _obj is T;
/**
* Assert that an object is valid according to the provided type
* Acts as a type guard
* @transformer ts-json-schema-transformer
*/
export declare function assertGuard<T = never>(_obj: unknown): asserts _obj is T;
/**
* Create an assert guard function for the provided type
* Acts as a type guard
* @transformer ts-json-schema-transformer
*/
export declare function createAssertGuardFn<T = never>(): GuardFn<T>;
/**
* Assert that an object is valid and returns the object back
* @transformer ts-json-schema-transformer
*/
export declare function assert<T = never>(_obj: unknown): T;
/**
* Create an assert function for the provided type
* @transformer ts-json-schema-transformer
*/
export declare function createAssertFn<T = never>(): (_obj: unknown) => T;
/**
* Parse a string into the provided type
* Returns undefined if the string is not valid
* @transformer ts-json-schema-transformer
*/
export declare function parse<T>(_input: string): T | undefined;
/**
* Create a parse function for the provided type
* Returns undefined if the string is not valid
* @transformer ts-json-schema-transformer
*/
export declare function createParseFn<T>(): (input: string) => T | undefined;
/**
* Assert that a string can be parsed into the provided type, then returns it
* @transformer ts-json-schema-transformer
*/
export declare function assertParse<T>(_input: string): T;
/**
* Create an assert parse function for the provided type
* @transformer ts-json-schema-transformer
*/
export declare function createAssertParseFn<T>(): (input: string) => T;
export type GuardFn<T> = (obj: unknown) => asserts obj is T;
export declare class ValidationError extends Error {
}
type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T ? 1 : 2) extends (<G>() => G extends U ? 1 : 2) ? Y : N;
declare const _default: {
getSchema: typeof getSchema;
validate: typeof validate;
createValidateFn: typeof createValidateFn;
guard: typeof guard;
createGuardFn: typeof createGuardFn;
createValidator: typeof createValidator;
getMockObject: typeof getMockObject;
mock: typeof mock;
createMockFn: typeof createMockFn;
assertValid: typeof assertValid;
assertGuard: typeof assertGuard;
createAssertGuardFn: typeof createAssertGuardFn;
assert: typeof assert;
createAssertFn: typeof createAssertFn;
parse: typeof parse;
createParseFn: typeof createParseFn;
assertParse: typeof assertParse;
createAssertParseFn: typeof createAssertParseFn;
ValidationError: typeof ValidationError;
};
export default _default;