Skip to content

Commit

Permalink
🐛 fix: schema in params
Browse files Browse the repository at this point in the history
  • Loading branch information
yourtion committed Oct 9, 2018
1 parent 21c13bf commit 8b6205c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "erest",
"version": "1.8.3",
"version": "1.8.4",
"description": "Easy to build api server depend on lei-web and express.",
"main": "dist/lib/index.js",
"typings": "dist/lib/index.d.ts",
Expand Down
27 changes: 16 additions & 11 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import pathToRegExp from "path-to-regexp";
import { api as debug } from "./debug";
import { getSchemaKey, SourceResult, getRealPath } from "./utils";
import { ISchemaType } from "./params";
import { SchemaType } from "@tuzhanai/schema-manager";
import { SchemaType, parseTypeName } from "@tuzhanai/schema-manager";
import ERest from ".";

export type TYPE_RESPONSE = string | SchemaType | ISchemaType | Record<string, ISchemaType>;
Expand Down Expand Up @@ -339,16 +339,21 @@ export default class API<T = DEFAULT_HANDLER> {
// 初始化时参数类型检查
for (const [name, options] of this.options._allParams.entries()) {
const typeName = options.type;
const type = parent.type.get(typeName).info;
if (options.required) this.options.required.add(name);
assert(type && type.checker, `please register type ${typeName}`);
// FIXME: 临时屏蔽 type 不存在
// if(!type) continue;
if (type!.isParamsRequired && options.params === undefined) {
throw new Error(`${typeName} is require a params`);
}
if (options.params) {
assert(type!.paramsChecker!(options.params), `test type params failed`);
const type = parent.type.has(typeName) && parent.type.get(typeName).info;
if (type) {
// 基础类型
if (options.required) this.options.required.add(name);
assert(type && type.checker, `please register type ${typeName}`);
if (type.isParamsRequired && options.params === undefined) {
throw new Error(`${typeName} is require a params`);
}
if (options.params && type.paramsChecker) {
assert(type.paramsChecker(options.params), `test type params failed`);
}
} else {
// schema 类型
const schemaName = parseTypeName(typeName);
assert(parent.schema.has(schemaName.name), `please register scheam ${schemaName}`);
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/test-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import lib from "./lib";
import express from "express";
import { build, TYPES } from "./helper";
import { GROUPS, INFO } from "./lib";
import { getCallerSourceLine, getPath } from "../lib/utils";
Expand Down Expand Up @@ -30,6 +31,24 @@ describe("API - base", () => {
});
});

describe("API - Params wtih schema", () => {
const apiService = lib();
apiService.schema.register("a", {
a: build(TYPES.String, "str"),
});
apiService.api
.get("/")
.group("Index")
.query({
a: build("a", "Int", true),
b: build("a[]", "Int[]", true),
})
.register(() => {});
const router = express();
apiService.bindRouter(router, apiService.checkerExpress);
expect(apiService.schema.has("a")).toBeTruthy();
});

describe("API - more test", () => {
const apiService = lib();
apiService.setMockHandler(() => {
Expand Down

0 comments on commit 8b6205c

Please sign in to comment.