Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export * from "./time/index.js";
export * from "./stellar/index.js";
export * from "./transport/index.js";
export * from "./errors/index.js";
export * from "./validation/schemas.js";
export * from "./validation/types.js";
14 changes: 9 additions & 5 deletions src/validation/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ export function literal<T extends string | number | boolean>(value: T): Schema<T
}

/**
* Optional schema - allows undefined or null, or validates against the underlying schema.
* Optional schema - allows undefined or delegates to the underlying schema if present.
*/
export function optional<T>(schema: Schema<T>): Schema<T | undefined> {
return {
parse(input: unknown, path: string[] = [], depth: number = 0): ValidationResult<T | undefined> {
checkDepth(depth);
if (input === undefined || input === null) {
if (input === undefined) {
return { success: true, data: undefined };
}
return schema.parse(input, path, depth);
Expand Down Expand Up @@ -196,11 +196,15 @@ export function object<T extends Record<string, Schema<any>>>(

/**
* Union schema - tries each schema sequentially until one succeeds.
* If all fail, returns a structured union error.
* If all fail, returns a structured union validation error.
*/
export function union<T>(...schemas: Schema<T>[]): Schema<T> {
export function union<T>(...schemas: Schema<T>[]): Schema<T>;
export function union<T, U>(...schemas: [Schema<T>, Schema<U>]): Schema<T | U>;
export function union<T, U, V>(...schemas: [Schema<T>, Schema<U>, Schema<V>]): Schema<T | U | V>;
export function union<T, U, V, W>(...schemas: [Schema<T>, Schema<U>, Schema<V>, Schema<W>]): Schema<T | U | V | W>;
export function union(...schemas: Schema<any>[]): Schema<any> {
return {
parse(input: unknown, path: string[] = [], depth: number = 0): ValidationResult<T> {
parse(input: unknown, path: string[] = [], depth: number = 0): ValidationResult<any> {
checkDepth(depth);
const errors: string[] = [];

Expand Down
Loading
Loading