@beignet/core
Version:
Core framework primitives for Beignet
70 lines • 2.27 kB
TypeScript
import type { StandardSchemaV1 } from "@standard-schema/spec";
/**
* Any Standard Schema compatible validator.
*/
export type StandardSchema = StandardSchemaV1<unknown, unknown>;
/**
* Infer the parsed output type from a Standard Schema.
*/
export type InferOutput<T extends StandardSchemaV1> = StandardSchemaV1.InferOutput<T>;
/**
* Value object definition returned by `defineValueObject(...).build()`.
*/
export interface ValueObjectDef<Name extends string, Schema extends StandardSchema> {
/** Name used for debugging and introspection. */
name: Name;
/** Standard Schema used to validate values. */
schema: Schema;
/**
* Create a validated value from unknown input.
*
* This is async because Standard Schema validators may be async.
*/
create(input: unknown): Promise<InferOutput<Schema>>;
/**
* Check whether an input is valid for this value object.
*/
isValid(input: unknown): Promise<boolean>;
/** Type-only alias for the inferred TypeScript type. Undefined at runtime. */
Type: InferOutput<Schema>;
}
/**
* Builder class for creating Value Objects.
*/
declare class ValueObjectBuilder<Name extends string, Schema extends StandardSchema> {
private readonly cfg;
constructor(cfg: {
name: Name;
schema?: Schema;
});
/**
* Set the schema for this value object.
*/
schema<S extends StandardSchema>(s: S): ValueObjectBuilder<Name, S>;
/**
* Finalize the value object definition.
*/
build(): ValueObjectDef<Name, Schema>;
}
/**
* Create a new value object builder.
*
* Value objects are schema-backed primitives for domain concepts such as email
* addresses or money values. Beignet does not add a runtime brand; the schema
* output controls the runtime value.
*
* @example
* ```ts
* const Email = defineValueObject("Email")
* .schema(z.string().email())
* .build();
*
* type Email = typeof Email.Type;
*
* const email = await Email.create("test@example.com"); // OK
* const isValid = await Email.isValid("test@example.com"); // true
* ```
*/
export declare function defineValueObject<Name extends string>(name: Name): ValueObjectBuilder<Name, never>;
export {};
//# sourceMappingURL=value-object.d.ts.map