UnprocessableEntity class represents an HTTP error response with status code 422. It is used to standardize "Unprocessable Entity" error responses, typically for form validation errors where the request syntax is correct but the semantic content is invalid.ts
import { UnprocessableEntity } from "@arkyn/server";
UnprocessableEntity takes an object with structured validation data:data (optional): Any additional data related to the error, such as metadata or instructions for the client.fieldErrors (optional): An object mapping field names to error messages, indicating specific validation issues with individual fields.fields (optional): An object containing the original field values that caused the validation errors, allowing clients to repopulate form fields.message (optional): A descriptive message explaining the validation error. Defaults to "Unprocessable entity" if not provided.toResponse() - Converts the instance into a Response object with JSON body and Content-Type: application/json header.toJson() - Alternative method using Response.json() for generating the JSON error response.typescript
import { UnprocessableEntity } from "@arkyn/server";// Basic form validation errorthrow new UnprocessableEntity({message: "Validation failed",fieldErrors: {email: "Invalid email format",password: "Password must be at least 8 characters",},fields: {email: "invalid-email",password: "123",},});// With additional data (e.g., scrollTo for auto-scroll to first error)throw new UnprocessableEntity({message: "Please fix the form errors",fieldErrors: { name: "Name is required" },fields: { name: "" },data: { scrollTo: "name" },});// Convert to Response objectconst error = new UnprocessableEntity({fieldErrors: { username: "Username already taken" },});return error.toResponse();
json
{"ok": false,"status": 422,"message": "Validation failed","data": { "scrollTo": "name" },"fieldErrors": {"email": "Invalid email format","password": "Password must be at least 8 characters"},"fields": {"email": "invalid-email","password": "123"}}
SchemaValidator.formValidate() and SchemaValidator.formAsyncValidate():typescript
import { SchemaValidator } from "@arkyn/server";import { z } from "zod";const schema = z.object({email: z.string().email("Invalid email"),name: z.string().min(1, "Name is required"),});const validator = new SchemaValidator(schema);// formValidate throws UnprocessableEntity on validation failureconst data = validator.formValidate(formData, "Please fix the errors");
fields property preserves the original input values, allowing forms to repopulate fields after a validation error.data property can include a scrollTo key with the first error field name, enabling automatic scroll-to-error functionality in the frontend.UnprocessableEntity (422) for semantic validation errors. Use BadRequest (400) for malformed syntax or missing required parameters.