我有低于json
的输入,其中我有意不发送";名称";属性
{
"IsActive": false,
"Contracts": [
{
"Id": 1
"IsActive": true,
"Name": "DH0",
},
{
"Id": 2
"IsActive": true
}
}
NewtonSoft JSON验证器成功验证了Name
的空值!
"对象中缺少必需的属性:Name。路径",第11行,位置1;
但是我想得到";Id";其中缺少Name
属性。
我们该怎么做?
当前代码(根据Robin Windey评论更新代码(
string schemaJson = File.ReadAllText("schema/qmcrmdataschema.json");
string inputJson = File.ReadAllText("schema/sample.json");
JSchemaReaderSettings settings = new JSchemaReaderSettings
{
Validators = new List<JsonValidator> { new MyValidator() }
};
JSchema schema = JSchema.Parse(schemaJson, settings );
JObject user = JObject.Parse(inputJson);
IList<ValidationError> messages;
bool valid = user.IsValid(schema, out messages);
Console.WriteLine(messages[0].Message);
自定义验证器
public class MyValidator : JsonValidator
{
public override void Validate(JToken value, JsonValidatorContext context)
{
}
public override bool CanValidate(JSchema schema)
{
return (schema.Format == "something");
}
}
但是自定义代码不会被调试器击中!
我缺少什么?
以下对我有效:
MyValidator.cs
internal class MyValidator : JsonValidator
{
private int _counter;
public override void Validate(JToken value, JsonValidatorContext context)
{
Console.WriteLine("Validate");
context.RaiseError($"MyCustomError {_counter++}");
}
public override bool CanValidate(JSchema schema)
{
Console.WriteLine("CanValidate");
return true;
}
}
schema.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"IsActive": {
"type": "boolean"
},
"Contracts": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"Id": {
"type": "integer"
},
"IsActive": {
"type": "boolean"
},
"Name": {
"type": "string"
}
},
"required": [
"Id",
"IsActive",
"Name"
]
},
{
"type": "object",
"properties": {
"Id": {
"type": "integer"
},
"IsActive": {
"type": "boolean"
}
},
"required": [
"Id",
"IsActive"
]
}
]
}
},
"required": [
"IsActive",
"Contracts",
"Name"
]
}
input.json
{
"IsActive": false,
"Contracts": [
{
"Id": 1,
"IsActive": true,
"Name": "DH0"
},
{
"Id": 2,
"IsActive": true
}
]
}
Program.cs
static void Main(string[] args)
{
var schemaJson = File.ReadAllText("schema.json");
var inputJson = File.ReadAllText("input.json");
var settings = new JSchemaReaderSettings
{
Validators = new List<JsonValidator> { new MyValidator() }
};
var schema = JSchema.Parse(schemaJson, settings);
var user = JObject.Parse(inputJson);
bool valid = user.IsValid(schema, out IList<ValidationError> errors);
if (!valid)
{
Console.WriteLine("Found errors:");
Console.WriteLine(string.Join(Environment.NewLine, errors.Select(e => e.Message)));
}
}
然后,当运行dotnet run
时,它给了我以下控制台输出:
CanValidate
CanValidate
CanValidate
CanValidate
CanValidate
CanValidate
CanValidate
CanValidate
CanValidate
CanValidate
Validate
Validate
Validate
Validate
Validate
Validate
Validate
Validate
Validate
Validate
Found errors:
MyCustomError 0
MyCustomError 1
MyCustomError 2
MyCustomError 3
MyCustomError 4
MyCustomError 5
MyCustomError 6
MyCustomError 7
MyCustomError 8
MyCustomError 9
Required properties are missing from object: Name.
您可以在最后一行中看到,尽管我们刚刚在JSchemaReaderSettings
类的Validators
集合中定义了自己的JsonValidator
,但当前仍进行默认验证。我还没有设法阻止这种行为,但我认为这应该是一个很好的起点。
Btw:您的input.json
中出现语法错误。缺少Contracts
数组的右括号和Id
属性后面的逗号。