UNPKG

@uni-ts/model

Version:

Utils for creating data models in TypeScript.

42 lines 1.72 kB
import { type Merge } from './helpers.js'; import type { StandardSchemaV1 } from './standard-schema.js'; import type { Model } from './types.js'; /** * Creates a type-safe data model based on schema from any Standard Schema compatible validation library. * * @template S - Type of the Standard Schema compatible validation schema * @param schema - A validation schema that follows the Standard Schema interface * @returns A model object with validation utilities * * @example * ```typescript * import { z } from 'zod'; * * type User = InferModelOutput<typeof User>; // { name: string; email: string } * const User = createModel(z.object({ * name: z.string().min(1), * email: z.string().email(), * })); * * const user1 = User.from({ name: 'John', email: 'john@example.com' }); // User * const user2 = User.from({ name: '', email: '' }); // throws ModelValidationError * ``` * * @example * ```typescript * import { z } from 'zod'; * * type Email = InferModelOutput<typeof Email>; // string & z.$brand<'Email'> * const Email = createModel(z.string().email().brand('Email')); * * function sendEmail(email: Email) { * // TypeScript ensures only validated emails can be passed * } * ``` */ export declare function createModel<S extends StandardSchemaV1, const E extends Record<PropertyKey, unknown> = {}>(schema: S, extend?: E): Merge<Model<S>, E>; export declare function validate<S extends StandardSchemaV1>(schema: S, value: unknown): StandardSchemaV1.InferOutput<S>; export { derive } from './derive.js'; export { ModelValidationError, prettifyError } from './error.js'; export type { InferModelInput, InferModelOutput, Model } from './types.js'; //# sourceMappingURL=index.d.ts.map