Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
feat: #126 move out string closest check to util lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Oct 22, 2020
1 parent 5aa13b9 commit 903ebb6
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 122 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"dependencies": {
"@cypress/schema-tools": "^4.7.5",
"@newdash/inject": "^0.2.22",
"@newdash/newdash": "^5.15.0",
"@newdash/newdash": "^5.16.0",
"@odata/metadata": "^0.2.2",
"@odata/parser": "^0.2.3",
"bignumber.js": "^9.0.1",
Expand Down
18 changes: 18 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,29 @@ export class ForeignKeyValidationError extends StartupError {


export class HttpRequestError extends CustomError {

statusCode: number
innerErrors: Array<CustomError>

constructor(statusCode: number, message: string) {
super(message);
this.statusCode = statusCode;
this.innerErrors = [];
}

addInnerError(error: CustomError): this {
this.innerErrors.push(error);
return this;
}

getInnerErrors() {
return this.innerErrors;
}

getStatusCode() {
return this.statusCode;
}

}

export class ServerInternalError extends HttpRequestError {
Expand Down
1 change: 0 additions & 1 deletion src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export * from './connection';
export * from './decorators';
export * from './entity';
export * from './hooks';
export * from './libraries';
export * from './migrate';
export * from './server';
export * from './service';
Expand Down
5 changes: 0 additions & 5 deletions src/type/libraries/index.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/type/libraries/string_closest.ts

This file was deleted.

45 changes: 0 additions & 45 deletions src/type/libraries/string_distance.ts

This file was deleted.

12 changes: 11 additions & 1 deletion src/type/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getUnProxyTarget, inject, InjectContainer, LazyRef, noWrap, required, t
import { forEach } from '@newdash/newdash/forEach';
import { isArray } from '@newdash/newdash/isArray';
import { isEmpty } from '@newdash/newdash/isEmpty';
import { closest } from '@newdash/newdash/string/distance';
import { defaultParser, ODataFilter, ODataMethod, ODataQueryParam, param, QueryOptionsNode as ODataQuery } from '@odata/parser';
import 'reflect-metadata';
import { Connection, DeepPartial, QueryRunner, Repository } from 'typeorm';
Expand Down Expand Up @@ -435,13 +436,22 @@ export class TypedService<T = any> extends ODataController {

const msgs = applyValidate(entityType, input, method);

const columns = Edm.getProperties(entityType);

// ensure client provide all keys are defined in entity type
for (const key of Object.keys(input)) {
if (!columns.includes(key)) {
msgs.push(`property/navigation '${key}' is not existed on EntityType(${entityName}), did you mean '${closest(key, columns)}'?`);
}
}

if (msgs.length > 0) {
throw new BadRequestError(`Entity '${entityName}': ${msgs.join(', ')}`);
}

}

// create or update
// create or overwrite
@odata.PUT
async save(@odata.key key, @odata.body body: DeepPartial<T>) {
const repo = await this._getRepository();
Expand Down
1 change: 1 addition & 0 deletions src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function createColumnValidationRules(entityType: Class, method: ODataMethod) {
}, {});
return columnMetaValidationRules;
}

function createCustomValidationRules(entityType: Class) {

const entityProps = Edm.getProperties(entityType);
Expand Down
35 changes: 0 additions & 35 deletions test/type/libraries.spec.ts

This file was deleted.

0 comments on commit 903ebb6

Please sign in to comment.