Multi-rule validation
Validate collections with several independent constraints and return every detected problem.
Zod · Module
.superRefine() adds custom validation logic to a schema and can report multiple issues. It also supports Zod's internal issue types, unlike the single custom issue produced by .refine().
.superRefine() runs custom validation after the value has been parsed and gives the callback a context for adding validation issues. Unlike .refine(), it can emit multiple issues and use Zod's internal issue types, while a type-predicate callback can narrow the inferred type.
schema.superRefine((value, ctx) => { ctx.addIssue({ code: "custom", message: "Invalid value" }); })| Name | Type | Description |
|---|---|---|
callback | (value, ctx) => void | Callback invoked with the parsed value and a context object; call ctx.addIssue() to report validation failures. |
A new schema with the added refinement logic.
.superRefine() for validations that need several diagnostics or issue codes beyond custom..refine() instead limits the validation to one custom issue; use .superRefine() when several issues or built-in issue codes are needed.import { z } from "zod";
const UniqueStringArray = z.array(z.string()).superRefine((value, ctx) => {
if (value.length > 3) {
ctx.addIssue({
code: "too_big",
maximum: 3,
origin: "array",
inclusive: true,
message: "Too many items",
input: value,
});
}
if (value.length !== new Set(value).size) {
ctx.addIssue({
code: "custom",
message: "No duplicates allowed",
input: value,
});
}
});
UniqueStringArray.parse(["a", "a", "b", "c"]);This schema reports separate issues for exceeding the length limit and for duplicate values.
Validate collections with several independent constraints and return every detected problem.
Emit a specific built-in issue type, such as an array-size failure, instead of only a generic custom error.
Express a user-defined type predicate when runtime validation should narrow the inferred TypeScript type.
.superRefine() differ from .refine()?.superRefine() accepts a callback receiving the parsed value and a context object. The callback calls ctx.addIssue() to report failures, and it may add more than one issue.
Follow-up: How would you report both duplicate values and an excessive array length? Use separate conditions and call ctx.addIssue() once for each failed condition.
.superRefine() be preferred?.superRefine() can create multiple issues and can use any of Zod's internal issue types. .refine() produces only one issue with the custom code.
Follow-up: What information can a size-related issue carry? Provide an appropriate issue code, a maximum, an origin such as array, inclusiveness, and a message.
.superRefine() affect TypeScript inference?The callback can be a type predicate, in which case Zod narrows the resulting type to the predicate's type. Otherwise, it is used to add custom validation issues without that type narrowing.
Follow-up: What is the practical risk of an inaccurate predicate? The inferred type may be narrower than the values the runtime validation truly guarantees.
refineaddIssueZodIssueCodeZodErrortype predicate