Coder Social home page Coder Social logo

Comments (20)

RicoSuter avatar RicoSuter commented on August 14, 2024

If only a single schema is defined for an array, then you have to access it with the Item property instead of the Items property. See

https://github.com/NJsonSchema/NJsonSchema/blob/master/src/NJsonSchema/JsonSchema4.Serialization.cs#L78

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

The question here is whether these two properties should be merged together (as you originally expected).

The drawback is that a collection with a single item will be automatically converted to a single schema (without array) and thus as conversion roundtrip (i.e. deserialize schema => serialize schema) may fail (i.e. not the same output).

from njsonschema.

vamsitp avatar vamsitp commented on August 14, 2024

Even if there is a single schema defined for an array, shouldn't Item be part of the _Items _ collection ideally (_Items _ will just return the single _Item _ in the collection like NewtonSoft does)?

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

We could Item change so that it only modifies the Items property and sets or removes its collection with one item...

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

And what returns Item when Items contains multiple items?

from njsonschema.

vamsitp avatar vamsitp commented on August 14, 2024

Just remove the _Item _ property to avoid confusion? Just have Items (for either single or more items).

from njsonschema.

vamsitp avatar vamsitp commented on August 14, 2024

Regarding your comment:

The drawback is that a collection with a single item will be automatically converted to a single schema (without array) and thus as conversion roundtrip (i.e. deserialize schema => serialize schema) may fail (i.e. not the same output).

With NJsonSchema:
Console.WriteLine(JsonConvert.SerializeObject(njsonSchema, Formatting.Indented));
Prints...

{
  "typeName": "ClassRoom",
  "additionalProperties": false,
  "type": "object",
  "required": [
    "Id"
  ],
  "properties": {
    "Id": {
      "type": "integer"
    },
    "Name": {
      "type": "string"
    },
    "Size": {
      "type": [
        "integer",
        "null"
      ]
    },
    "Students": {
      "items": {
        "type": "object",
        "properties": {
          "Age": {
            "type": [
              "integer",
              "null"
            ]
          },
          "Name": {
            "type": [
              "null",
              "string"
            ]
          }
        }
      },```
      **"type": "array"**
    ```}
  },
  "$schema": "http://json-schema.org/draft-04/schema#"
}

With Newtonsoft Schema:
Console.WriteLine(JsonConvert.SerializeObject(jsonNetSchema, Formatting.Indented));
Prints...

{
  "typeName": "ClassRoom",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "Id": {
      "type": "integer"
    },
    "Name": {
      "type": "string"
    },
    "Size": {
      "type": [
        "integer",
        "null"
      ]
    },
    "Students": {```
     **"type": "array",**
      ```"items": {
        "type": "object",
        "properties": {
          "Age": {
            "type": [
              "integer",
              "null"
            ]
          },
          "Name": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      }
    }
  },
  "required": [
    "Id"
  ]
}

from njsonschema.

vamsitp avatar vamsitp commented on August 14, 2024

Actually, both of them have the Type:Array retained. Updated the formatting above. So it should not be a problem moving Item to Items?

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

(Please use three ` at the beginning and end of source code blocks here so that they are formatted correctly)

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

The output of both libraries is correct: Only the items is allowed on the JSON Schema... NJsonSchema introduces the Item property for better access/readability

from njsonschema.

vamsitp avatar vamsitp commented on August 14, 2024

Yeah, so would you be populating _Items _too now (even if there's a single Item)?

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

As you can see here:

http://spacetelescope.github.io/understanding-json-schema/reference/array.html

the two properties cannot be merge because

Item = Schema 

is not the same as

Items = new List { Schema }

This is because the Item schema validates every item in the array and Items validates a tuple...

from njsonschema.

vamsitp avatar vamsitp commented on August 14, 2024

All I'm saying is the Type of the property should decide how it's interpreted/represented. If I declare a Type as an Array, I'd expect it back in a collection (Items). It's confusing when we declare something as an array, but have to probe the Item property instead of Items. In the above example, what I've been using so far with NewtonSoft is property.Items to fetch properties recursively of all Items (since I declared Students as an Array). With NJsonSchema, I need to first check if property.Item != null, else, probe property.Items. Not a big deal, but a bit confusing...

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

Yes, but the semantics is different even if the type is always Array:

  • C# Item / JSON items is schema:
    • Array with uniform items (all have the same schema)
  • C# Items / JSON items is array of schemas:
    • Tuple array with different items

Having Item and Items with one schema is not the same and thus we need both... How does Json.NET solve this problem?

from njsonschema.

vamsitp avatar vamsitp commented on August 14, 2024

NewtonSoft: IList<JSchema> Items. (did not look into the details)
In NJsonSchema case, If Item was an array like you said, it would have been nice (but probably more confusing because of the singular name :-)), but it's just an instance of type JsonSchema4 - right?

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

The problem is, that the validator has to differentiate between a single item and a list with a single item and this is not possible with a single list property...

The only way to solve this is to make the Items property of type object so that assigning a schema or a list of schemes are allowed...

from njsonschema.

vamsitp avatar vamsitp commented on August 14, 2024

Yeah, that is what I was about to say...In example from the link above, setting the Type to object (instead of number) would mean I can have different objects with different properties? (which is what I have currently and NewtonSoft is handling fine)...

{ "type": "array", "items": { "type": "object" } }

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

Yes, but this should work with NJsonSchema too if you set the Item property...

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

Just to complete this.

Array validation

Schema

{ "type": "array", "items": { "type": "number" } }  

Data:

[5, true]

INVALID

Tuple validation

Schema

{ "type": "array", "items": [{ "type": "number" }] }  

Data:

[5, true]

VALID

Tested on http://jsonschemalint.com/draft4/

from njsonschema.

vamsitp avatar vamsitp commented on August 14, 2024

Got you. I guess Newtonsoft conditionally handles it somehow making use of other properties like UniqueItems

from njsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.