Code Coverage Statistics for Source File
Newtonsoft.Json\Schema\JsonSchemaModel.cs
Symbol Coverage: 100.00% (32 of 32)
Branch Coverage: 100.00% (54 of 54)
Cyclomatic Complexity Avg: 1.31 Max:10
Code Lines: 30
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
#region License |
|
2 |
// Copyright (c) 2007 James Newton-King |
|
3 |
// |
|
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 |
|
11 |
// conditions: |
|
12 |
// |
|
13 |
// The above copyright notice and this permission notice shall be |
|
14 |
// included in all copies or substantial portions of the Software. |
|
15 |
// |
|
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. |
|
24 |
#endregion |
|
25 |
||
26 |
using System; |
|
27 |
using System.Collections.Generic; |
|
28 |
using System.Linq; |
|
29 |
using Newtonsoft.Json.Linq; |
|
30 |
using Newtonsoft.Json.Utilities; |
|
31 |
||
32 |
namespace Newtonsoft.Json.Schema |
|
33 |
{ |
|
34 |
internal class JsonSchemaModel |
|
35 |
{ |
|
36 |
public bool Optional { get; set; } |
|
37 |
public JsonSchemaType Type { get; set; } |
|
38 |
public int? MinimumLength { get; set; } |
|
39 |
public int? MaximumLength { get; set; } |
|
40 |
public int? MaximumDecimals { get; set; } |
|
41 |
public double? Minimum { get; set; } |
|
42 |
public double? Maximum { get; set; } |
|
43 |
public int? MinimumItems { get; set; } |
|
44 |
public int? MaximumItems { get; set; } |
|
45 |
public IList<string> Patterns { get; set; } |
|
46 |
public IList<JsonSchemaModel> Items { get; set; } |
|
47 |
public IDictionary<string, JsonSchemaModel> Properties { get; set; } |
|
48 |
public JsonSchemaModel AdditionalProperties { get; set; } |
|
49 |
public bool AllowAdditionalProperties { get; set; } |
|
50 |
public IList<JToken> Enum { get; set; } |
|
51 |
public JsonSchemaType Disallow { get; set; } |
|
52 |
||
53 |
170 |
public JsonSchemaModel()
|
54 |
{ |
|
55 |
170 |
Type = JsonSchemaType.Any;
|
56 |
170 |
AllowAdditionalProperties = true;
|
57 |
170 |
Optional = true;
|
58 |
170 |
}
|
59 |
||
60 |
public static JsonSchemaModel Create(IList<JsonSchema> schemata) |
|
61 |
{ |
|
62 |
170 |
JsonSchemaModel model = new JsonSchemaModel();
|
63 |
||
64 |
170 |
foreach (JsonSchema schema in schemata)
|
65 |
{ |
|
66 |
187 |
Combine(model, schema);
|
67 |
} |
|
68 |
||
69 |
170 |
return model;
|
70 |
170 |
}
|
71 |
||
72 |
private static void Combine(JsonSchemaModel model, JsonSchema schema) |
|
73 |
{ |
|
74 |
187 |
model.Optional = model.Optional && (schema.Optional ?? false);
|
75 |
187 |
model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any);
|
76 |
||
77 |
187 |
model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
|
78 |
187 |
model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength);
|
79 |
187 |
model.MaximumDecimals = MathUtils.Min(model.MaximumDecimals, schema.MaximumDecimals);
|
80 |
187 |
model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum);
|
81 |
187 |
model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum);
|
82 |
187 |
model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
|
83 |
187 |
model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems);
|
84 |
187 |
model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties;
|
85 |
187 |
if (schema.Enum != null)
|
86 |
{ |
|
87 |
9 |
if (model.Enum == null)
|
88 |
7 |
model.Enum = new List<JToken>();
|
89 |
||
90 |
9 |
model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer());
|
91 |
} |
|
92 |
187 |
model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None);
|
93 |
||
94 |
187 |
if (schema.Pattern != null)
|
95 |
{ |
|
96 |
9 |
if (model.Patterns == null)
|
97 |
7 |
model.Patterns = new List<string>();
|
98 |
||
99 |
9 |
model.Patterns.AddDistinct(schema.Pattern);
|
100 |
} |
|
101 |
187 |
}
|
102 |
} |
|
103 |
} |