Skip to content

Commit

Permalink
feat: migrate to new http create validator syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Nov 6, 2024
1 parent ba6cc10 commit 38ca70e
Show file tree
Hide file tree
Showing 18 changed files with 385 additions and 238 deletions.
40 changes: 21 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"@routup/rate-limit": "^2.4.0",
"@routup/swagger": "^2.4.1",
"@socket.io/redis-emitter": "^5.1.0",
"@validup/adapter-routup": "^0.1.8",
"@validup/adapter-validator": "^0.1.7",
"@validup/adapter-routup": "^0.1.9",
"@validup/adapter-validator": "^0.2.0",
"amqp-extension": "^4.0.0-beta.3",
"busboy": "^1.6.0",
"cors": "^2.8.5",
Expand All @@ -51,7 +51,7 @@
"singa": "^1.0.0",
"typeorm": "^0.3.20",
"typeorm-extension": "^3.6.3",
"validup": "^0.1.7",
"validup": "^0.1.8",
"yargs": "^17.7.1",
"zod": "^3.23.7"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { Container } from 'validup';
import type { AnalysisBucketFile } from '@privateaim/core-kit';
import { createValidator } from '@validup/adapter-validator';
import { createValidationChain, createValidator } from '@validup/adapter-validator';
import { HTTPHandlerOperation } from '@privateaim/server-http-kit';

export class AnalysisBucketFileValidator extends Container<AnalysisBucketFile> {
Expand All @@ -17,44 +17,56 @@ export class AnalysisBucketFileValidator extends Container<AnalysisBucketFile> {
this.mount(
'bucket_id',
{ group: HTTPHandlerOperation.CREATE },
createValidator((chain) => chain
.exists()
.notEmpty()
.isUUID()),
createValidator(() => {
const chain = createValidationChain();
return chain
.exists()
.notEmpty()
.isUUID();
}),
);

const nameValidator = createValidator(() => {
const chain = createValidationChain();

return chain
.exists()
.isString();
});
this.mount(
'name',
{ group: HTTPHandlerOperation.CREATE },
createValidator((chain) => chain
.exists()
.isString()),
nameValidator,
);

this.mount(
'name',
{ group: HTTPHandlerOperation.UPDATE, optional: true },
createValidator((chain) => chain
.exists()
.isString()
.optional({ values: 'null' })),
nameValidator,
);

this.mount(
'external_id',
{ group: HTTPHandlerOperation.CREATE },
createValidator((chain) => chain
.exists()
.isUUID()),
createValidator(() => {
const chain = createValidationChain();

return chain
.exists()
.isUUID();
}),
);

this.mount(
'root',
createValidator((chain) => chain
.optional()
.toBoolean()
.isBoolean()
.default(false)),
createValidator(() => {
const chain = createValidationChain();
return chain
.optional()
.toBoolean()
.isBoolean()
.default(false);
}),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type { AnalysisBucket } from '@privateaim/core-kit';
import { AnalysisBucketType } from '@privateaim/core-kit';
import { Container } from 'validup';
import { createValidator } from '@validup/adapter-validator';
import { createValidationChain, createValidator } from '@validup/adapter-validator';
import { HTTPHandlerOperation } from '@privateaim/server-http-kit';

export class AnalysisBucketValidator extends Container<AnalysisBucket> {
Expand All @@ -18,18 +18,26 @@ export class AnalysisBucketValidator extends Container<AnalysisBucket> {
this.mount(
'analysis_id',
{ group: HTTPHandlerOperation.CREATE },
createValidator((chain) => chain
.exists()
.notEmpty()
.isUUID()),
createValidator(() => {
const chain = createValidationChain();

return chain
.exists()
.notEmpty()
.isUUID();
}),
);

this.mount(
'type',
{ group: HTTPHandlerOperation.CREATE },
createValidator((chain) => chain
.notEmpty()
.isIn(Object.values(AnalysisBucketType))),
createValidator(() => {
const chain = createValidationChain();

return chain
.notEmpty()
.isIn(Object.values(AnalysisBucketType));
}),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function updateAnalysisNodeRouteHandler(req: Request, res: Response
}

if (!isAuthorityOfNode || !canUpdate) {
if (data.run_status) {
if (isPropertySet(data, 'run_status')) {
throw new BadRequestError(
'You are either no authority of the node or you don\'t have the required permissions.',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { AnalysisNodeApprovalStatus, AnalysisNodeRunStatus } from '@privateaim/core-kit';
import { Container } from 'validup';
import { createValidator } from '@validup/adapter-validator';
import { createValidationChain, createValidator } from '@validup/adapter-validator';
import { HTTPHandlerOperation } from '@privateaim/server-http-kit';
import type { AnalysisNodeEntity } from '../../../../domains';

Expand All @@ -18,52 +18,70 @@ export class AnalysisNodeValidator extends Container<AnalysisNodeEntity> {
this.mount(
'node_id',
{ group: HTTPHandlerOperation.CREATE },
createValidator((chain) => chain
.exists()
.notEmpty()
.isUUID()),
createValidator(() => {
const chain = createValidationChain();
return chain
.exists()
.notEmpty()
.isUUID();
}),
);

this.mount(
'analysis_id',
{ group: HTTPHandlerOperation.CREATE },
createValidator((chain) => chain
.exists()
.notEmpty()
.isUUID()),
createValidator(() => {
const chain = createValidationChain();
return chain
.exists()
.notEmpty()
.isUUID();
}),
);

this.mount(
'run_status',
{ optional: true },
createValidator((chain) => chain
.isIn(Object.values(AnalysisNodeRunStatus))
.optional({ values: 'null' })),
createValidator(() => {
const chain = createValidationChain();
return chain
.isIn(Object.values(AnalysisNodeRunStatus))
.optional({ values: 'null' });
}),
);

this.mount(
'index',
{ optional: true },
createValidator((chain) => chain
.exists()
.isInt()
.optional({ values: 'null' })),
createValidator(() => {
const chain = createValidationChain();
return chain
.exists()
.isInt()
.optional({ values: 'null' });
}),
);

this.mount(
'approval_status',
{ optional: true },
createValidator((chain) => chain
.optional({ nullable: true })
.isIn(Object.values(AnalysisNodeApprovalStatus))),
createValidator(() => {
const chain = createValidationChain();
return chain
.optional({ nullable: true })
.isIn(Object.values(AnalysisNodeApprovalStatus));
}),
);

this.mount(
'comment',
{ optional: true },
createValidator((chain) => chain
.optional({ nullable: true })
.isString()),
createValidator(() => {
const chain = createValidationChain();
return chain
.optional({ nullable: true })
.isString();
}),
);
}
}
Loading

0 comments on commit 38ca70e

Please sign in to comment.