编辑
最后我实现了不同的类并使用@Pipe 来验证我的查询:这是代码:
管道.ts
@Injectable()
export class ValidateQueryPipe implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata) {
let incomingQuery = value as IncomingQuery;
let mappedQuery = new QueryParameters()
mappedQuery.key1 = incomingQuery.key1
mappedQuery.key2= incomingQuery.key2
// other mapped valus
mappedQuery.createdAt = new CreatedAt(incomingQuery['createdAt[lte]'], incomingQuery['createdAt[gte]'])
return mappedQuery;
}
控制器.ts
@Get(':someurl/someurl')
getAllOrders(
@Query(ValidateQueryPipe) query: QueryParameters): Promise<Observable<any>> {
return this.service.findAll(erpCode, query);
}
查询参数.ts
import { CreatedAt } from "./created-at";
export class QueryParameters {
key1: string;
key2: string;
createdAt: CreatedAt;
}
创建-at.ts
export class CreatedAt {
lte: string;
gte: string;
constructor(lte: string, gte: string){
this.lte = lte;
this.gte = gte;
};
}
好吧,我找到了一个部分解决方案,我会在这里发布,直到出现更好的答案:我将嵌套属性提取到单独的查询中并创建我的模型。它并不完美,但它现在正在完成这项工作......
控制器.ts
getAllOrders(
@Query() query,
@Query('createdAt[lte]') lte: string,
@Query('createdAt[gte]') gte: string): Promise<void> {
const mappedObject = new QueryParameters(query, lte, gte)
return this.orderService.findAll(erpCode, mappedObject);
}
createdAtModel.ts
export class CreatedAt {
lte: string;
gte: string;
constructor(lte: string, gte: string){
this.lte = lte;
this.gte = gte;
};
}
我的模型.ts
import { CreatedAt } from "./created-at";
export class QueryParameters {
//other keys
createdAt: CreatedAt;
constructor(query, lte, gte){
//other keys
this.createdAt = new CreatedAt(lte, gte)
}
}
我确信更好的答案在于使用 NestJS 验证管道,但我没有找到适合我的那个。