是否可以在 nestJs(类验证器 => 自定义验证器)中注入执行上下文或访问当前请求?
import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';
import { Injectable } from '@nestjs/common';
import { Connection } from 'typeorm';
import { InjectConnection } from '@nestjs/typeorm';
@ValidatorConstraint({ name: 'modelExistsConstraint', async: true })
@Injectable()
export class ModelExistsConstraint implements ValidatorConstraintInterface {
constructor(
@InjectConnection('default') private readonly connection: Connection,
) {
}
async validate(value: string, validationArguments: ValidationArguments) {
// here need request or execution context;
// const test = this.context.switchToHttp().getRequest();
const model = await this.connection
.getRepository(validationArguments.constraints[0])
.findOne({ where: { [validationArguments.property]: …Run Code Online (Sandbox Code Playgroud) https://github.com/rokudo5262/phone这是我的项目,
我想加载智能表中的品牌列表,但收到警告,
我尝试修复,但警告仍然存在,请帮助
Brands-features.selector.ts
import { createFeatureSelector } from '@ngrx/store';
import { State, FeatureKey } from '../reducers';
export const selectBrandsState = createFeatureSelector<State>(FeatureKey);
Run Code Online (Sandbox Code Playgroud)
品牌.selector.ts
import { createSelector } from '@ngrx/store';
import { selectBrandsState } from './brands-features.selector';
import { BrandsReducer } from '../reducers';
import { brandAdapter } from '../states/brands.state';
export const selectBrandEntitiesState = createSelector(
selectBrandsState,
state => state[BrandsReducer.brandsFeatureKey],
);
export const {
selectIds: selectBrandIds,
selectEntities: selectBrandEntities,
selectAll: selectAllBrands,
selectTotal: selectTotalBrands,
} = brandAdapter.getSelectors(selectBrandEntitiesState);
export const BrandSelectors = {
selectBrandEntitiesState,
selectBrandIds,
selectBrandEntities, …Run Code Online (Sandbox Code Playgroud) 我有一个使用 vue cli 创建的 vue 应用程序,我使用的版本是 vue2(带有 eslint 和 prettier)。
我可以运行npm run serve并加载我的页面。但是在 Visual Studio Code 中,我注意到这个错误:
{
"resource": "/c:/vue/app2/public/index.html",
"owner": "eslint",
"code": {
"value": "prettier/prettier",
"target": {
"$mid": 1,
"external": "https://github.com/prettier/eslint-plugin-prettier#options",
"path": "/prettier/eslint-plugin-prettier",
"scheme": "https",
"authority": "github.com",
"fragment": "options"
}
},
"severity": 4,
"message": "Parsing error: Unexpected token",
"source": "eslint",
"startLineNumber": 1,
"startColumn": 2,
"endLineNumber": 1,
"endColumn": 2
}
Run Code Online (Sandbox Code Playgroud)
这是我.eslintrc.js创建应用程序时自动生成的,此后我没有对其进行任何更改。
module.exports = {
root: true,
env: {
node: true
},
extends: ["plugin:vue/essential", "eslint:recommended", "@vue/prettier"],
parserOptions: …Run Code Online (Sandbox Code Playgroud) 我正在向一个有角度的项目添加测试。有一项服务从 API 获取一些数据,并根据这些数据创建一些业务实体。
我使用ng-mocks模拟了此服务来测试依赖于它的组件。
原来的服务是这样的:
class DataService {
public getEntityFromApi(): Observable<Entity> {
return http.get(...).pipe(
map(httpResponse => {
return this.createEntityFromApiData(httpResponse);
})
);
}
private createEntityFromApiData(apiData: any): Entity {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
它被嘲笑为:
getMockedServiceProvider() {
const testData = ...;
return MockProvider(DataService, {
getEntityFromApi: () => {
let entity = // duplicated code from the createEntityFromApiData method to create the object from testData
return of(entity);
}
});
}
Run Code Online (Sandbox Code Playgroud)
因此,模拟服务能够返回一个对象,而无需向 API 发出请求,但我必须复制从纯 json 创建对象的代码。
避免模拟服务中出现重复的最佳方法是什么?
createEntityFromApiData原始服务的私有性?我有一个对象定义为:
obj:{
name:"string",
system:"string"
}
Run Code Online (Sandbox Code Playgroud)
但是我正在尝试将模拟商店中的值更新为:
store.setState({
name:"Rose",
system:"Updated Dummy"
});
store.refreshState();
fixture.detectChanges();
Run Code Online (Sandbox Code Playgroud)
但该值没有更新。如何更新该值,然后验证它是否已作为单元测试的一部分进行更新?
如何修复:ngRx 中的“类型错误:实体不可迭代”:如果有人明白这一点,请告诉我。
//用户.action
import { Course } from '../model/user';
import { createAction, props } from '@ngrx/store';
import { Update } from '@ngrx/entity';
export const loadCourses = createAction(
'[Courses List] Load Courses via Service'
);
export const coursesLoaded = createAction(
'[Courses Effect] Courses Loaded Successfully',
props<{ courses: Course[] }>()
);
export const createCourse = createAction(
'[Create Course Component] Create Course',
props<{ course: Course }>()
);
export const deleteCourse = createAction(
'[Courses List Operations] Delete Course',
props<{ courseId: string }>()
);
export …Run Code Online (Sandbox Code Playgroud) angular ×4
ngrx ×3
eslint ×1
nestjs ×1
ng-mocks ×1
ngrx-effects ×1
ngrx-entity ×1
ngrx-store ×1
prettier ×1
redux ×1
testing ×1
typescript ×1
unit-testing ×1