Skip to content

Generalize rejected data schema for confirmation responses #141

Description

@andreabadesso

Context

In PR #43, a rejectedDataSchema is introduced inline:

const rejectedDataSchema = z.object({ accepted: z.literal(false) });

This { accepted: false } pattern is already used across multiple response types in packages/hathor-rpc-handler/src/types/prompt.ts:

  • SendNanoContractTxConfirmationResponse (line 242)
  • CreateTokenConfirmationResponse (line 252)
  • PinRequestResponse (line 262)
  • CreateNanoContractCreateTokenTxConfirmationResponse (line 336)
  • SendTransactionConfirmationResponse (line 314, uses accepted: boolean — slight variation)

And the rejection check (if (!response.data.accepted)) is repeated across all RPC methods:

  • sendTransaction.ts
  • sendNanoContractTx.ts
  • createToken.ts
  • createNanoContractCreateTokenTx.ts
  • signWithAddress.ts
  • signOracleData.ts

Proposal

Create a shared schema (e.g. in a schemas/ or validators/ file) that can be reused:

  1. A shared rejectedDataSchema:

    export const rejectedDataSchema = z.object({ accepted: z.literal(false) });
  2. Optionally, a discriminated union factory for confirmation responses:

    export const confirmationResponseSchema = <T extends z.ZodRawShape>(acceptedFields: T) =>
      z.discriminatedUnion('accepted', [
        z.object({ accepted: z.literal(true), ...acceptedFields }),
        rejectedDataSchema,
      ]);

This would standardize validation across all confirmation response handlers and reduce duplication.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions