semanticBoolean.ts
utils/semanticBoolean.ts
No strong subsystem tag
30
Lines
1167
Bytes
1
Exports
1
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with general runtime concerns. It contains 30 lines, 1 detected imports, and 1 detected exports.
Important relationships
Detected exports
semanticBoolean
Keywords
semanticbooleaninnerunknownmodelreplace_allpreprocessschemaoptionalalsoaccepts
Detected imports
zod/v4
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
import { z } from 'zod/v4'
/**
* Boolean that also accepts the string literals "true"/"false".
*
* Tool inputs arrive as model-generated JSON. The model occasionally quotes
* booleans — `"replace_all":"false"` instead of `"replace_all":false` — and
* z.boolean() rejects that with a type error. z.coerce.boolean() is the wrong
* fix: it uses JS truthiness, so "false" → true.
*
* z.preprocess emits {"type":"boolean"} to the API schema, so the model is
* still told this is a boolean — the string tolerance is invisible client-side
* coercion, not an advertised input shape.
*
* .optional()/.default() go INSIDE (on the inner schema), not chained after:
* chaining them onto ZodPipe widens z.output<> to unknown in Zod v4.
*
* semanticBoolean() → boolean
* semanticBoolean(z.boolean().optional()) → boolean | undefined
* semanticBoolean(z.boolean().default(false)) → boolean
*/
export function semanticBoolean<T extends z.ZodType>(
inner: T = z.boolean() as unknown as T,
) {
return z.preprocess(
(v: unknown) => (v === 'true' ? true : v === 'false' ? false : v),
inner,
)
}