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

public List<string> operators { get; set; } is causing validation errors #60

Closed
dyetube opened this issue Apr 28, 2016 · 15 comments
Closed

Comments

@dyetube
Copy link

dyetube commented Apr 28, 2016

I'm generating a json schema from a class and everything works except: public Dictionary<string, object> values { get; set; }.

For some reason it converts it to:

"values": { "type": [ "null", "object" ], "additionalProperties": { "type": "object" } },.

What the json should look like is this:

"values": { "employee Only": "Employee Only", "employee and Spouse": "Employee and Spouse", "employee and Children": "Employee and Children", "employee and Family": "Employee and Family" }

This is causing an error to be thrown when it shouldn't.

If you want me to post the fully convert Json Schema, I can but it is quite long. Hopefully this makes sense. I'm thinking the "additionalProperties" is what is causing the error. Am I doing something wrong maybe?

@RicoSuter
Copy link
Owner

Looks correct to me, can you post exception details?

@dyetube
Copy link
Author

dyetube commented Apr 28, 2016

It says "ArrayItemNotValid"
screenshot
Does this screenshot help or do you need more info?

@RicoSuter
Copy link
Owner

RicoSuter commented Apr 28, 2016

You have a validation error in the filters property not in the values property... Can you post its data and schema as well?

@RicoSuter
Copy link
Owner

Clarification: JSON Schema does not know dictionaries. The schema describes an object which can have any properties (i.e. the dictionary keys) of type object (i.e. the dictionary values)... Because the property is nullable, its JSON Schema type can be null or object (as described).

@dyetube
Copy link
Author

dyetube commented Apr 28, 2016

Post the wrong screenshot. I know I do have an error there, I've got that fixed. maybe this screenshot will help better?

screenshot2

screenshot3

@RicoSuter
Copy link
Owner

RicoSuter commented Apr 28, 2016

Ok, I see the problem. Probably the .NET string is not recognized as JSON Schema object...

@dyetube
Copy link
Author

dyetube commented Apr 28, 2016

The reason it can be null or object is that I didn't make it required. If it is Null, I get no errors, I only get errors when I populate it with key:value pairs as an object.

@RicoSuter
Copy link
Owner

I have to implement a Unit Test... I'll report back...

@dyetube
Copy link
Author

dyetube commented Apr 28, 2016

Ok. Thanks!

@RicoSuter
Copy link
Owner

According to http://jsonschemalint.com/draft4/ the JSON Schema validation is correct for the given data:

image

So we have to change the JSON Schema generator...

@RicoSuter
Copy link
Owner

I've added the following unit test:

    public class DictTest
    {
        public Dictionary<string, object> values { get; set; }
    }

    [TestMethod]
    public void When_dictionary_value_is_null_then_string_values_are_allowed()
    {
        //// Arrange
        var schema = JsonSchema4.FromType<DictTest>();
        var schemaData = schema.ToJson();

        var data = @"{
            ""values"": { 
                ""key"": ""value"", 
            }
        }";

        //// Act
        var errors = schema.Validate(data);

        //// Assert
        Assert.AreEqual(0, errors.Count);
    }

Is this correct?

@RicoSuter
Copy link
Owner

Commit: 6406618

@RicoSuter
Copy link
Owner

v2.11 now available via NuGet, please try with this version and report if it works...

@dyetube
Copy link
Author

dyetube commented Apr 29, 2016

Sorry it took a while to get back with you on this. Yes, this fixed the issue and now dictionary works correctly.

@dyetube dyetube closed this as completed Apr 29, 2016
@RicoSuter
Copy link
Owner

Nice. Thx for reporting the issue and using the library...

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

No branches or pull requests

2 participants