2.8 KiB
schemas/options
Purpose
UserPluginOptionsSchema is the Zod boundary contract for the
operator-supplied options accepted by createUserPlugin. It enforces
defaults, validates the route prefix shape, gates profile-field names
through the SurrealQL identifier grammar so they can be interpolated
into DDL without quoting, and structurally validates the optional
logger override. Invalid input causes the factory to throw
UserError(USER_INVALID_INPUT) carrying the ZodError as cause.
Features
- Defaults
routePrefixto/users,allowSelfDeletetotrue,applySchemaOnInittotrue, andprofileFieldsto["displayName", "avatarUrl", "locale", "timezone"]. routePrefixis regex-checked: starts with/, ends without a trailing/(root/is the single allowed exception).profileFieldsis regex-checked against[A-Za-z_][A-Za-z0-9_]*so the schema-apply step can drop each name straight into DDL.loggeris structurally validated viaz.customso operators can pass any object that satisfiesLoggerLikewithout nominal typing.- Two type aliases —
UserPluginOptionsInput(pre-parse) andUserPluginOptionsResolved(post-parse) — are exported so callers can pick the variant that matches their lifecycle stage.
Usage: Basic
import { UserPluginOptionsSchema } from "@suaveplan/api-plugin-user/schemas/options";
const resolved = UserPluginOptionsSchema.parse({});
// resolved.routePrefix === "/users"
Usage: Advanced
When wiring the plugin into a custom factory, validate the operator input separately so the error surface is explicit:
import { UserPluginOptionsSchema } from "@suaveplan/api-plugin-user/schemas/options";
const parsed = UserPluginOptionsSchema.safeParse(input);
if (!parsed.success) {
return Result.err({ code: "USER.INVALID_INPUT", issues: parsed.error });
}
const resolved = parsed.data;
API Reference
UserPluginOptionsSchema: z.ZodObject
Schema with five entries — routePrefix, allowSelfDelete,
profileFields, applySchemaOnInit, and logger. Defaults are
applied during parse.
UserPluginOptionsInput
z.input of the schema — the pre-parse shape, every field optional.
UserPluginOptionsResolved
z.output of the schema — defaults applied. The plugin's defaults
field on the descriptor types to this shape.
Implementation Notes
- The
profileFieldsregex deliberately rejects digits-at-position-0 and dashes; SurrealQL identifiers that contain those characters would need backtick-quoting which the bundled schema does not emit. .readonly()onprofileFieldsflows through to the runtime config so the array is shared without defensive copies.- The
loggerpredicate only checksinfo; the other three methods are inferred from theLoggerLikecast for downstream consumers.