我需要创建动态模式以根据请求查询中的键node js
使用Joi 验证器来验证我的 api请求查询。说下面提到的模式是我的有效查询。
我正在使用hapi/joi
版本16.1.8
组合1
{ type: 1, firstname: 'user first name', lastname: 'user last name'}
Run Code Online (Sandbox Code Playgroud)
组合2
{ type: 2 , salary: 1000, pension: 200}
Run Code Online (Sandbox Code Playgroud)
组合3
{ type: 3 , credit: 550, debit: 100}
Run Code Online (Sandbox Code Playgroud)
如您所见,对象键因 的值而异type
。如何正确处理?
我们可以使用Joi.alternatives处理两个条件,例如
const schema = Joi.alternatives().conditional(Joi.object({ type: 1 }).unknown(), {
then: Joi.object({
type: Joi.string(),
firstname: Joi.string(),
lastname: Joi.string()
}),
otherwise: Joi.object({
type: Joi.number(),
salary: Joi.any(),
pension: Joi.any()
})
});
Run Code Online (Sandbox Code Playgroud)
但是如何在 3 个条件下做到这一点?
我试图对节点中的一个函数进行单元测试,无论任何条件如何,该函数都会引发错误。这是我的节点函数定义。
public testFunction() {
throw new Error('Test Error');
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,每当调用该函数时,它总是会抛出错误。我尝试使用 jest .toThrow(error?)方法对此函数执行单元测试。我无法按预期对该功能进行单元测试。
下面提到的是我编写的测试用例,并附上了我在执行相同测试时遇到的错误的屏幕截图。
测试用例 #1
it('Should throw test error', (done) => {
const testClass = TestClassService.getInstance();
expect(testClass.testFunction()).toThrow();
});
Run Code Online (Sandbox Code Playgroud)
测试用例 #2
it('Should throw test error', (done) => {
const testClass = TestClassService.getInstance();
expect(testClass.testFunction).toThrow();
});
Run Code Online (Sandbox Code Playgroud)
测试用例 #3
在这篇博客中,提到了
如果我们希望函数对某些输入参数抛出异常,关键点是我们必须传入函数定义,并且不要在期望内部调用我们的函数。
所以我将我的测试用例更新为
it('Should throw test error', (done) => {
const testClass = TestClassService.getInstance();
expect(() => testClass.testFunction()).toThrow();
});
Run Code Online (Sandbox Code Playgroud)
但它抛出了类似的错误
我的实施有什么问题?对抛出错误对象的函数进行单元测试的正确方法是什么?
我试图获取给定子级内部元素的父级索引,例如,我有一个具有以下结构的list
变量(2) [Array(1), Array(2)]
:
[
[
{id: "1",contactType: {id: "phoneNumber",company: {id: "01",name: "Company01"}},value: "5555555555"},
],
[
{id: "2",contactType: {id: "phoneNumber",company: {id: "03",name: "Company03"}},value: "7777777777"},
{id: "3",contactType: {id: "phoneNumber",company: {id: "05",name: "Company05"}},value: "8888888888"},
],
]
Run Code Online (Sandbox Code Playgroud)
我尝试使用includes
并findIndex
首先验证此类元素是否存在,然后获取父索引:
list.includes('5555555555', 0);
Run Code Online (Sandbox Code Playgroud)
我期望得到,true
因为我要求include
开始在列表的索引 0 中搜索"5555555555"
元素所在的位置,但我得到false
了。
还尝试过:
list.findIndex(x => x.value === '5555555555');
Run Code Online (Sandbox Code Playgroud)
我预计0
,因为元素5555555555
位于父级 0 索引中。但-1
反而得到了。
我也尝试使用flat()
进入儿童和使用includes
,但后来我失去了原来的0
和索引。1
list
预期输出: …
我需要一个具有以下功能的 kendo ui 下拉列表。
- 带有选择复选框的多选下拉菜单可一次检查多个选项。
- 选择所有复选框作为标题模板,当我单击它时,会选择该选项的所有过滤搜索结果。
我查阅了很多参考资料,并从 telrik 找到了一个接近的解决方案。至此我的第一个要求就满足了。我已在此处附加了代码片段。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Kendo UI Snippet</title>
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.common.min.css" />
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.rtl.min.css" />
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.silver.min.css" />
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.mobile.all.min.css" />
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.3.1028/js/kendo.all.min.js"></script>
</head>
<body>
<div id="example" role="application">
<div class="demo-section k-header">
<h2>Invite Attendees</h2>
<label for="required">Required</label>
<select id="required" multiple="multiple" data-placeholder="Select attendees...">
<option selected>Steven White</option>
<option>Nancy King</option>
<option>Nancy Davolio</option>
<option>Robert Davolio</option>
<option>Michael Leverling</option>
<option>Andrew Callahan</option>
<option>Michael Suyama</option>
<option selected>Anne King</option>
<option>Laura Peacock</option>
<option>Robert Fuller</option>
<option>Janet …
Run Code Online (Sandbox Code Playgroud) 我有两个divs
包含width: 50%
在容器中的文本内容。问题是当文本内容改变时,宽度也会改变:
我的容器的固定宽度等于200px
:
.container {
display: block;
width: 200px;
}
.text1 {
background-color: red;
width: 50%;
display: inline;
}
.text2 {
background-color: blue;
width: 50%;
display: inline;
}
Run Code Online (Sandbox Code Playgroud)
<div class='container'>
<div class='text1'>Text1</div>
<div class='text2'>Text2</div>
</div>
Run Code Online (Sandbox Code Playgroud)
div
即使文本发生变化,如何保持子宽度的 50% ?链接到JSFiddle:
我创建了一个如下所示的角反应形式
组件.html
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<h5 class="card-title text-center">Login</h5>
<div class="form-group">
<label class="text-success" for="exampleInputEmail1"
>Email address</label
>
<input
type="email"
class="form-control"
id="exampleInputEmail1"
aria-describedby="emailHelp"
formControlName="email"
placeholder="Enter email"
/>
</div>
<div class="form-group">
<label class="text-danger" for="exampleInputPassword1"
>Password</label
>
<input
type="password"
class="form-control"
id="exampleInputPassword1"
formControlName="password"
placeholder="Password"
/>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
Run Code Online (Sandbox Code Playgroud)
组件.ts
constructor(private fb: FormBuilder) {}
public loginForm: FormGroup;
ngOnInit(): void {
this.initializeForm();
}
initializeForm = () => {
this.loginForm = this.fb.group({
age: [''],
email: ['', [Validators.required]],
password: ['', [Validators.required]],
});
this.loginForm.controls.email.disable();
this.loginForm.controls.password.disable();
}
onSubmit() …
Run Code Online (Sandbox Code Playgroud) javascript ×5
node.js ×2
angular ×1
arrays ×1
css ×1
forms ×1
hapijs ×1
html ×1
jestjs ×1
joi ×1
jquery ×1
kendo-ui ×1
multi-select ×1
unit-testing ×1