Skip to content

refactor(sdk): wire AuthConfigSchema.parse and clean up redundancies#1190

Draft
toiroakr wants to merge 6 commits into
mainfrom
refactor/auth-cli-parse-wiring
Draft

refactor(sdk): wire AuthConfigSchema.parse and clean up redundancies#1190
toiroakr wants to merge 6 commits into
mainfrom
refactor/auth-cli-parse-wiring

Conversation

@toiroakr
Copy link
Copy Markdown
Contributor

@toiroakr toiroakr commented May 19, 2026

Summary

Follow-up to PR #1189 (chore/side-effect). Splits the auth-related refactor into a focused stack so the parent PR can stay scoped to the tree-shaking cleanup.

  • Reshape AuthConfigSchema so the mutex between userProfile and machineUserAttributes is encoded with z.xor([V1, V2], { error }). The custom error callback inspects the union's iss.errors and emits a one-line friendly message only when every variant's failures point at the mutex paths; nested field errors fall through to Zod's default. Replaces the prior nested z.union([..., z.xor([...])]), which produced verbose 3-variant types and Zod errors that buried the actual constraint.
  • Wire AuthConfigSchema.parse into application.ts:defineAuth so auth configs are validated at CLI deploy/generate time. This matches the existing pattern used for IdP, TailorDB, and static websites, and closes the gap left when the configure-layer defineAuth stopped calling parse().
  • Drop the per-call IdProviderSchema.optional().parse and per-connection AuthConnectionConfigSchema.parse inside createAuthService. They were duplicating work now done by the top-level AuthConfigSchema.parse and pulled parser imports into cli/services/auth/service.ts unnecessarily.

The merge { ...config, ...AuthConfigSchema.parse(config) } in defineAuth preserves runtime methods (invoker, getConnectionToken) attached by the configure layer while applying parse-side transforms (OAuth2 token lifetime number to BigInt seconds) and refinements.

AuthConfigInput is consumed only inside its own generated file, so the shape change (single object → V1 | V2 union) is safe for downstream consumers.

Base: chore/side-effect (will rebase onto main once #1189 lands).

toiroakr added 3 commits May 19, 2026 15:05
Replace the z.union([..., z.xor([...])]) structure with a flat object schema
plus .refine that emits a friendly message when both userProfile and
machineUserAttributes are supplied.

The union+xor encoding produced verbose 3-variant types in auth.generated.ts
and surfaced as Zod union errors that buried the actual constraint. The new
shape generates a single object type and yields a one-line error pointing at
machineUserAttributes.

AuthConfigInput is consumed only inside its own generated file, so the type
shape change is safe for downstream consumers.
Move the auth config validation that used to live in defineAuth (configure
layer) into application.ts:defineAuth so it runs at CLI deploy/generate time.

Matches the existing pattern used for IdP (IdPSchema.parse), TailorDB
(TailorDBServiceConfigSchema.parse), and static websites (StaticWebsiteSchema.parse).

Closes the gap where the configure layer stopped calling parse() but no
CLI-side enforcement was wired up, leaving invalid auth configs to fall
through silently until they hit the platform.

Merge the parsed result back over the original config so runtime methods
(invoker, getConnectionToken) attached by the configure layer survive,
while parse-side transforms (OAuth2 token lifetime number to BigInt
seconds) and validation refinements take effect.
AuthConfigSchema.parse now runs at the CLI defineAuth boundary, validating
idProvider and connections as part of the top-level config parse. The
per-field IdProviderSchema.optional().parse and per-connection
AuthConnectionConfigSchema.parse calls inside createAuthService were
duplicating that work.

Replace them with simple type casts that surface the parsed shape stored in
@/types/auth.generated. The runtime values are already validated, so the
casts are not lying about anything that hasn't been checked.

Removes two parser imports from the cli/services/auth/service.ts module,
keeping the runtime path inside the configure-layer boundary unchanged.
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 19, 2026

⚠️ No Changeset found

Latest commit: 2241e75

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 19, 2026

⚡ pkg.pr.new

@tailor-platform/sdk

pnpm add https://pkg.pr.new/@tailor-platform/sdk@2241e75
pnpm dlx https://pkg.pr.new/@tailor-platform/sdk@2241e75 --help

@tailor-platform/create-sdk

pnpm add https://pkg.pr.new/@tailor-platform/create-sdk@2241e75
pnpm dlx https://pkg.pr.new/@tailor-platform/create-sdk@2241e75 my-app

commit: 2241e75

This comment was marked as outdated.

Replace the flat `.refine`-based mutex check with `z.xor([V1, V2], { error })`,
where V1 allows `userProfile` and V2 requires `machineUserAttributes`. A custom
`error` callback inspects `iss.errors` to detect the "both fields supplied"
case (every variant's errors must point at top-level `userProfile`/
`machineUserAttributes` paths) and emits a one-line friendly message. For any
other shape — e.g. nested validation failures inside `userProfile.type` — the
callback returns `undefined` and Zod's default union error is preserved.

This keeps the mutex constraint visible in the generated `AuthConfigInput`
type (`V1 | V2`) without sacrificing the focused error string users saw under
the `.refine` variant.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

[
AuthConfigBaseSchema.extend({
userProfile: UserProfileSchema,
userProfile: UserProfileSchema.optional().describe("User profile configuration"),
Comment on lines +178 to +180
const parsedConfig = {
...config,
...AuthConfigSchema.parse(config),
toiroakr added 2 commits May 19, 2026 17:40
Now that AuthConfigSchema.parse is wired into the CLI application loader
(commit 4ec5165), the mutex between userProfile and machineUserAttributes
is enforced at CLI parse time via z.xor(). The defensive runtime check in
defineAuth duplicated that behavior and prevented the factory body from
being side-effect-free for tree-shaking.

The configure-layer test that bypassed the type system to exercise the
throw is removed; the parser-layer test (src/parser/service/auth/index.test.ts)
continues to cover the same case via AuthConfigSchema.parse().
Base automatically changed from chore/side-effect to main May 20, 2026 06:14
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