Comments (20)
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
from njsonschema.
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.
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.
We could Item change so that it only modifies the Items property and sets or removes its collection with one item...
from njsonschema.
And what returns Item when Items contains multiple items?
from njsonschema.
Just remove the _Item _ property to avoid confusion? Just have Items (for either single or more items).
from njsonschema.
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.
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.
(Please use three ` at the beginning and end of source code blocks here so that they are formatted correctly)
from njsonschema.
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.
Yeah, so would you be populating _Items _too now (even if there's a single Item)?
from njsonschema.
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.
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.
Yes, but the semantics is different even if the type is always Array
:
- C#
Item
/ JSONitems
is schema:- Array with uniform items (all have the same schema)
- C#
Items
/ JSONitems
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.
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.
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.
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.
Yes, but this should work with NJsonSchema too if you set the Item
property...
from njsonschema.
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.
Got you. I guess Newtonsoft conditionally handles it somehow making use of other properties like UniqueItems
from njsonschema.
Related Issues (20)
- Infinite loop in RenamePropertyWithSameNameAsClass
- C#11 required member support when generating a schema from a type
- Could not find the JSON path of a referenced schema: Manually referenced schemas must be added to the 'Definitions' of a parent schema
- Repository in nuget package is point not to repository HOT 1
- System.TypeLoadException: Could not load type 'NJsonSchema.Annotations.JsonSchemaFlattenAttribute' from assembly 'NJsonSchema, Version=11.0.0.0
- Question: Retrieve JSON Schema definition for a Json Element/Node?
- System.MissingMethodException: Method not found: 'Void NJsonSchema.JsonSchema.set_Type(NJsonSchema.JsonObjectType) HOT 1
- Bug: Public Fields not showing up when using System.Text.Json
- Values set in custom Schema Processor are overwritten during schema generation
- CSharpClientGenerator and CSharpGenerator not working together
- Using the generic type 'JsonInheritanceConverter<TBase>' requires 1 type argument HOT 2
- NJsonSchema generated code throwing The JSON value could not be converted HOT 1
- Improve base package readme
- AllowAdditionalProperties - not working
- Format "guid" vs "uuid"
- Validation Failures On All JSON Schema Draft Versions
- Private expression-bodied read-only properties should be ignored HOT 1
- Unable to generate document when XmlElement[] is a property of the class
- JsonProperty not working on version 11.0.0 (working on 10.9.0) HOT 1
- Referenced schema could not be loaded when defined in external schema file
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from njsonschema.