-
Notifications
You must be signed in to change notification settings - Fork 1
feat(core): add complete schema validation to all authentication flows #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
halvaradop
wants to merge
1
commit into
master
Choose a base branch
from
feat/support-full-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { formatZodError } from "@/shared/utils.ts" | ||
| import { UserIdentity } from "@/shared/identity.ts" | ||
| import { IdentityConfig } from "@/@types/config.ts" | ||
| import { AuthValidationError } from "@/shared/errors.ts" | ||
| import { createValidator } from "@/validator/validator.ts" | ||
| import { isArkType, isValibotSchema, isZodSchema } from "@/shared/assert.ts" | ||
| import { strictObject, partial, looseObject, type ObjectSchema, object } from "valibot" | ||
| import type { Type } from "arktype" | ||
| import type { ZodObject } from "zod/v4" | ||
|
|
||
| export const deriveSchema = <T extends ZodObject<any> | ObjectSchema<any, undefined> | Type<{}>>( | ||
| schema: T, | ||
| mode: "strip" | "passthrough" | "strict" | "partial" = "strip" | ||
| ): any => { | ||
| if (isZodSchema(schema)) { | ||
| return mode === "strip" | ||
| ? schema.strip() | ||
| : mode === "passthrough" | ||
| ? schema.loose() | ||
|
halvaradop marked this conversation as resolved.
|
||
| : mode === "strict" | ||
| ? schema.strict() | ||
| : schema.partial() | ||
| } | ||
| if (isValibotSchema(schema)) { | ||
| return mode === "strip" | ||
| ? object(schema.entries) | ||
| : mode === "passthrough" | ||
| ? looseObject(schema.entries) | ||
| : mode === "strict" | ||
| ? strictObject(schema.entries) | ||
| : partial(schema as ObjectSchema<any, undefined>) | ||
| } | ||
| if (isArkType(schema)) { | ||
| return mode === "strip" | ||
| ? schema.onUndeclaredKey("delete") | ||
| : mode === "passthrough" | ||
| ? schema.onUndeclaredKey("ignore") | ||
| : mode === "strict" | ||
| ? schema.onUndeclaredKey("reject") | ||
| : schema.partial() | ||
| } | ||
| throw new AuthValidationError( | ||
| "INVALID_IDENTITY_VALIDATION_FAILED", | ||
| `Unsupported schema mode configuration. Valid options are: "strip", "passthrough", "strict" and "partial".` | ||
| ) | ||
| } | ||
|
|
||
| export const createSchemaRegistry = <Identity extends ZodObject<any> | ObjectSchema<any, undefined> | Type<{}>>( | ||
| config: IdentityConfig<Identity> | ||
| ) => { | ||
| const schema = deriveSchema(config.schema ?? UserIdentity, config.unknownKeys) | ||
| const partialSchema = deriveSchema(config.schema ?? UserIdentity, "partial") | ||
|
|
||
| const validator = createValidator(schema) | ||
| const partialValidator = createValidator(partialSchema) | ||
|
|
||
| const parse = async (data: unknown = {}) => { | ||
| const { data: output, success, error } = validator.validate(data) | ||
| if (!success) { | ||
| const details = JSON.stringify(isZodSchema(schema) ? formatZodError(error) : {}, null, 2) | ||
| throw new AuthValidationError("INVALID_IDENTITY_VALIDATION_FAILED", details, { | ||
| cause: isZodSchema(schema) ? error : undefined, | ||
| }) | ||
| } | ||
| return output | ||
| } | ||
|
halvaradop marked this conversation as resolved.
|
||
|
|
||
| const parseAsPartial = async (data: unknown = {}) => { | ||
| const { data: output, success, error } = partialValidator.validate(data) | ||
| if (!success) { | ||
| const details = JSON.stringify(isZodSchema(schema) ? formatZodError(error) : {}, null, 2) | ||
| throw new AuthValidationError("INVALID_IDENTITY_VALIDATION_FAILED", details, { | ||
| cause: isZodSchema(schema) ? error : undefined, | ||
| }) | ||
| } | ||
| return output | ||
| } | ||
|
halvaradop marked this conversation as resolved.
|
||
|
|
||
| return { parse, parseAsPartial } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { IsObject } from "typebox" | ||
| import { safeParse } from "valibot" | ||
| import { Check } from "typebox/value" | ||
| import { isValibotSchema, isZodSchema, isArkType } from "@/shared/assert.ts" | ||
|
|
||
| export type ValidationResult<T> = { success: true; data: T; error: null } | { success: false; data: null; error: any } | ||
|
|
||
| export interface SchemaAdapter<T> { | ||
| validate: (data: unknown) => ValidationResult<T> | ||
| } | ||
|
|
||
| /** | ||
| * Universal wrapper for Zod, Valibot, ArkType, etc. | ||
| */ | ||
| export const createValidator = <T>(schema: any): SchemaAdapter<T> => { | ||
| if (!isZodSchema(schema) && !isValibotSchema(schema) && !isArkType(schema) && !IsObject(schema)) { | ||
| throw new Error("Unsupported schema type") | ||
| } | ||
| return { | ||
| validate: (data: unknown): ValidationResult<T> => { | ||
| try { | ||
| if (isZodSchema(schema)) { | ||
| const parsed = schema.safeParse(data) | ||
| return parsed.success | ||
| ? { success: true, data: parsed.data as T, error: null } | ||
| : { success: false, data: null, error: parsed.error } | ||
| } | ||
| if (isValibotSchema(schema)) { | ||
| const parsed = safeParse(schema, data) | ||
| return parsed.success | ||
| ? { success: true, data: parsed.output as T, error: null } | ||
| : { success: false, data: null, error: parsed.issues } | ||
| } | ||
| if (isArkType(schema)) { | ||
| const parsed = schema(data) | ||
| const isError = | ||
| parsed !== null && | ||
| typeof parsed === "object" && | ||
| "summary" in parsed && | ||
| typeof (parsed as any).summary === "string" | ||
|
|
||
| return isError | ||
| ? { success: false, data: null, error: parsed } | ||
| : { success: true, data: parsed as T, error: null } | ||
|
halvaradop marked this conversation as resolved.
|
||
| } | ||
| if (IsObject(schema)) { | ||
| const isValid = Check(schema, data) | ||
| return isValid | ||
| ? { success: true, data: data as T, error: null } | ||
| : { success: false, data: null, error: new Error("Validation failed") } | ||
| } | ||
| return { success: false, data: null, error: new Error("Unsupported schema type") } | ||
| } catch (e) { | ||
| return { success: false, data: null, error: e } | ||
| } | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: aura-stack-ts/auth
Length of output: 2518
Regenerate
pnpm-lock.yamlafter dependency version changes.Line 89 updates
@aura-stack/routerto^0.7.0, but the lockfile must be synchronized. CI currently fails withERR_PNPM_OUTDATED_LOCKFILEbecause frozen-lockfile installations cannot proceed until the manifest and lockfile are in sync. Runpnpm installto regenerate the lockfile and resolve this blocker.🧰 Tools
🪛 GitHub Actions: CI / 0_Node.js.txt
[error] Lockfile specifiers do not match package.json specifiers during pnpm install --frozen-lockfile (ERR_PNPM_OUTDATED_LOCKFILE).
🤖 Prompt for AI Agents