JSON Schema Validation Online vs C# Could not resolve schema reference

2

I've been working on a schema that needs to reference several other schema files and basically working in jsonschemavalidator.net and then copying bits back to a github repo. Today I arrived on a working version that I saved, you can see the schema and the data has a fail for an undefinedResource, this is expected as it's not defined in my schema.

https://www.jsonschemavalidator.net/s/JrLE3dI0

I have created a simple forms app, to test the code that I lifted from the View Source button on that page.


            WebClient wget = new System.Net.WebClient();
            Uri schemaUri = new System.Uri(textBox1.Text);
            var jsonSchema = wget.DownloadString(schemaUri.AbsoluteUri);
            JObject json = JObject.Parse(textBox3.Text);

            JSchema schema = JSchema.Parse(jsonSchema);
            IList<ValidationError> errors;
            bool valid = json.IsValid(schema, out errors);
            textBox2.Clear();
            if (valid == false)
            {
                foreach (var err in errors)
                {
                    textBox2.Text += (err.Path + "\r\n") + (err.Message) + "\r\n";
                }
            }
            else
            {
                textBox2.Text = json.ToString();
            }

It's nothing fancy and lives inside a click even for a button. Aside from removing the api stuff from the online source I feel this code should return the same results. Except when I pass in the schema that works online I get the following error:

Newtonsoft.Json.Schema.JSchemaReaderException
  HResult=0x80131500
  Message=Could not resolve schema reference 'https://raw.githubusercontent.com/jeffpatton1971/randomschema/main/resources/definedResource.json#'. Path 'properties.definedResources.items', line 10, position 16.
  Source=Newtonsoft.Json.Schema
  StackTrace:
   at Newtonsoft.Json.Schema.Infrastructure.JSchemaReader.ResolveDeferedSchema(DeferedSchema deferedSchema)
   at Newtonsoft.Json.Schema.Infrastructure.JSchemaReader.ResolveDeferedSchemas()
   at Newtonsoft.Json.Schema.Infrastructure.JSchemaReader.ReadRoot(JsonReader reader, Boolean resolveDeferedSchemas)
   at Newtonsoft.Json.Schema.JSchema.Load(JsonReader reader, JSchemaReaderSettings settings)
   at Newtonsoft.Json.Schema.JSchema.Parse(String json, JSchemaReaderSettings settings)
   at Newtonsoft.Json.Schema.JSchema.Parse(String json)
   at jsonTesting.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\JeffreyPatton\source\repos\jsonTesting\jsonTesting\Form1.cs:line 31
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at jsonTesting.Program.Main() in C:\Users\JeffreyPatton\source\repos\jsonTesting\jsonTesting\Program.cs:line 19

Any help would be greatly appreciated, all the referenced URLs work and returns properly in the online validator, so I feel I've done something silly in the small amount of code that I lifted.

json.net
asked on Stack Overflow Mar 31, 2021 by Jeff Patton

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0