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.

352 lines
14 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 System.IO;
  28. using Newtonsoft.Json.Linq;
  29. using Newtonsoft.Json.Utilities;
  30. using System.Globalization;
  31. namespace Newtonsoft.Json.Schema
  32. {
  33. /// <summary>
  34. /// <para>
  35. /// An in-memory representation of a JSON Schema.
  36. /// </para>
  37. /// <note type="caution">
  38. /// JSON Schema validation has been moved to its own package. See <see href="http://www.newtonsoft.com/jsonschema">http://www.newtonsoft.com/jsonschema</see> for more details.
  39. /// </note>
  40. /// </summary>
  41. [Obsolete("JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.")]
  42. internal class JsonSchema
  43. {
  44. /// <summary>
  45. /// Gets or sets the id.
  46. /// </summary>
  47. public string Id { get; set; }
  48. /// <summary>
  49. /// Gets or sets the title.
  50. /// </summary>
  51. public string Title { get; set; }
  52. /// <summary>
  53. /// Gets or sets whether the object is required.
  54. /// </summary>
  55. public bool? Required { get; set; }
  56. /// <summary>
  57. /// Gets or sets whether the object is read-only.
  58. /// </summary>
  59. public bool? ReadOnly { get; set; }
  60. /// <summary>
  61. /// Gets or sets whether the object is visible to users.
  62. /// </summary>
  63. public bool? Hidden { get; set; }
  64. /// <summary>
  65. /// Gets or sets whether the object is transient.
  66. /// </summary>
  67. public bool? Transient { get; set; }
  68. /// <summary>
  69. /// Gets or sets the description of the object.
  70. /// </summary>
  71. public string Description { get; set; }
  72. /// <summary>
  73. /// Gets or sets the types of values allowed by the object.
  74. /// </summary>
  75. /// <value>The type.</value>
  76. public JsonSchemaType? Type { get; set; }
  77. /// <summary>
  78. /// Gets or sets the pattern.
  79. /// </summary>
  80. /// <value>The pattern.</value>
  81. public string Pattern { get; set; }
  82. /// <summary>
  83. /// Gets or sets the minimum length.
  84. /// </summary>
  85. /// <value>The minimum length.</value>
  86. public int? MinimumLength { get; set; }
  87. /// <summary>
  88. /// Gets or sets the maximum length.
  89. /// </summary>
  90. /// <value>The maximum length.</value>
  91. public int? MaximumLength { get; set; }
  92. /// <summary>
  93. /// Gets or sets a number that the value should be divisible by.
  94. /// </summary>
  95. /// <value>A number that the value should be divisible by.</value>
  96. public double? DivisibleBy { get; set; }
  97. /// <summary>
  98. /// Gets or sets the minimum.
  99. /// </summary>
  100. /// <value>The minimum.</value>
  101. public double? Minimum { get; set; }
  102. /// <summary>
  103. /// Gets or sets the maximum.
  104. /// </summary>
  105. /// <value>The maximum.</value>
  106. public double? Maximum { get; set; }
  107. /// <summary>
  108. /// Gets or sets a flag indicating whether the value can not equal the number defined by the <c>minimum</c> attribute (<see cref="JsonSchema.Minimum"/>).
  109. /// </summary>
  110. /// <value>A flag indicating whether the value can not equal the number defined by the <c>minimum</c> attribute (<see cref="JsonSchema.Minimum"/>).</value>
  111. public bool? ExclusiveMinimum { get; set; }
  112. /// <summary>
  113. /// Gets or sets a flag indicating whether the value can not equal the number defined by the <c>maximum</c> attribute (<see cref="JsonSchema.Maximum"/>).
  114. /// </summary>
  115. /// <value>A flag indicating whether the value can not equal the number defined by the <c>maximum</c> attribute (<see cref="JsonSchema.Maximum"/>).</value>
  116. public bool? ExclusiveMaximum { get; set; }
  117. /// <summary>
  118. /// Gets or sets the minimum number of items.
  119. /// </summary>
  120. /// <value>The minimum number of items.</value>
  121. public int? MinimumItems { get; set; }
  122. /// <summary>
  123. /// Gets or sets the maximum number of items.
  124. /// </summary>
  125. /// <value>The maximum number of items.</value>
  126. public int? MaximumItems { get; set; }
  127. /// <summary>
  128. /// Gets or sets the <see cref="JsonSchema"/> of items.
  129. /// </summary>
  130. /// <value>The <see cref="JsonSchema"/> of items.</value>
  131. public IList<JsonSchema> Items { get; set; }
  132. /// <summary>
  133. /// Gets or sets a value indicating whether items in an array are validated using the <see cref="JsonSchema"/> instance at their array position from <see cref="JsonSchema.Items"/>.
  134. /// </summary>
  135. /// <value>
  136. /// <c>true</c> if items are validated using their array position; otherwise, <c>false</c>.
  137. /// </value>
  138. public bool PositionalItemsValidation { get; set; }
  139. /// <summary>
  140. /// Gets or sets the <see cref="JsonSchema"/> of additional items.
  141. /// </summary>
  142. /// <value>The <see cref="JsonSchema"/> of additional items.</value>
  143. public JsonSchema AdditionalItems { get; set; }
  144. /// <summary>
  145. /// Gets or sets a value indicating whether additional items are allowed.
  146. /// </summary>
  147. /// <value>
  148. /// <c>true</c> if additional items are allowed; otherwise, <c>false</c>.
  149. /// </value>
  150. public bool AllowAdditionalItems { get; set; }
  151. /// <summary>
  152. /// Gets or sets whether the array items must be unique.
  153. /// </summary>
  154. public bool UniqueItems { get; set; }
  155. /// <summary>
  156. /// Gets or sets the <see cref="JsonSchema"/> of properties.
  157. /// </summary>
  158. /// <value>The <see cref="JsonSchema"/> of properties.</value>
  159. public IDictionary<string, JsonSchema> Properties { get; set; }
  160. /// <summary>
  161. /// Gets or sets the <see cref="JsonSchema"/> of additional properties.
  162. /// </summary>
  163. /// <value>The <see cref="JsonSchema"/> of additional properties.</value>
  164. public JsonSchema AdditionalProperties { get; set; }
  165. /// <summary>
  166. /// Gets or sets the pattern properties.
  167. /// </summary>
  168. /// <value>The pattern properties.</value>
  169. public IDictionary<string, JsonSchema> PatternProperties { get; set; }
  170. /// <summary>
  171. /// Gets or sets a value indicating whether additional properties are allowed.
  172. /// </summary>
  173. /// <value>
  174. /// <c>true</c> if additional properties are allowed; otherwise, <c>false</c>.
  175. /// </value>
  176. public bool AllowAdditionalProperties { get; set; }
  177. /// <summary>
  178. /// Gets or sets the required property if this property is present.
  179. /// </summary>
  180. /// <value>The required property if this property is present.</value>
  181. public string Requires { get; set; }
  182. /// <summary>
  183. /// Gets or sets the a collection of valid enum values allowed.
  184. /// </summary>
  185. /// <value>A collection of valid enum values allowed.</value>
  186. public IList<JToken> Enum { get; set; }
  187. /// <summary>
  188. /// Gets or sets disallowed types.
  189. /// </summary>
  190. /// <value>The disallowed types.</value>
  191. public JsonSchemaType? Disallow { get; set; }
  192. /// <summary>
  193. /// Gets or sets the default value.
  194. /// </summary>
  195. /// <value>The default value.</value>
  196. public JToken Default { get; set; }
  197. /// <summary>
  198. /// Gets or sets the collection of <see cref="JsonSchema"/> that this schema extends.
  199. /// </summary>
  200. /// <value>The collection of <see cref="JsonSchema"/> that this schema extends.</value>
  201. public IList<JsonSchema> Extends { get; set; }
  202. /// <summary>
  203. /// Gets or sets the format.
  204. /// </summary>
  205. /// <value>The format.</value>
  206. public string Format { get; set; }
  207. internal string Location { get; set; }
  208. private readonly string _internalId = Guid.NewGuid().ToString("N");
  209. internal string InternalId => _internalId;
  210. // if this is set then this schema instance is just a deferred reference
  211. // and will be replaced when the schema reference is resolved
  212. internal string DeferredReference { get; set; }
  213. internal bool ReferencesResolved { get; set; }
  214. /// <summary>
  215. /// Initializes a new instance of the <see cref="JsonSchema"/> class.
  216. /// </summary>
  217. public JsonSchema()
  218. {
  219. AllowAdditionalProperties = true;
  220. AllowAdditionalItems = true;
  221. }
  222. /// <summary>
  223. /// Reads a <see cref="JsonSchema"/> from the specified <see cref="JsonReader"/>.
  224. /// </summary>
  225. /// <param name="reader">The <see cref="JsonReader"/> containing the JSON Schema to read.</param>
  226. /// <returns>The <see cref="JsonSchema"/> object representing the JSON Schema.</returns>
  227. public static JsonSchema Read(JsonReader reader)
  228. {
  229. return Read(reader, new JsonSchemaResolver());
  230. }
  231. /// <summary>
  232. /// Reads a <see cref="JsonSchema"/> from the specified <see cref="JsonReader"/>.
  233. /// </summary>
  234. /// <param name="reader">The <see cref="JsonReader"/> containing the JSON Schema to read.</param>
  235. /// <param name="resolver">The <see cref="JsonSchemaResolver"/> to use when resolving schema references.</param>
  236. /// <returns>The <see cref="JsonSchema"/> object representing the JSON Schema.</returns>
  237. public static JsonSchema Read(JsonReader reader, JsonSchemaResolver resolver)
  238. {
  239. ValidationUtils.ArgumentNotNull(reader, nameof(reader));
  240. ValidationUtils.ArgumentNotNull(resolver, nameof(resolver));
  241. JsonSchemaBuilder builder = new JsonSchemaBuilder(resolver);
  242. return builder.Read(reader);
  243. }
  244. /// <summary>
  245. /// Load a <see cref="JsonSchema"/> from a string that contains JSON Schema.
  246. /// </summary>
  247. /// <param name="json">A <see cref="String"/> that contains JSON Schema.</param>
  248. /// <returns>A <see cref="JsonSchema"/> populated from the string that contains JSON Schema.</returns>
  249. public static JsonSchema Parse(string json)
  250. {
  251. return Parse(json, new JsonSchemaResolver());
  252. }
  253. /// <summary>
  254. /// Load a <see cref="JsonSchema"/> from a string that contains JSON Schema using the specified <see cref="JsonSchemaResolver"/>.
  255. /// </summary>
  256. /// <param name="json">A <see cref="String"/> that contains JSON Schema.</param>
  257. /// <param name="resolver">The resolver.</param>
  258. /// <returns>A <see cref="JsonSchema"/> populated from the string that contains JSON Schema.</returns>
  259. public static JsonSchema Parse(string json, JsonSchemaResolver resolver)
  260. {
  261. ValidationUtils.ArgumentNotNull(json, nameof(json));
  262. using (JsonReader reader = new JsonTextReader(new StringReader(json)))
  263. {
  264. return Read(reader, resolver);
  265. }
  266. }
  267. /// <summary>
  268. /// Writes this schema to a <see cref="JsonWriter"/>.
  269. /// </summary>
  270. /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
  271. public void WriteTo(JsonWriter writer)
  272. {
  273. WriteTo(writer, new JsonSchemaResolver());
  274. }
  275. /// <summary>
  276. /// Writes this schema to a <see cref="JsonWriter"/> using the specified <see cref="JsonSchemaResolver"/>.
  277. /// </summary>
  278. /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
  279. /// <param name="resolver">The resolver used.</param>
  280. public void WriteTo(JsonWriter writer, JsonSchemaResolver resolver)
  281. {
  282. ValidationUtils.ArgumentNotNull(writer, nameof(writer));
  283. ValidationUtils.ArgumentNotNull(resolver, nameof(resolver));
  284. JsonSchemaWriter schemaWriter = new JsonSchemaWriter(writer, resolver);
  285. schemaWriter.WriteSchema(this);
  286. }
  287. /// <summary>
  288. /// Returns a <see cref="String"/> that represents the current <see cref="Object"/>.
  289. /// </summary>
  290. /// <returns>
  291. /// A <see cref="String"/> that represents the current <see cref="Object"/>.
  292. /// </returns>
  293. public override string ToString()
  294. {
  295. StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
  296. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  297. jsonWriter.Formatting = Formatting.Indented;
  298. WriteTo(jsonWriter);
  299. return writer.ToString();
  300. }
  301. }
  302. }