#region License // Copyright (c) 2007 James Newton-King // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. #endregion using System; using System.Collections.Generic; using System.IO; using LC.Newtonsoft.Json.Linq; using LC.Newtonsoft.Json.Utilities; using System.Globalization; namespace LC.Newtonsoft.Json.Schema { /// /// /// An in-memory representation of a JSON Schema. /// /// /// JSON Schema validation has been moved to its own package. See https://www.newtonsoft.com/jsonschema for more details. /// /// [Obsolete("JSON Schema validation has been moved to its own package. See https://www.newtonsoft.com/jsonschema for more details.")] public class JsonSchema { /// /// Gets or sets the id. /// public string Id { get; set; } /// /// Gets or sets the title. /// public string Title { get; set; } /// /// Gets or sets whether the object is required. /// public bool? Required { get; set; } /// /// Gets or sets whether the object is read-only. /// public bool? ReadOnly { get; set; } /// /// Gets or sets whether the object is visible to users. /// public bool? Hidden { get; set; } /// /// Gets or sets whether the object is transient. /// public bool? Transient { get; set; } /// /// Gets or sets the description of the object. /// public string Description { get; set; } /// /// Gets or sets the types of values allowed by the object. /// /// The type. public JsonSchemaType? Type { get; set; } /// /// Gets or sets the pattern. /// /// The pattern. public string Pattern { get; set; } /// /// Gets or sets the minimum length. /// /// The minimum length. public int? MinimumLength { get; set; } /// /// Gets or sets the maximum length. /// /// The maximum length. public int? MaximumLength { get; set; } /// /// Gets or sets a number that the value should be divisible by. /// /// A number that the value should be divisible by. public double? DivisibleBy { get; set; } /// /// Gets or sets the minimum. /// /// The minimum. public double? Minimum { get; set; } /// /// Gets or sets the maximum. /// /// The maximum. public double? Maximum { get; set; } /// /// Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (). /// /// A flag indicating whether the value can not equal the number defined by the minimum attribute (). public bool? ExclusiveMinimum { get; set; } /// /// Gets or sets a flag indicating whether the value can not equal the number defined by the maximum attribute (). /// /// A flag indicating whether the value can not equal the number defined by the maximum attribute (). public bool? ExclusiveMaximum { get; set; } /// /// Gets or sets the minimum number of items. /// /// The minimum number of items. public int? MinimumItems { get; set; } /// /// Gets or sets the maximum number of items. /// /// The maximum number of items. public int? MaximumItems { get; set; } /// /// Gets or sets the of items. /// /// The of items. public IList Items { get; set; } /// /// Gets or sets a value indicating whether items in an array are validated using the instance at their array position from . /// /// /// true if items are validated using their array position; otherwise, false. /// public bool PositionalItemsValidation { get; set; } /// /// Gets or sets the of additional items. /// /// The of additional items. public JsonSchema AdditionalItems { get; set; } /// /// Gets or sets a value indicating whether additional items are allowed. /// /// /// true if additional items are allowed; otherwise, false. /// public bool AllowAdditionalItems { get; set; } /// /// Gets or sets whether the array items must be unique. /// public bool UniqueItems { get; set; } /// /// Gets or sets the of properties. /// /// The of properties. public IDictionary Properties { get; set; } /// /// Gets or sets the of additional properties. /// /// The of additional properties. public JsonSchema AdditionalProperties { get; set; } /// /// Gets or sets the pattern properties. /// /// The pattern properties. public IDictionary PatternProperties { get; set; } /// /// Gets or sets a value indicating whether additional properties are allowed. /// /// /// true if additional properties are allowed; otherwise, false. /// public bool AllowAdditionalProperties { get; set; } /// /// Gets or sets the required property if this property is present. /// /// The required property if this property is present. public string Requires { get; set; } /// /// Gets or sets the a collection of valid enum values allowed. /// /// A collection of valid enum values allowed. public IList Enum { get; set; } /// /// Gets or sets disallowed types. /// /// The disallowed types. public JsonSchemaType? Disallow { get; set; } /// /// Gets or sets the default value. /// /// The default value. public JToken Default { get; set; } /// /// Gets or sets the collection of that this schema extends. /// /// The collection of that this schema extends. public IList Extends { get; set; } /// /// Gets or sets the format. /// /// The format. public string Format { get; set; } internal string Location { get; set; } #pragma warning disable CA1305 // Specify IFormatProvider private readonly string _internalId = Guid.NewGuid().ToString("N"); #pragma warning restore CA1305 // Specify IFormatProvider internal string InternalId => _internalId; // if this is set then this schema instance is just a deferred reference // and will be replaced when the schema reference is resolved internal string DeferredReference { get; set; } internal bool ReferencesResolved { get; set; } /// /// Initializes a new instance of the class. /// public JsonSchema() { AllowAdditionalProperties = true; AllowAdditionalItems = true; } /// /// Reads a from the specified . /// /// The containing the JSON Schema to read. /// The object representing the JSON Schema. public static JsonSchema Read(JsonReader reader) { return Read(reader, new JsonSchemaResolver()); } /// /// Reads a from the specified . /// /// The containing the JSON Schema to read. /// The to use when resolving schema references. /// The object representing the JSON Schema. public static JsonSchema Read(JsonReader reader, JsonSchemaResolver resolver) { ValidationUtils.ArgumentNotNull(reader, nameof(reader)); ValidationUtils.ArgumentNotNull(resolver, nameof(resolver)); JsonSchemaBuilder builder = new JsonSchemaBuilder(resolver); return builder.Read(reader); } /// /// Load a from a string that contains JSON Schema. /// /// A that contains JSON Schema. /// A populated from the string that contains JSON Schema. public static JsonSchema Parse(string json) { return Parse(json, new JsonSchemaResolver()); } /// /// Load a from a string that contains JSON Schema using the specified . /// /// A that contains JSON Schema. /// The resolver. /// A populated from the string that contains JSON Schema. public static JsonSchema Parse(string json, JsonSchemaResolver resolver) { ValidationUtils.ArgumentNotNull(json, nameof(json)); using (JsonReader reader = new JsonTextReader(new StringReader(json))) { return Read(reader, resolver); } } /// /// Writes this schema to a . /// /// A into which this method will write. public void WriteTo(JsonWriter writer) { WriteTo(writer, new JsonSchemaResolver()); } /// /// Writes this schema to a using the specified . /// /// A into which this method will write. /// The resolver used. public void WriteTo(JsonWriter writer, JsonSchemaResolver resolver) { ValidationUtils.ArgumentNotNull(writer, nameof(writer)); ValidationUtils.ArgumentNotNull(resolver, nameof(resolver)); JsonSchemaWriter schemaWriter = new JsonSchemaWriter(writer, resolver); schemaWriter.WriteSchema(this); } /// /// Returns a that represents the current . /// /// /// A that represents the current . /// public override string ToString() { StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); JsonTextWriter jsonWriter = new JsonTextWriter(writer); jsonWriter.Formatting = Formatting.Indented; WriteTo(jsonWriter); return writer.ToString(); } } }