You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
5.3 KiB

  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. using System;
  26. using System.Collections.Generic;
  27. using Newtonsoft.Json.Linq;
  28. using Newtonsoft.Json.Utilities;
  29. namespace Newtonsoft.Json.Schema
  30. {
  31. [Obsolete("JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.")]
  32. internal class JsonSchemaModel
  33. {
  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 JsonSchemaModel AdditionalItems { get; set; }
  51. public bool PositionalItemsValidation { get; set; }
  52. public bool AllowAdditionalProperties { get; set; }
  53. public bool AllowAdditionalItems { get; set; }
  54. public bool UniqueItems { get; set; }
  55. public IList<JToken> Enum { get; set; }
  56. public JsonSchemaType Disallow { get; set; }
  57. public JsonSchemaModel()
  58. {
  59. Type = JsonSchemaType.Any;
  60. AllowAdditionalProperties = true;
  61. AllowAdditionalItems = true;
  62. Required = false;
  63. }
  64. public static JsonSchemaModel Create(IList<JsonSchema> schemata)
  65. {
  66. JsonSchemaModel model = new JsonSchemaModel();
  67. foreach (JsonSchema schema in schemata)
  68. {
  69. Combine(model, schema);
  70. }
  71. return model;
  72. }
  73. private static void Combine(JsonSchemaModel model, JsonSchema schema)
  74. {
  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);
  78. model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
  79. model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength);
  80. // not sure what is the best way to combine divisibleBy
  81. model.DivisibleBy = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy);
  82. model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum);
  83. model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum);
  84. model.ExclusiveMinimum = model.ExclusiveMinimum || (schema.ExclusiveMinimum ?? false);
  85. model.ExclusiveMaximum = model.ExclusiveMaximum || (schema.ExclusiveMaximum ?? false);
  86. model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
  87. model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems);
  88. model.PositionalItemsValidation = model.PositionalItemsValidation || schema.PositionalItemsValidation;
  89. model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties;
  90. model.AllowAdditionalItems = model.AllowAdditionalItems && schema.AllowAdditionalItems;
  91. model.UniqueItems = model.UniqueItems || schema.UniqueItems;
  92. if (schema.Enum != null)
  93. {
  94. if (model.Enum == null)
  95. {
  96. model.Enum = new List<JToken>();
  97. }
  98. model.Enum.AddRangeDistinct(schema.Enum, JToken.EqualityComparer);
  99. }
  100. model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None);
  101. if (schema.Pattern != null)
  102. {
  103. if (model.Patterns == null)
  104. {
  105. model.Patterns = new List<string>();
  106. }
  107. model.Patterns.AddDistinct(schema.Pattern);
  108. }
  109. }
  110. }
  111. }