Newtonsoft.Json.Schema
Newtonsoft.Json.Schema copied to clipboard
Different results between web validator and NuGet package
I am getting different results between the web validator and my validation in code, the schema is:
{
"title": "Response",
"description": "",
"references": {
"KeyValue": {
"title": "KeyValue",
"description": "",
"type": "object",
"properties": {
"Key": {
"title": "Key",
"description": "",
"type": "string",
"minLength": 1,
"maxLength": 100
},
"Value": {
"title": "Value",
"description": "",
"type": "string",
"minLength": 1,
"maxLength": 100
}
},
"required": [
"Key",
"Value"
]
},
"Person": {
"title": "Person",
"description": "",
"type": "object",
"properties": {
"FirstName": {
"title": "FirstName",
"description": "",
"type": "string",
"minLength": 1,
"maxLength": 100
}
},
"required": [
"FirstName"
]
},
"ValidResponse": {
"title": "ValidResponse",
"description": "",
"type": "object",
"properties": {
"Headers": {
"title": "Headers",
"description": "",
"items": {
"$ref": "#/references/KeyValue"
}
},
"Body": {
"title": "Body",
"description": "",
"$ref": "#/references/Person"
}
},
"required": [
"Headers",
"Body"
]
},
"Error404": {
"title": "Error404",
"description": "",
"type": "object",
"properties": {
"Code": {
"title": "Code",
"description": "",
"type": "number"
},
"Message": {
"title": "Message",
"description": "",
"type": "string",
"minLength": 1,
"maxLength": 100
}
},
"required": [
"Code",
"Message"
]
}
},
"oneOf": [
{
"title": "ValidResponse",
"$ref": "#/references/ValidResponse"
},
{
"title": "Error404",
"$ref": "#/references/Error404"
}
]
}
The JSON I am trying to validate:
{
'Headers': [
{
'Key': 'Token',
'Value': 'Foo'
}
],
'Body': {
'FirstName':'Peter'
}
}
This passes (and should pass) on the web validator, but when I try to validate this using C#:
IList<ValidationError> errors;
bool valid = person.IsValid(schema, out errors);
Assert.True(valid);
Assert.Empty(errors);
I get the following error:
JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: 0, 1. Path '', line 2, position 1.
I am using:
- Newtonsoft.Json.Schema (3.0.14)
- TargetFramework: net5.0
- Nullable: enable
- LangVersion: latest
Looks valid to me: https://dotnetfiddle.net/i3NeRr
I have found another weird thing, I've gotten the schema text by doing a .ToString()
.
If I now change my code of the test suite to:
JSchema _schema = getSchema();
string schemaText = _schema.ToString();
JSchema schema = JSchema.Parse(schemaText);
The tests succeed. If I just use _schema
the tests fail.
@Baudin999 , how did you manage to install/use the version 3.0.14 with net5.0?
When I try to install (via command line on MAC) I'm getting the error below:
dotnet add package Newtonsoft.Json.Schema --version 3.0.14
error: NU1100: Unable to resolve 'Newtonsoft.Json.Schema (>= 3.0.14)' for 'net5.0'. error: Package 'Newtonsoft.Json.Schema' is incompatible with 'all' frameworks in project '/wa/CLOUD/AZURE/APPLICATIONS/SUBSCRIPTIONS-DB/data-model/models/models.csproj'.
Thank you
It just seemed to work... I did nothing special to install that version. I did use the Visual Studio Nuget Manager and not the CLI tool
:D yes, exactly... I got it working using the VS 2019.... but not with VS Code and CLI... :/
Thank you