自定义 JsonValidator 中的访问字段 parent
Access field parent in custom JsonValidator
我正在使用 Newtonsoft 出色的 JsonSchema 库并尝试为 greaterthanfield
另一个字段验证构建自定义验证器。
为了做到这一点,我显然需要从 Validate(JToken value, JsonValidatorContext context)
方法访问另一个字段。然而,JToken
目前没有任何 parent 信息可以找到所需的兄弟姐妹。同样,JsonValidatorContext
仅对架构没有任何对验证数据的引用。
我希望能够:
value.Parent["siblingkey"]
但看起来 JToken
实际上只是那个无法访问其余已解析数据的令牌。
有谁知道实现这种验证器的方法吗?一个引用其他字段的。其他示例类似于 combinedmaxlength
等...
我刚遇到这个问题。我在 属性 级别上应用自定义验证器(使用 "Format"),但没有用。
{
"$schema": "http://json-schema.org/draft-04/schema",
"title": "JSON Schema for custom rule",
"type": "object",
"properties": {
"Prop1": {
"type": "string"
},
"Prop2": {
"type": "string",
"format": "YourCustomValidatorName"
}
}
}
您需要将自定义验证器应用于整个模式。
{
"$schema": "http://json-schema.org/draft-04/schema",
"title": "JSON Schema for custom rule",
"type": "object",
"properties": {
"Prop1": {
"type": "string"
},
"Prop2": {
"type": "string"
}
},
"format": "YourCustomValidatorName"
}
然后在您的自定义验证器中 class 您将能够访问所有属性
public override void Validate(JToken value, JsonValidatorContext context)
{
var prop1 = value.SelectToken("..Prop1")?.Value<string>();
var prop2 = value.SelectToken("..Prop2")?.Value<string>();
// Rest of your logic...
}
我一直在玩,因为我 运行 遇到了同样的问题。
我发现自定义验证器系统有一些奇怪的行为,其中 JToken 树正在根据您的验证器可以验证的最高父节点 进行重建(其中 CanValidate returns true )
这意味着如果我们可以欺骗系统相信您的验证器可以验证根令牌 和 您的特定令牌,您的树将被水合。
public class TestValidator : JsonValidator
{
public override bool CanValidate(JSchema schema)
{
// we assume every schema has a title/schemaversion in its root object.
return schema.Title != null || schema.SchemaVersion != null || schema.ExtensionData.ContainsKey("greaterthanfield");
}
public override void Validate(JToken value, JsonValidatorContext context)
{
// we should ignore the "root token validation"
if (!context.Schema.ExtensionData.ContainsKey("greaterthanfield"))
return;
// value.Parent is hydrated now
}
}
我正在使用 Newtonsoft 出色的 JsonSchema 库并尝试为 greaterthanfield
另一个字段验证构建自定义验证器。
为了做到这一点,我显然需要从 Validate(JToken value, JsonValidatorContext context)
方法访问另一个字段。然而,JToken
目前没有任何 parent 信息可以找到所需的兄弟姐妹。同样,JsonValidatorContext
仅对架构没有任何对验证数据的引用。
我希望能够:
value.Parent["siblingkey"]
但看起来 JToken
实际上只是那个无法访问其余已解析数据的令牌。
有谁知道实现这种验证器的方法吗?一个引用其他字段的。其他示例类似于 combinedmaxlength
等...
我刚遇到这个问题。我在 属性 级别上应用自定义验证器(使用 "Format"),但没有用。
{
"$schema": "http://json-schema.org/draft-04/schema",
"title": "JSON Schema for custom rule",
"type": "object",
"properties": {
"Prop1": {
"type": "string"
},
"Prop2": {
"type": "string",
"format": "YourCustomValidatorName"
}
}
}
您需要将自定义验证器应用于整个模式。
{
"$schema": "http://json-schema.org/draft-04/schema",
"title": "JSON Schema for custom rule",
"type": "object",
"properties": {
"Prop1": {
"type": "string"
},
"Prop2": {
"type": "string"
}
},
"format": "YourCustomValidatorName"
}
然后在您的自定义验证器中 class 您将能够访问所有属性
public override void Validate(JToken value, JsonValidatorContext context)
{
var prop1 = value.SelectToken("..Prop1")?.Value<string>();
var prop2 = value.SelectToken("..Prop2")?.Value<string>();
// Rest of your logic...
}
我一直在玩,因为我 运行 遇到了同样的问题。
我发现自定义验证器系统有一些奇怪的行为,其中 JToken 树正在根据您的验证器可以验证的最高父节点 进行重建(其中 CanValidate returns true )
这意味着如果我们可以欺骗系统相信您的验证器可以验证根令牌 和 您的特定令牌,您的树将被水合。
public class TestValidator : JsonValidator
{
public override bool CanValidate(JSchema schema)
{
// we assume every schema has a title/schemaversion in its root object.
return schema.Title != null || schema.SchemaVersion != null || schema.ExtensionData.ContainsKey("greaterthanfield");
}
public override void Validate(JToken value, JsonValidatorContext context)
{
// we should ignore the "root token validation"
if (!context.Schema.ExtensionData.ContainsKey("greaterthanfield"))
return;
// value.Parent is hydrated now
}
}