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.

136 lines
6.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. /// <summary>
  32. /// <para>
  33. /// Contains the JSON schema extension methods.
  34. /// </para>
  35. /// <note type="caution">
  36. /// 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.
  37. /// </note>
  38. /// </summary>
  39. [Obsolete("JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.")]
  40. internal static class Extensions
  41. {
  42. /// <summary>
  43. /// <para>
  44. /// Determines whether the <see cref="JToken"/> is valid.
  45. /// </para>
  46. /// <note type="caution">
  47. /// 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.
  48. /// </note>
  49. /// </summary>
  50. /// <param name="source">The source <see cref="JToken"/> to test.</param>
  51. /// <param name="schema">The schema to test with.</param>
  52. /// <returns>
  53. /// <c>true</c> if the specified <see cref="JToken"/> is valid; otherwise, <c>false</c>.
  54. /// </returns>
  55. [Obsolete("JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.")]
  56. public static bool IsValid(this JToken source, JsonSchema schema)
  57. {
  58. bool valid = true;
  59. source.Validate(schema, (sender, args) => { valid = false; });
  60. return valid;
  61. }
  62. /// <summary>
  63. /// <para>
  64. /// Determines whether the <see cref="JToken"/> is valid.
  65. /// </para>
  66. /// <note type="caution">
  67. /// 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.
  68. /// </note>
  69. /// </summary>
  70. /// <param name="source">The source <see cref="JToken"/> to test.</param>
  71. /// <param name="schema">The schema to test with.</param>
  72. /// <param name="errorMessages">When this method returns, contains any error messages generated while validating. </param>
  73. /// <returns>
  74. /// <c>true</c> if the specified <see cref="JToken"/> is valid; otherwise, <c>false</c>.
  75. /// </returns>
  76. [Obsolete("JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.")]
  77. public static bool IsValid(this JToken source, JsonSchema schema, out IList<string> errorMessages)
  78. {
  79. IList<string> errors = new List<string>();
  80. source.Validate(schema, (sender, args) => errors.Add(args.Message));
  81. errorMessages = errors;
  82. return (errorMessages.Count == 0);
  83. }
  84. /// <summary>
  85. /// <para>
  86. /// Validates the specified <see cref="JToken"/>.
  87. /// </para>
  88. /// <note type="caution">
  89. /// 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.
  90. /// </note>
  91. /// </summary>
  92. /// <param name="source">The source <see cref="JToken"/> to test.</param>
  93. /// <param name="schema">The schema to test with.</param>
  94. [Obsolete("JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.")]
  95. public static void Validate(this JToken source, JsonSchema schema)
  96. {
  97. source.Validate(schema, null);
  98. }
  99. /// <summary>
  100. /// <para>
  101. /// Validates the specified <see cref="JToken"/>.
  102. /// </para>
  103. /// <note type="caution">
  104. /// 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.
  105. /// </note>
  106. /// </summary>
  107. /// <param name="source">The source <see cref="JToken"/> to test.</param>
  108. /// <param name="schema">The schema to test with.</param>
  109. /// <param name="validationEventHandler">The validation event handler.</param>
  110. [Obsolete("JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.")]
  111. public static void Validate(this JToken source, JsonSchema schema, ValidationEventHandler validationEventHandler)
  112. {
  113. ValidationUtils.ArgumentNotNull(source, nameof(source));
  114. ValidationUtils.ArgumentNotNull(schema, nameof(schema));
  115. using (JsonValidatingReader reader = new JsonValidatingReader(source.CreateReader()))
  116. {
  117. reader.Schema = schema;
  118. if (validationEventHandler != null)
  119. {
  120. reader.ValidationEventHandler += validationEventHandler;
  121. }
  122. while (reader.Read())
  123. {
  124. }
  125. }
  126. }
  127. }
  128. }