Skip to content
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

feat: using egg-typebox-validate for PackageController #12

Merged
merged 4 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions app/port/controller/PackageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { PackageRepository } from 'app/repository/PackageRepository';
import { getFullname, getScopeAndName, FULLNAME_REG_STRING } from '../../common/PackageUtil';
import { PackageManagerService } from 'app/core/service/PackageManagerService';
import { Package } from 'app/core/entity/Package';
import { Static, Type } from '@sinclair/typebox';
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

type PackageVersion = Simplify<PackageJson.PackageJsonStandard & {
name: 'string';
Expand All @@ -33,36 +34,27 @@ type PackageVersion = Simplify<PackageJson.PackageJsonStandard & {
},
}>;

const FullPackageRule = {
name: 'string',
// should has at least one version
versions: 'object',
// deprecated request can only has versions
_attachments: 'object?',
description: 'string?',
'dist-tags': 'object?',
readme: 'string?',
};
const FullPackageRule = Type.Object({
name: Type.String(),
// Since we don't validate versions & _attachments previous, here we use Type.Any() just for object validate
versions: Type.Optional(Type.Any()),
_attachments: Type.Optional(Type.Any()),
description: Type.Optional(Type.String()),
'dist-tags': Type.Optional(Type.Record(Type.String(), Type.String())),
readme: Type.Optional(Type.String()),
});

type FullPackage = {
name: string;
versions: {
[key: string]: PackageVersion;
},
// maintainers: JsonObject[],
_attachments?: {
[key: string]: {
content_type: string;
data: string;
length: number;
};
},
description?: string;
'dist-tags'?: {
[key: string]: string;
// overwrite versions & _attachments
type FullPackage = Omit<Static<typeof FullPackageRule>, 'versions' | '_attachments'> &
{ versions: { [key: string]: PackageVersion } } &
{ _attachments: {
[key: string]: {
content_type: string;
data: string;
length: number;
};
readme?: string;
};
}};


// https://www.npmjs.com/package/path-to-regexp#custom-matching-parameters
const PACKAGE_NAME_PATH = `/:fullname(${FULLNAME_REG_STRING})`;
Expand Down Expand Up @@ -141,7 +133,7 @@ export class PackageController extends BaseController {
})
async saveVersion(@Context() ctx: EggContext, @HTTPParam() fullname: string, @HTTPBody() pkg: FullPackage) {
// TODO: using https://github.com/npm/validate-npm-package-name to validate package name
ctx.validate(FullPackageRule, pkg);
ctx.tValidate(FullPackageRule, pkg);
if (fullname !== pkg.name) {
throw new UnprocessableEntityError(`fullname(${fullname}) not match package.name(${pkg.name})`);
}
Expand Down
4 changes: 4 additions & 0 deletions config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const plugin: EggPlugin = {
enable: true,
package: 'egg-opentracing',
},
typeboxValidate: {
enable: true,
package: 'egg-typebox-validate',
},
};

export default plugin;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
"@eggjs/tegg-orm-plugin": "^0.1.5",
"@eggjs/tegg-plugin": "^0.1.1",
"@eggjs/tsconfig": "^1.0.0",
"@sinclair/typebox": "^0.23.0",
"bson-objectid": "^2.0.1",
"dayjs": "^1.10.7",
"egg": "^2.29.4",
"egg-errors": "^2.3.0",
"egg-opentracing": "^1.1.1",
"egg-typebox-validate": "^1.1.0",
"egg-validate": "^2.0.2",
"fresh": "^0.5.2",
"fs-cnpm": "^2.2.0",
Expand Down