@xylabs/zod
v5.0.87
Published
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
Downloads
26,895
Keywords
Readme
@xylabs/zod
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
Reference
@xylabs/zod
Interfaces
| Interface | Description | | ------ | ------ | | ZodFactoryConfigObject | Configuration object for zod factory functions, providing a name for error messages. |
Type Aliases
| Type Alias | Description | | ------ | ------ | | ZodFactoryConfig | Configuration for zod factory assertion behavior, either an AssertConfig or a named config object. | | AllZodFactories | - |
Functions
| Function | Description |
| ------ | ------ |
| zodAllFactory | Creates a bundle of is, as, and to factory functions for a given zod schema. |
| zodAsAsyncFactory | Creates an async function that validates a value against a zod schema and returns it with a narrowed type. Uses safeParseAsync for schemas with async refinements. When called without an assert config, returns undefined on failure. |
| zodAsFactory | Creates a function that validates a value against a zod schema and returns it with a narrowed type. When called without an assert config, returns undefined on failure. When called with an assert config, throws on failure. |
| zodIsFactory | Creates a type guard function that checks if a value matches a zod schema. |
| zodToAsyncFactory | Creates an async function that converts a value to the zod schema type, delegating to zodAsAsyncFactory internally. Provides overloads for optional assertion: without assert config resolves to undefined on failure, with assert config throws on failure. |
| zodToFactory | Creates a function that converts a value to the zod schema type, delegating to zodAsFactory internally. Provides overloads for optional assertion: without assert config returns undefined on failure, with assert config throws on failure. |
functions
zodAllFactory
function zodAllFactory<T, TName>(zod: ZodType<T>, name: TName): {
[key: string]: {
<T> (value: T): T & T | undefined;
<T> (value: T, assert: ZodFactoryConfig): T & T;
};
};Alpha
Creates a bundle of is, as, and to factory functions for a given zod schema.
Type Parameters
| Type Parameter |
| ------ |
| T |
| TName extends string |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| zod | ZodType<T> | The zod schema to validate against |
| name | TName | The name used to suffix the generated function names (e.g. 'Address' produces isAddress, asAddress, toAddress) |
Returns
{
[key: string]: {
<T> (value: T): T & T | undefined;
<T> (value: T, assert: ZodFactoryConfig): T & T;
};
}An object containing is<Name>, as<Name>, and to<Name> functions
zodAsAsyncFactory
function zodAsAsyncFactory<TZod>(zod: ZodType<TZod>, name: string): {
<T> (value: T): Promise<T & TZod | undefined>;
<T> (value: T, assert: ZodFactoryConfig): Promise<T & TZod>;
};Creates an async function that validates a value against a zod schema and returns it with a narrowed type.
Uses safeParseAsync for schemas with async refinements. When called without an assert config, returns undefined on failure.
Type Parameters
| Type Parameter |
| ------ |
| TZod |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| zod | ZodType<TZod> | The zod schema to validate against |
| name | string | A name used in error messages for identification |
Returns
An async function that validates and narrows the type of a value
<T>(value: T): Promise<T & TZod | undefined>;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
Promise<T & TZod | undefined>
<T>(value: T, assert: ZodFactoryConfig): Promise<T & TZod>;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| assert | ZodFactoryConfig |
Returns
Promise<T & TZod>
zodAsFactory
function zodAsFactory<TZod>(zod: ZodType<TZod>, name: string): {
<T> (value: T): T & TZod | undefined;
<T> (value: T, assert: ZodFactoryConfig): T & TZod;
};Creates a function that validates a value against a zod schema and returns it with a narrowed type. When called without an assert config, returns undefined on failure. When called with an assert config, throws on failure.
Type Parameters
| Type Parameter |
| ------ |
| TZod |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| zod | ZodType<TZod> | The zod schema to validate against |
| name | string | A name used in error messages for identification |
Returns
A function that validates and narrows the type of a value
<T>(value: T): T & TZod | undefined;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
T & TZod | undefined
<T>(value: T, assert: ZodFactoryConfig): T & TZod;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| assert | ZodFactoryConfig |
Returns
T & TZod
zodIsFactory
function zodIsFactory<TZod>(zod: ZodType<TZod>): <T>(value: T) => value is T & TZod;Creates a type guard function that checks if a value matches a zod schema.
Type Parameters
| Type Parameter |
| ------ |
| TZod |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| zod | ZodType<TZod> | The zod schema to validate against |
Returns
A type guard function that returns true if the value passes validation
<T>(value: T): value is T & TZod;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
value is T & TZod
zodToAsyncFactory
function zodToAsyncFactory<TZod>(zod: ZodType<TZod>, name: string): {
<T> (value: T): Promise<T & TZod | undefined>;
<T> (value: T, assert: ZodFactoryConfig): Promise<T & TZod>;
};Creates an async function that converts a value to the zod schema type, delegating to zodAsAsyncFactory internally.
Provides overloads for optional assertion: without assert config resolves to undefined on failure, with assert config throws on failure.
Type Parameters
| Type Parameter |
| ------ |
| TZod |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| zod | ZodType<TZod> | The zod schema to validate against |
| name | string | A name used in error messages for identification |
Returns
An async function that validates and converts a value to the schema type
<T>(value: T): Promise<T & TZod | undefined>;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
Promise<T & TZod | undefined>
<T>(value: T, assert: ZodFactoryConfig): Promise<T & TZod>;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| assert | ZodFactoryConfig |
Returns
Promise<T & TZod>
zodToFactory
function zodToFactory<TZod>(zod: ZodType<TZod>, name: string): {
<T> (value: T): T & TZod | undefined;
<T> (value: T, assert: ZodFactoryConfig): T & TZod;
};Creates a function that converts a value to the zod schema type, delegating to zodAsFactory internally.
Provides overloads for optional assertion: without assert config returns undefined on failure, with assert config throws on failure.
Type Parameters
| Type Parameter |
| ------ |
| TZod |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| zod | ZodType<TZod> | The zod schema to validate against |
| name | string | A name used in error messages for identification |
Returns
A function that validates and converts a value to the schema type
<T>(value: T): T & TZod | undefined;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
T & TZod | undefined
<T>(value: T, assert: ZodFactoryConfig): T & TZod;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| assert | ZodFactoryConfig |
Returns
T & TZod
interfaces
ZodFactoryConfigObject
Configuration object for zod factory functions, providing a name for error messages.
Properties
| Property | Type |
| ------ | ------ |
| name | string |
type-aliases
AllZodFactories
type AllZodFactories<TType, TName> = { [K in `is${TName}`]: ReturnType<typeof zodIsFactory> } & { [K in `as${TName}`]: ReturnType<typeof zodAsFactory> } & { [K in `to${TName}`]: ReturnType<typeof zodToFactory> };Alpha
Type Parameters
| Type Parameter |
| ------ |
| TType |
| TName extends string |
ZodFactoryConfig
type ZodFactoryConfig =
| AssertConfig
| ZodFactoryConfigObject;Configuration for zod factory assertion behavior, either an AssertConfig or a named config object.
Part of sdk-js
Maintainers
License
See the LICENSE file for license details
