2 // Copyright (c) 2007 James Newton-King
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
27 using System.Collections.Generic;
31 using Newtonsoft.Json.Schema;
32 using NUnit.Framework;
34 namespace Newtonsoft.Json.Tests.Schema
36 public class JsonSchemaTests : TestFixtureBase
42 JsonSchemaResolver resolver = new JsonSchemaResolver();
47 ""additionalProperties"":{}
50 JsonSchema first = JsonSchema.Parse(json, resolver);
56 ""extends"":{""$ref"":""first""},
57 ""additionalProperties"":{""type"":""string""}
60 JsonSchema second = JsonSchema.Parse(json, resolver);
61 Assert.AreEqual(first, second.Extends);
67 ""extends"":{""$ref"":""second""},
68 ""additionalProperties"":false
71 JsonSchema third = JsonSchema.Parse(json, resolver);
72 Assert.AreEqual(second, third.Extends);
73 Assert.AreEqual(first, third.Extends.Extends);
75 StringWriter writer = new StringWriter();
76 JsonTextWriter jsonWriter = new JsonTextWriter(writer);
77 jsonWriter.Formatting = Formatting.Indented;
79 third.WriteTo(jsonWriter, resolver);
81 string writtenJson = writer.ToString();
85 ""additionalProperties"": false,
91 StringWriter writer1 = new StringWriter();
92 JsonTextWriter jsonWriter1 = new JsonTextWriter(writer1);
93 jsonWriter1.Formatting = Formatting.Indented;
95 third.WriteTo(jsonWriter1);
97 writtenJson = writer1.ToString();
100 ""type"": ""object"",
101 ""additionalProperties"": false,
104 ""type"": ""object"",
105 ""additionalProperties"": {
110 ""type"": ""object"",
111 ""additionalProperties"": {}
117 public void WriteTo_AdditionalProperties()
119 StringWriter writer = new StringWriter();
120 JsonTextWriter jsonWriter = new JsonTextWriter(writer);
121 jsonWriter.Formatting = Formatting.Indented;
123 JsonSchema schema = JsonSchema.Parse(@"{
124 ""description"":""AdditionalProperties"",
125 ""type"":[""string"", ""integer""],
126 ""additionalProperties"":{""type"":[""object"", ""boolean""]}
129 schema.WriteTo(jsonWriter);
131 string json = writer.ToString();
134 ""description"": ""AdditionalProperties"",
139 ""additionalProperties"": {
149 public void WriteTo_Properties()
151 JsonSchema schema = JsonSchema.Parse(@"{
152 ""description"":""A person"",
156 ""name"":{""type"":""string""},
160 ""items"": {""type"":""string""}
165 StringWriter writer = new StringWriter();
166 JsonTextWriter jsonWriter = new JsonTextWriter(writer);
167 jsonWriter.Formatting = Formatting.Indented;
169 schema.WriteTo(jsonWriter);
171 string json = writer.ToString();
174 ""description"": ""A person"",
175 ""type"": ""object"",
192 public void WriteTo_Enum()
194 JsonSchema schema = JsonSchema.Parse(@"{
195 ""description"":""Type"",
196 ""type"":[""string"",""array""],
198 ""enum"":[""string"",""object"",""array"",""boolean"",""number"",""integer"",""null"",""any""]
201 StringWriter writer = new StringWriter();
202 JsonTextWriter jsonWriter = new JsonTextWriter(writer);
203 jsonWriter.Formatting = Formatting.Indented;
205 schema.WriteTo(jsonWriter);
207 string json = writer.ToString();
210 ""description"": ""Type"",
230 public void WriteTo_CircularReference()
233 ""id"":""CircularReferenceArray"",
234 ""description"":""CircularReference"",
235 ""type"":[""array""],
236 ""items"":{""$ref"":""CircularReferenceArray""}
239 JsonSchema schema = JsonSchema.Parse(json);
241 StringWriter writer = new StringWriter();
242 JsonTextWriter jsonWriter = new JsonTextWriter(writer);
243 jsonWriter.Formatting = Formatting.Indented;
245 schema.WriteTo(jsonWriter);
247 string writtenJson = writer.ToString();
250 ""id"": ""CircularReferenceArray"",
251 ""description"": ""CircularReference"",
254 ""$ref"": ""CircularReferenceArray""
260 public void WriteTo_DisallowMultiple()
262 JsonSchema schema = JsonSchema.Parse(@"{
263 ""description"":""Type"",
264 ""type"":[""string"",""array""],
266 ""disallow"":[""string"",""object"",""array""]
269 StringWriter writer = new StringWriter();
270 JsonTextWriter jsonWriter = new JsonTextWriter(writer);
271 jsonWriter.Formatting = Formatting.Indented;
273 schema.WriteTo(jsonWriter);
275 string json = writer.ToString();
278 ""description"": ""Type"",
293 public void WriteTo_DisallowSingle()
295 JsonSchema schema = JsonSchema.Parse(@"{
296 ""description"":""Type"",
297 ""type"":[""string"",""array""],
302 StringWriter writer = new StringWriter();
303 JsonTextWriter jsonWriter = new JsonTextWriter(writer);
304 jsonWriter.Formatting = Formatting.Indented;
306 schema.WriteTo(jsonWriter);
308 string json = writer.ToString();
311 ""description"": ""Type"",
317 ""disallow"": ""any""
322 public void WriteTo_MultipleItems()
324 JsonSchema schema = JsonSchema.Parse(@"{
328 StringWriter writer = new StringWriter();
329 JsonTextWriter jsonWriter = new JsonTextWriter(writer);
330 jsonWriter.Formatting = Formatting.Indented;
332 schema.WriteTo(jsonWriter);
334 string json = writer.ToString();
345 public void ReadOptions()
347 JsonSchema schema = JsonSchema.Parse(@"{
348 ""type"": ""object"",
351 ""type"": ""integer"",
375 Assert.AreEqual(schema.Properties["x"].Options.Count, 3);
376 Assert.AreEqual(schema.Properties["x"].Options[0], "No");
377 Assert.AreEqual(schema.Properties["x"].Options[1], "Asc");
378 Assert.AreEqual(schema.Properties["x"].Options[-1], "Desc");
382 public void WriteTo_ExclusiveMinimum_ExclusiveMaximum()
384 JsonSchema schema = new JsonSchema();
385 schema.ExclusiveMinimum = true;
386 schema.ExclusiveMaximum = true;
388 StringWriter writer = new StringWriter();
389 JsonTextWriter jsonWriter = new JsonTextWriter(writer);
390 jsonWriter.Formatting = Formatting.Indented;
392 schema.WriteTo(jsonWriter);
394 string json = writer.ToString();
397 ""exclusiveMinimum"": true,
398 ""exclusiveMaximum"": true
403 public void WriteTo_PatternProperties()
405 JsonSchema schema = new JsonSchema();
406 schema.PatternProperties = new Dictionary<string, JsonSchema>
408 { "[abc]", new JsonSchema() }
411 StringWriter writer = new StringWriter();
412 JsonTextWriter jsonWriter = new JsonTextWriter(writer);
413 jsonWriter.Formatting = Formatting.Indented;
415 schema.WriteTo(jsonWriter);
417 string json = writer.ToString();
420 ""patternProperties"": {