3.2 KiB
3.2 KiB
macos-keychain.schemas
(no submodules)
Purpose
Provides runtime Zod schemas that mirror the public type contracts. They are
parsed at every consumer boundary so an invalid timeoutMs, missing required
field, or wrong-shape logger surfaces as a precise ZodError rather than a
downstream undefined-behaviour failure deep inside the bridge.
The schemas are colocated with the rest of the package surface but are also
exposed via the dedicated ./schemas/macos-keychain.schemas subpath so a
calling library can re-validate config it received from elsewhere (CLI flags,
config file, environment) before constructing the bridge.
Features
KeychainConfigSchema— structural shape of bridge construction options.KeychainItemSchema— full credential record (used by the parser).KeychainResultSchema— boolean success envelope.- Inferred types —
KeychainConfigInput,KeychainItemInput,KeychainResultInput— parallel the canonical interfaces with the parsing defaults applied.
Usage: Basic Example
import { KeychainConfigSchema } from "@suaveplan/macos-keychain";
const config = KeychainConfigSchema.parse({
serviceName: "my-app",
timeoutMs: 5_000,
});
Usage: Advanced Example
import {
KeychainConfigSchema,
KeychainItemSchema,
} from "@suaveplan/macos-keychain";
function loadKeychainConfigFromCli(rawArgs: Record<string, unknown>) {
const result = KeychainConfigSchema.safeParse({
serviceName: rawArgs["service"],
timeoutMs: Number(rawArgs["timeout"]) || undefined,
});
if (!result.success) {
throw new Error(
`Invalid keychain CLI flags: ${result.error.message}`
);
}
return result.data;
}
const stored = KeychainItemSchema.parse({
service: "github",
account: "alice",
password: "ghp_…",
});
void stored;
void loadKeychainConfigFromCli;
API Reference
KeychainConfigSchema— Zod object schema. Every field optional;timeoutMsvalidated as a positive integer;clockvalidated as a function;loggervalidated as an object exposing the fourLoggerLikemethods.KeychainItemSchema— Zod object schema requiring non-emptyserviceandaccountstrings, cappingpasswordat 1 MiB, and accepting optionallabel,comment,createdAt,modifiedAt.KeychainResultSchema— Zod object schema with requiredsuccessboolean and optionalerrorstring.- Inferred types — re-exported via
z.infer<typeof Schema>for callers that want a single source of truth for both runtime and compile-time validation.
Implementation Notes
- Schemas use
z.customforclockandloggerbecause they are structural function/object shapes. Zod's.parsewould otherwise narrow them tounknownwithout a type assertion. - The
passwordupper bound exists because the macOSsecurityCLI enforces a similar limit and a multi-megabyte payload almost certainly indicates a programming error or accidental binary blob. - Schemas do not validate the platform itself — the bridge throws
KEYCHAIN.UNAVAILABLEat runtime when invoked off macOS, but constructing the schema and parsing inputs remains valid on any platform.