Skip to content

add optional, nullable, nullish, default and coerce to schemas#66

Open
pxseu wants to merge 2 commits into
kaito-http:v4from
pxseu:pxseu/v4-schema-optional-nullable
Open

add optional, nullable, nullish, default and coerce to schemas#66
pxseu wants to merge 2 commits into
kaito-http:v4from
pxseu:pxseu/v4-schema-optional-nullable

Conversation

@pxseu

@pxseu pxseu commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Adds schema modifiers that were missing (IMO) from k:

  • .optional() — accepts undefined, dropped from the OpenAPI required list
  • .nullable() / .nullish() — accept null / null | undefined
  • .default(value) — falls back to value when undefined, surfaced as default in OpenAPI
  • k.coerce.number() — parses numeric strings before the usual number checks (query params, form data)

object, ref and objectFromURLSearchParams honour optional & default fields when parsing/serializing. A genuinely-missing required property still errors with Missing required property as before. Tests added for each.

const user = k.object({
  id: k.coerce.number(),        // "1" -> 1
  name: k.string(),
  bio: k.string().optional(),
  role: k.string().default('user'),
});

I used them successfully before in https://github.com/pxseu/pxseu.com (as a patch), and they came in clutch honestly.…les, make getContext and onError optional, move server options into the router to require less boilerplate, add openapi example

@vercel

vercel Bot commented Jun 1, 2026

Copy link
Copy Markdown

@pxseu is attempting to deploy a commit to the otters Team on Vercel.

A member of the Team first needs to authorize it.

@alii

alii commented Jun 1, 2026

Copy link
Copy Markdown
Member

Sorry we cannot support undefined as a value in kschema. The semantics for optional are going to be difficult. I have some changes somewhere that implement my desired behaviour. I'll follow up once I have time to work on that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants