model-validator-ts
Version:
[](https://www.npmjs.com/package/model-validator-ts)
158 lines • 5.17 kB
TypeScript
/**
* Order Cancellation Business Logic Validation Example
*
* This example demonstrates how to handle complex business rules for order cancellation
* in an imaginary ecommerce app:
* The rules are that a product can be cancelled if:
* - The order is not already cancelled
* - The user has permission to cancel the order (it's owned by the user or the user is an admin)
* - The order is not shipped or scheduled to ship within 24 hours
* - The order does not contain non-cancellable items (like personalized products or digital downloads)
* - The order did not use a special discount code
* - The order is not fulfilled by a third party
* - The order was created within the last 10 days
* - The order belongs to the requesting customer (except admin panel)
*/
import { z } from "zod";
export declare const orderCancellationSchema: z.ZodObject<{
orderId: z.ZodString;
customerId: z.ZodString;
reason: z.ZodString;
source: z.ZodEnum<["customer-portal", "admin-panel", "api"]>;
}, "strip", z.ZodTypeAny, {
orderId: string;
customerId: string;
reason: string;
source: "customer-portal" | "admin-panel" | "api";
}, {
orderId: string;
customerId: string;
reason: string;
source: "customer-portal" | "admin-panel" | "api";
}>;
export type OrderCancellationRequest = z.infer<typeof orderCancellationSchema>;
export declare const orderCancellationValidator: import("./index.js").FluentValidatorBuilder<z.ZodObject<{
orderId: z.ZodString;
customerId: z.ZodString;
reason: z.ZodString;
source: z.ZodEnum<["customer-portal", "admin-panel", "api"]>;
}, "strip", z.ZodTypeAny, {
orderId: string;
customerId: string;
reason: string;
source: "customer-portal" | "admin-panel" | "api";
}, {
orderId: string;
customerId: string;
reason: string;
source: "customer-portal" | "admin-panel" | "api";
}>, CancellationDependencies, {
order: Order;
shippingStatus: ShippingStatus;
}, "required">;
export declare const cancelOrderCommand: import("./index.js").Command<z.ZodObject<{
orderId: z.ZodString;
customerId: z.ZodString;
reason: z.ZodString;
source: z.ZodEnum<["customer-portal", "admin-panel", "api"]>;
}, "strip", z.ZodTypeAny, {
orderId: string;
customerId: string;
reason: string;
source: "customer-portal" | "admin-panel" | "api";
}, {
orderId: string;
customerId: string;
reason: string;
source: "customer-portal" | "admin-panel" | "api";
}>, CancellationDependencies, {
order: Order;
shippingStatus: ShippingStatus;
}, import("./index.js").ErrorBag<"orderId" | "customerId" | "reason" | "source"> | {
success: boolean;
orderId: string;
status: OrderStatus;
refundAmount: number;
message: string;
}, "required">;
export declare function exampleUsage(dependencies: CancellationDependencies): Promise<{
success: boolean;
data: {
refundAmount: number;
};
error?: undefined;
} | {
success: boolean;
error: {
global: string | undefined;
issues: Partial<Record<"orderId" | "customerId" | "reason" | "source", string[]>>;
};
data?: undefined;
}>;
export type OrderStatus = "pending" | "processing" | "shipped" | "delivered" | "cancelled";
export type ProductType = "physical" | "digital" | "personalized" | "downloadable";
export type FulfillmentType = "internal" | "third-party";
export type RequestSource = "customer-portal" | "admin-panel" | "api";
export interface User {
id: string;
role: "customer" | "admin";
}
export interface OrderItem {
id: string;
productId: string;
productType: ProductType;
quantity: number;
price: number;
}
export interface Order {
id: string;
customerId: string;
status: OrderStatus;
items: OrderItem[];
totalAmount: number;
discountCode?: string;
fulfillmentType: FulfillmentType;
createdAt: Date;
shippingId: string;
}
export interface Product {
id: string;
name: string;
type: ProductType;
isCancellable: boolean;
}
export interface ShippingStatus {
shippingId: string;
isShipped: boolean;
plannedShippingDate?: Date;
trackingNumber?: string;
carrier?: string;
}
export interface OrderService {
findById(orderId: string): Promise<Order | null>;
cancelOrder(orderId: string, reason: string): Promise<Order>;
}
export interface ProductCatalog {
findById(productId: string): Promise<Product | null>;
}
export interface DiscountService {
isSpecialDiscount(code: string): Promise<boolean>;
}
export interface ShippingService {
getShippingStatus(shippingId: string): Promise<ShippingStatus>;
}
export interface NotificationService {
notifyCancellation(orderId: string, customerId: string, reason: string): Promise<{
sent: boolean;
timestamp: Date;
}>;
}
export interface CancellationDependencies {
orderService: OrderService;
productCatalog: ProductCatalog;
discountService: DiscountService;
shippingService: ShippingService;
notificationService: NotificationService;
user: User;
}
//# sourceMappingURL=order-cancellation.example.d.ts.map