@leancodepl/validation
v10.0.0
Published
CQRS validation helpers for command and query validation
Readme
@leancodepl/validation
TypeScript library for handling validation errors in CQRS command responses with type-safe error code mapping.
Installation
npm install @leancodepl/validation
# or
yarn add @leancodepl/validationAPI
handleValidationErrors(validationErrors, errorCodesMap, validationResults)
Creates a validation error handler that processes errors with type-safe error code mapping.
Parameters:
validationErrors: ValidationError<TAllErrors>[]- Array of validation errors to processerrorCodesMap: TAllErrors- Mapping of error names to numeric codesvalidationResults?: TInResult[]- Optional array of previous handler results
Returns: Handler with handle, handleAll, and check methods
handleResponse(response, errorCodesMap)
Handles CQRS command responses and transforms them into validation error handlers.
Parameters:
response: ApiResponse<CommandResult<TErrors>>- API response containing command resulterrorCodesMap: TErrors- Mapping of error names to numeric codes
Returns: Validation error handler with success/failure support
Usage Examples
Basic Error Handling
import { handleValidationErrors } from "@leancodepl/validation"
const errorCodes = { EmailExists: 1, InvalidEmail: 2 } as const
const errors = [{ ErrorCode: 1, ErrorMessage: "Email exists", PropertyName: "Email" }]
handleValidationErrors(errors, errorCodes)
.handle("EmailExists", () => console.log("Email already registered"))
.handle("InvalidEmail", () => console.log("Invalid email format"))
.check()Command Response Handling
import { handleResponse } from "@leancodepl/validation"
const errorCodes = { UserNotFound: 1 } as const
const response = await fetch("/api/users/123", { method: "PUT", body: JSON.stringify({ name: "John" }) })
handleResponse(response, errorCodes)
.handle("success", () => console.log("User updated"))
.handle("UserNotFound", () => console.log("User not found"))
.handle("failure", () => console.log("Request failed"))
.check()Multiple Error Handling
import { handleValidationErrors } from "@leancodepl/validation"
const errorCodes = { Required: 1, Invalid: 2 } as const
const errors = [
{ ErrorCode: 1, PropertyName: "email", ErrorMessage: "Email required" },
{ ErrorCode: 2, PropertyName: "name", ErrorMessage: "Invalid name" },
]
handleValidationErrors(errors, errorCodes)
.handleAll(["Required", "Invalid"], errorGroups => {
errorGroups.forEach(({ errors }) => {
errors.forEach(error => console.log(`${error.PropertyName}: ${error.ErrorMessage}`))
})
})
.check()Success/Failure Result Processing
import { handleResponse } from "@leancodepl/validation"
const errorCodes = { InvalidData: 1 } as const
const response = await fetch("/api/data")
const isSuccess = handleResponse(response, errorCodes)
.handle("success", () => true)
.handle(["InvalidData", "failure"], () => false)
.check({
reducer: (prev, current) => prev && current,
initialValue: true,
})