Skip to content

Commit

Permalink
Work example of union type array validation
Browse files Browse the repository at this point in the history
  • Loading branch information
wcalderipe committed Feb 8, 2024
1 parent c64b1c2 commit 23c93b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions apps/authz/src/app/__test__/e2e/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ describe('Admin Endpoints', () => {
{
criterion: 'checkAction',
args: ['signTransaction']
},
{
criterion: 'checkIntentType',
args: ['transferNative']
}
]
}
Expand Down
23 changes: 15 additions & 8 deletions packages/authz-shared/src/lib/type/policy-builder.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@narval/authz-shared'
import { Intents } from '@narval/transaction-request-intent'
import { ApiExtraModels, ApiProperty, getSchemaPath } from '@nestjs/swagger'
import { Type } from 'class-transformer'
import { Transform, plainToInstance } from 'class-transformer'
import { IsDefined, IsEnum, IsIn, IsString, ValidateNested } from 'class-validator'

export const Then = {
Expand Down Expand Up @@ -386,13 +386,20 @@ export class Policy {
name: string

@ValidateNested({ each: true })
// @Type(() => BaseCriterion, {
// discriminator: {
// property: 'criterion',
// subTypes: [{ value: ActionCriterion, name: Criterion.CHECK_ACTION }]
// }
// })
@Type(() => ActionCriterion)
@Transform(({ value }) => {
return value.map((criterion: PolicyCriterion) => {
switch (criterion.criterion) {
case Criterion.CHECK_ACTION:
return plainToInstance(ActionCriterion, criterion)
case Criterion.CHECK_INTENT_TYPE:
return plainToInstance(IntentTypeCriterion, criterion)
// TODO: Sam, fill the rest. Maybe move these validation/transform logic elsewhere
// Don't forget delete the rego files on the e2e tests :)
default:
throw new Error('boom')
}
})
})
@ApiProperty({
oneOf: SUPPORTED_CRITERION.map((entity) => ({
$ref: getSchemaPath(entity)
Expand Down

0 comments on commit 23c93b8

Please sign in to comment.