Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema Validation errors on Ver. 3.0.14 #293

Open
whiletrue0222 opened this issue May 10, 2022 · 0 comments
Open

Schema Validation errors on Ver. 3.0.14 #293

whiletrue0222 opened this issue May 10, 2022 · 0 comments

Comments

@whiletrue0222
Copy link

whiletrue0222 commented May 10, 2022

As per the title, Schema Validation errors seem to be occurring on version 3.0.14. JToken.IsValid seems to be fine on ver. 3.0.13. The error that pops up on 3.0.14 is as follows:

JSON does not match schema from 'then'. Path 'Goo', line 3, position 10.

I have attached some sample code below.

using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;

JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();

resolver.Add(new Uri("CommonEnum.schema.json", UriKind.RelativeOrAbsolute), @"
{
  '$schema': 'http://json-schema.org/draft/2019-09/schema#',
  'type': 'object',
  'definitions': {
    'FooType': {
      'type': 'string',
      'enum': [ 'A', 'B' ]
    }
  }
}
");
resolver.Add(new Uri("DataCommon.schema.json", UriKind.RelativeOrAbsolute),
  @"
{
  '$schema': 'http://json-schema.org/draft/2019-09/schema#',
  'type': 'object',
  'properties': {
    'Goo': {
      'type': 'object',
      'properties': {
        'm1': { 'type': 'integer', 'minimum': 0 },
        'm2': { 'type': 'integer', 'minimum': 1 }
      }
    }
  }
}
");

var schemaText = @"
{
  '$schema': 'http://json-schema.org/draft/2019-09/schema#',
  'type': 'object',
  'properties': {
    'Type': {
      '$ref': 'CommonEnum.schema.json#/definitions/FooType'
    },
    'Goo': {
      '$ref': 'DataCommon.schema.json#/properties/Goo',
      'if': {
        'properties': {
          'Type': { 'enum': ['A'] }
        }
      },
      'then': { 'required': [ 'Goo' ] }
    }
  }
}";
var settings = new JSchemaReaderSettings
{
  Resolver = resolver,
};
JSchema schema = JSchema.Parse(schemaText, settings);
var schemaString = schema.ToString();
var jsonText = @"{
  'Type': 'A',
  'Goo': {
    'm1': 30,
    'm2': 100
  }
}";
var json = JToken.Parse(jsonText);

var isValid = json.IsValid(schema, out IList<string> errorMessages);

Console.WriteLine(isValid);
foreach (var errorMessage in errorMessages)
{
  Console.WriteLine(errorMessage);
}

Assistance is much appreciated.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant