-
Notifications
You must be signed in to change notification settings - Fork 4
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
schemas for serialized requests and usage in engine #238
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
4 changes: 4 additions & 0 deletions
4
apps/policy-engine/src/engine/http/rest/dto/serialized-evaluation-response.dto.ts
This file contains 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,4 @@ | ||
import { SerializedEvaluationResponse } from '@narval/policy-engine-shared' | ||
import { createZodDto } from 'nestjs-zod' | ||
|
||
export class SerializedEvaluationResponseDto extends createZodDto(SerializedEvaluationResponse) {} |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { ZodTypeAny, z } from 'zod' | ||
import { AccountId } from '../util/caip.util' | ||
import { SignMessageAction, SignRawAction, SignTransactionAction, SignTypedDataAction } from './action.type' | ||
import { | ||
SerializedTransactionAction, | ||
SignMessageAction, | ||
SignRawAction, | ||
SignTransactionAction, | ||
SignTypedDataAction | ||
} from './action.type' | ||
|
||
export enum Decision { | ||
PERMIT = 'Permit', | ||
|
@@ -92,6 +98,14 @@ export const Request = z.discriminatedUnion('action', [ | |
]) | ||
export type Request = z.infer<typeof Request> | ||
|
||
export const SerializedRequest = z.discriminatedUnion('action', [ | ||
SerializedTransactionAction, | ||
SignMessageAction, | ||
SignTypedDataAction, | ||
SignRawAction | ||
]) | ||
export type SerializedRequest = z.infer<typeof SerializedRequest> | ||
|
||
export const Feed = <Data extends ZodTypeAny>(dataSchema: Data) => | ||
z.object({ | ||
source: z.string(), | ||
|
@@ -137,6 +151,11 @@ export const EvaluationRequest = z | |
.describe('The action being authorized') | ||
export type EvaluationRequest = z.infer<typeof EvaluationRequest> | ||
|
||
export const SerializedEvaluationRequest = EvaluationRequest.extend({ | ||
request: SerializedRequest | ||
}) | ||
export type SerializedEvaluationRequest = z.infer<typeof SerializedEvaluationRequest> | ||
|
||
export const ApprovalRequirement = z.object({ | ||
approvalCount: z.number().min(0), | ||
approvalEntityType: z.nativeEnum(EntityType).describe('The number of requried approvals'), | ||
|
@@ -145,22 +164,30 @@ export const ApprovalRequirement = z.object({ | |
}) | ||
export type ApprovalRequirement = z.infer<typeof ApprovalRequirement> | ||
|
||
export type AccessToken = { | ||
value: string // JWT | ||
// could include a key-proof | ||
} | ||
export const AccessToken = z.object({ | ||
value: JwtString | ||
}) | ||
export type AccessToken = z.infer<typeof AccessToken> | ||
|
||
export const EvaluationResponse = z.object({ | ||
decision: z.nativeEnum(Decision), | ||
request: Request.optional(), | ||
approvals: z | ||
.object({ | ||
required: z.array(ApprovalRequirement).optional(), | ||
missing: z.array(ApprovalRequirement).optional(), | ||
satisfied: z.array(ApprovalRequirement).optional() | ||
}) | ||
.optional(), | ||
accessToken: AccessToken.optional(), | ||
transactionRequestIntent: z.unknown().optional() | ||
}) | ||
export type EvaluationResponse = z.infer<typeof EvaluationResponse> | ||
|
||
export type EvaluationResponse = { | ||
decision: Decision | ||
request?: Request | ||
approvals?: { | ||
required: ApprovalRequirement[] | ||
missing: ApprovalRequirement[] | ||
satisfied: ApprovalRequirement[] | ||
} | ||
accessToken?: AccessToken | ||
transactionRequestIntent?: unknown | ||
} | ||
export const SerializedEvaluationResponse = EvaluationResponse.extend({ | ||
request: SerializedRequest.optional() | ||
}) | ||
export type SerializedEvaluationResponse = z.infer<typeof SerializedEvaluationResponse> | ||
|
||
export type Hex = `0x${string}` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for refactoring it. |
||
|
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.
Let's step up your game on the controller implementation by using DTO for documentation and serialization.
The way you implemented the DTO today isn't adding much value because Nest doesn't know how to use it.
Let me know if that doesn't work
The serialization must be configured by either module and/or application. As good practice, we always add it to the application level, so all modules have it in production.
armory/apps/policy-engine/src/main.ts
Lines 17 to 21 in cdd72ef
Note the
{ transform: true }
option.You'll notice we also add it per module. The reason is that modules are standalone functionalities, and we test them in a mock server instead of the real one.
Since we're moving away from the standard DTOs for validation and serialization, we'll need to use nest-zod utilities.
{ provide: APP_INTERCEPTOR, useClass: ZodSerializerInterceptor }
(see https://github.com/risen228/nestjs-zod?tab=readme-ov-file#include-zodserializerinterceptor-in-application-root)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.
Thank you for the link. I've implemented it following the documentation you linked. Meaning:
As you asked 'let me know if that doesn't work', your code snippet as is didnt. Also, from what I understood of the documentation and what I've tried, you need to specifically add
@ZodSerializerDto
decorator with the schema you want to use on every route that uses the interceptor. Did you wanted to pass the SerializerDto as part of the @ApiResponse decorator ? If so, I didn't managed to configure that, nor do I think it is possible looking at the documentation.