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.
26
using System.Collections.Generic;
27
using Newtonsoft.Json.Linq;
28
using Newtonsoft.Json.Utilities;
30
namespace Newtonsoft.Json.Schema
32
internal class JsonSchemaModel
34
public bool Required { get; set; }
35
public JsonSchemaType Type { get; set; }
36
public int? MinimumLength { get; set; }
37
public int? MaximumLength { get; set; }
38
public double? DivisibleBy { get; set; }
39
public double? Minimum { get; set; }
40
public double? Maximum { get; set; }
41
public bool ExclusiveMinimum { get; set; }
42
public bool ExclusiveMaximum { 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 IDictionary<string, JsonSchemaModel> PatternProperties { get; set; }
49
public JsonSchemaModel AdditionalProperties { get; set; }
50
public bool AllowAdditionalProperties { get; set; }
51
public IList<JToken> Enum { get; set; }
52
public JsonSchemaType Disallow { get; set; }
54
public JsonSchemaModel()
56
Type = JsonSchemaType.Any;
57
AllowAdditionalProperties = true;
61
public static JsonSchemaModel Create(IList<JsonSchema> schemata)
63
JsonSchemaModel model = new JsonSchemaModel();
65
foreach (JsonSchema schema in schemata)
67
Combine(model, schema);
73
private static void Combine(JsonSchemaModel model, JsonSchema schema)
75
// Version 3 of the Draft JSON Schema has the default value of Not Required
76
model.Required = model.Required || (schema.Required ?? false);
77
model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any);
79
model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
80
model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength);
82
// not sure what is the best way to combine divisibleBy
83
model.DivisibleBy = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy);
85
model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum);
86
model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum);
87
model.ExclusiveMinimum = model.ExclusiveMinimum || (schema.ExclusiveMinimum ?? false);
88
model.ExclusiveMaximum = model.ExclusiveMaximum || (schema.ExclusiveMaximum ?? false);
90
model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
91
model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems);
92
model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties;
93
if (schema.Enum != null)
95
if (model.Enum == null)
96
model.Enum = new List<JToken>();
98
model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer());
100
model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None);
102
if (schema.Pattern != null)
104
if (model.Patterns == null)
105
model.Patterns = new List<string>();
107
model.Patterns.AddDistinct(schema.Pattern);
b'\\ No newline at end of file'