在Vue.js文档中,有一个自定义输入组件的示例.我试图找出如何为这样的组件编写单元测试.组件的用法如下所示
<currency-input v-model="price"></currency-input>
Run Code Online (Sandbox Code Playgroud)
完整的实施可以在https://vuejs.org/v2/guide/components.html#Form-Input-Components-using-Custom-Events找到
文件说
因此,对于要使用的组件
v-model,它应该(这些可以在2.2.0+中配置):
- 接受价值道具
- 使用新值发出输入事件
我如何编写一个单元测试来确保我已经编写了这个组件以便它可以使用v-model?理想情况下,我不想专门测试这两个条件,我想测试一下行为,当组件内的值发生变化时,它也会在模型中发生变化.
有人可以解释一下JSON Schema 中 $dynamicRef 关键字的用途吗
有关实际使用,请参阅 JSON Schema 元架构本身。
它使用 $dynamicAnchor 和 $dynamicRef。
核心架构如下所示
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://json-schema.org/draft/2020-12/schema",
---- <snip>
"$dynamicAnchor": "meta", <-- first usage here
---- <snip>
"allOf": [
{"$ref": "meta/core"},
---- <snip>
]
---- <snip>
}
Run Code Online (Sandbox Code Playgroud)
meta/core(它被“包含”在allOf看起来像这样的
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://json-schema.org/draft/2020-12/meta/core",
---- <snip>
"$dynamicAnchor": "meta", <-- second usage here
---- <snip>
"properties": {
---- <snip>
"$defs": {
"type": "object",
"additionalProperties": { "$dynamicRef": "#meta" } <-- reference here
---- <snip>
}
} …Run Code Online (Sandbox Code Playgroud) 我正在开发一个需要定义 json-ld 架构的 React 应用程序。我通过 API 获取架构字符串,并且需要将适当的脚本标记添加到我的页面。
我的架构字符串看起来像这样:
[{"@context": "http://schema.org"}]
Run Code Online (Sandbox Code Playgroud)
我希望这会被翻译成:
<script type="application/ld+json">
[{"@context": "http://schema.org"}]
</script>
Run Code Online (Sandbox Code Playgroud)
然而,我在处理双引号时遇到了麻烦,因为它们被转换为等效的 html -"
这就是我的代码的样子:
schemaString = "[{\"@context\": \"http://schema.org\"}]";
render() {
return (<div>{schemaString &&
<script type="application/ld+json">
{schemaString}
</script>}
</div>)
}
Run Code Online (Sandbox Code Playgroud)
生成的html为:
<div>
<script type="application/ld+json">
[{"@context": "http://schema.org"}]
</script>
</div>
Run Code Online (Sandbox Code Playgroud)
关于我在这里缺少什么的任何指示吗?谢谢!
我有类似的东西:
let moduleId
moduleRackOutputs.forEach((output) => {
if (!moduleId) {
moduleId = output.moduleId
} else if (output.moduleId !== moduleId) {
errors.add([
'The drawing contains more than one module type. Multiple module types are not yet supported by the PVsyst RPA.'
])
}
})
Run Code Online (Sandbox Code Playgroud)
我想将其转换为Joi模式。我将如何实现这个目标?
谢谢
我的 Json 架构
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"userInfo": {
"type": "object",
"properties": {
"firstName": { "type": "string" },
"lastName": { "type": "string" },
"emailAddress":{ "type": "string" }
},
"required": ["firstName", "lastName", "emailAddress"]
},
"userPassword": {
"type": "object",
"properties": {
"password": { "type": "string" },
"confirmPassword": { "type": "string" }
}
}
},
"type": "object",
"properties": {
"standaloneDeveloper": {
"$ref": "#/definitions/userInfo",
"$ref": "#/definitions/userPassword"
}
}
}
Run Code Online (Sandbox Code Playgroud)
数据总是被#/definitions/userPassword覆盖
我使用此模式得到以下输出
{
"standaloneDeveloper": {
"password": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC",
"confirmPassword": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC"
}
}
Run Code Online (Sandbox Code Playgroud)
预期产出
{ …Run Code Online (Sandbox Code Playgroud) 我想pydantic用于在 api 和数据存储之间处理数据(双向),因为它很好地支持我关心的几种类型,这些类型不是原生的 json-serializable。它比当前方法具有更好的读取/验证支持,但我还需要创建 json-serializabledict对象来写出。
from uuid import UUID, uuid4
from pydantic import BaseModel
class Model(BaseModel):
the_id: UUID
instance = Model(the_id=uuid4())
print("1: %s" % instance.dict()
print("2: %s" % instance.json()
Run Code Online (Sandbox Code Playgroud)
印刷
{'the_id': UUID('4108356a-556e-484b-9447-07b56a664763')}
>>> inst.json()
'{"the_id": "4108356a-556e-484b-9447-07b56a664763"}'
Run Code Online (Sandbox Code Playgroud)
我喜欢以下内容:
{"the_id": "4108356a-556e-484b-9447-07b56a664763"} # eg "json-compatible" dict
Run Code Online (Sandbox Code Playgroud)
看来,虽然pydantic拥有所有的映射,但是我找不到序列化的任何使用标准之外json〜递归编码器(json.dumps( ... default=pydantic_encoder))的pydantic/main.py。但我更愿意保留一个库来验证 raw->obj(pydantic 在这方面很棒)以及 obj->raw(dict),这样我就不必管理多个序列化映射。我想我可以实现类似于json使用编码器的东西,但这应该是一个常见的用例吗?
其他方法,例如dataclasses(builtin)+ 库,例如dataclasses_jsonschema向 提供此 ~ 序列化json-ready dict,但同样希望使用 pydantic 进行更强大的输入验证,同时保持对称。
如何自定义无效输入的错误消息?
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"username": {
"type": "string",
"pattern": "^[A-Za-z0-9-_.]+$",
"minLength": 3
},
"password": {
"type": "string",
"minLength": 8,
"pattern": "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d\\W]$"
}
},
"required": [
"username",
"password"
],
"errors": [
{
"property": "username",
"message": "min 3 characters, do not use spaces or special characters"
}
]
}
Run Code Online (Sandbox Code Playgroud)
例如,如果用户名输入不是必需的最小长度或不满足正则表达式模式,则显示一条自定义消息min 3 characters, do not use spaces or special characters
jsonschema ×3
javascript ×2
json ×2
joi ×1
json-ld ×1
pydantic ×1
python ×1
python-3.x ×1
reactjs ×1
schema.org ×1
validation ×1
vue.js ×1
vuejs2 ×1