public void AddProperty(IDictionary<string, JsonSchemaNode> target, string propertyName, JsonSchema schema)
        {
            JsonSchemaNode propertyNode;
              target.TryGetValue(propertyName, out propertyNode);

              target[propertyName] = AddSchema(propertyNode, schema);
        }
Example #2
0
        public JsonSchemaNode(JsonSchema schema)
        {
            Schemas = new ReadOnlyCollection<JsonSchema>(new []{ schema });
              Properties = new Dictionary<string, JsonSchemaNode>();
              PatternProperties = new Dictionary<string, JsonSchemaNode>();
              Items = new List<JsonSchemaNode>();

              Id = GetId(Schemas);
        }
Example #3
0
        /// <summary>
        /// Determines whether the <see cref="JToken"/> is valid.
        /// </summary>
        /// <param name="source">The source <see cref="JToken"/> to test.</param>
        /// <param name="schema">The schema to test with.</param>
        /// <param name="errorMessages">When this method returns, contains any error messages generated while validating. </param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="JToken"/> is valid; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValid(this JToken source, JsonSchema schema, out IList<string> errorMessages)
        {
            IList<string> errors = new List<string>();

              source.Validate(schema, (sender, args) => errors.Add(args.Message));

              errorMessages = errors;
              return (errorMessages.Count == 0);
        }
Example #4
0
        private JsonSchemaNode(JsonSchemaNode source, JsonSchema schema)
        {
            Schemas = new ReadOnlyCollection<JsonSchema>(source.Schemas.Union(new[] { schema }).ToList());
              Properties = new Dictionary<string, JsonSchemaNode>(source.Properties);
              PatternProperties = new Dictionary<string, JsonSchemaNode>(source.PatternProperties);
              Items = new List<JsonSchemaNode>(source.Items);
              AdditionalProperties = source.AdditionalProperties;

              Id = GetId(Schemas);
        }
        public void AddItem(JsonSchemaNode parentNode, int index, JsonSchema schema)
        {
            JsonSchemaNode existingItemNode = (parentNode.Items.Count > index)
                                  ? parentNode.Items[index]
                                  : null;

              JsonSchemaNode newItemNode = AddSchema(existingItemNode, schema);

              if (!(parentNode.Items.Count > index))
              {
            parentNode.Items.Add(newItemNode);
              }
              else
              {
            parentNode.Items[index] = newItemNode;
              }
        }
Example #6
0
        private void WriteItems(JsonSchema schema)
        {
            if (CollectionUtils.IsNullOrEmpty(schema.Items))
            return;

              _writer.WritePropertyName(JsonSchemaConstants.ItemsPropertyName);

              if (schema.Items.Count == 1)
              {
            ReferenceOrWriteSchema(schema.Items[0]);
            return;
              }

              _writer.WriteStartArray();
              foreach (JsonSchema itemSchema in schema.Items)
              {
            ReferenceOrWriteSchema(itemSchema);
              }
              _writer.WriteEndArray();
        }
Example #7
0
 private void ReferenceOrWriteSchema(JsonSchema schema)
 {
     if (schema.Id != null && _resolver.GetSchema(schema.Id) != null)
       {
     _writer.WriteStartObject();
     _writer.WritePropertyName(JsonSchemaConstants.ReferencePropertyName);
     _writer.WriteValue(schema.Id);
     _writer.WriteEndObject();
       }
       else
       {
     WriteSchema(schema);
       }
 }
Example #8
0
        private static void Combine(JsonSchemaModel model, JsonSchema schema)
        {
            // Version 3 of the Draft JSON Schema has the default value of Not Required
              model.Required = model.Required || (schema.Required ?? false);
              model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any);

              model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
              model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength);

              // not sure what is the best way to combine divisibleBy
              model.DivisibleBy = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy);

              model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum);
              model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum);
              model.ExclusiveMinimum = model.ExclusiveMinimum || (schema.ExclusiveMinimum ?? false);
              model.ExclusiveMaximum = model.ExclusiveMaximum || (schema.ExclusiveMaximum ?? false);

              model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
              model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems);
              model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties;
              if (schema.Enum != null)
              {
            if (model.Enum == null)
              model.Enum = new List<JToken>();

            model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer());
              }
              model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None);

              if (schema.Pattern != null)
              {
            if (model.Patterns == null)
              model.Patterns = new List<string>();

            model.Patterns.AddDistinct(schema.Pattern);
              }
        }
Example #9
0
            public TypeSchema(Type type, JsonSchema schema)
            {
                ValidationUtils.ArgumentNotNull(type, "type");
                ValidationUtils.ArgumentNotNull(schema, "schema");

                Type = type;
                Schema = schema;
            }
Example #10
0
 /// <summary>
 /// Determines whether the <see cref="JToken"/> is valid.
 /// </summary>
 /// <param name="source">The source <see cref="JToken"/> to test.</param>
 /// <param name="schema">The schema to test with.</param>
 /// <returns>
 /// 	<c>true</c> if the specified <see cref="JToken"/> is valid; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsValid(this JToken source, JsonSchema schema)
 {
     bool valid = true;
       source.Validate(schema, (sender, args) => { valid = false; });
       return valid;
 }
Example #11
0
 private void Push(JsonSchema value)
 {
     _currentSchema = value;
       _stack.Add(value);
       _resolver.LoadedSchemas.Add(value);
 }
Example #12
0
 public void AddAdditionalProperties(JsonSchemaNode parentNode, JsonSchema schema)
 {
     parentNode.AdditionalProperties = AddSchema(parentNode.AdditionalProperties, schema);
 }
Example #13
0
        public JsonSchemaModel Build(JsonSchema schema)
        {
            _nodes = new JsonSchemaNodeCollection();
              _node = AddSchema(null, schema);

              _nodeModels = new Dictionary<JsonSchemaNode, JsonSchemaModel>();
              JsonSchemaModel model = BuildNodeModel(_node);

              return model;
        }
Example #14
0
        /// <summary>
        /// Validates the specified <see cref="JToken"/>.
        /// </summary>
        /// <param name="source">The source <see cref="JToken"/> to test.</param>
        /// <param name="schema">The schema to test with.</param>
        /// <param name="validationEventHandler">The validation event handler.</param>
        public static void Validate(this JToken source, JsonSchema schema, ValidationEventHandler validationEventHandler)
        {
            ValidationUtils.ArgumentNotNull(source, "source");
              ValidationUtils.ArgumentNotNull(schema, "schema");

              using (JsonValidatingReader reader = new JsonValidatingReader(source.CreateReader()))
              {
            reader.Schema = schema;
            if (validationEventHandler != null)
              reader.ValidationEventHandler += validationEventHandler;

            while (reader.Read())
            {
            }
              }
        }
Example #15
0
 /// <summary>
 /// Validates the specified <see cref="JToken"/>.
 /// </summary>
 /// <param name="source">The source <see cref="JToken"/> to test.</param>
 /// <param name="schema">The schema to test with.</param>
 public static void Validate(this JToken source, JsonSchema schema)
 {
     source.Validate(schema, null);
 }
Example #16
0
        public void WriteSchema(JsonSchema schema)
        {
            ValidationUtils.ArgumentNotNull(schema, "schema");

              if (!_resolver.LoadedSchemas.Contains(schema))
            _resolver.LoadedSchemas.Add(schema);

              _writer.WriteStartObject();
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.IdPropertyName, schema.Id);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.TitlePropertyName, schema.Title);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.DescriptionPropertyName, schema.Description);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.RequiredPropertyName, schema.Required);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.ReadOnlyPropertyName, schema.ReadOnly);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.HiddenPropertyName, schema.Hidden);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.TransientPropertyName, schema.Transient);
              if (schema.Type != null)
            WriteType(JsonSchemaConstants.TypePropertyName, _writer, schema.Type.Value);
              if (!schema.AllowAdditionalProperties)
              {
            _writer.WritePropertyName(JsonSchemaConstants.AdditionalPropertiesPropertyName);
            _writer.WriteValue(schema.AllowAdditionalProperties);
              }
              else
              {
            if (schema.AdditionalProperties != null)
            {
              _writer.WritePropertyName(JsonSchemaConstants.AdditionalPropertiesPropertyName);
              ReferenceOrWriteSchema(schema.AdditionalProperties);
            }
              }
              WriteSchemaDictionaryIfNotNull(_writer, JsonSchemaConstants.PropertiesPropertyName, schema.Properties);
              WriteSchemaDictionaryIfNotNull(_writer, JsonSchemaConstants.PatternPropertiesPropertyName, schema.PatternProperties);
              WriteItems(schema);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.MinimumPropertyName, schema.Minimum);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumPropertyName, schema.Maximum);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.ExclusiveMinimumPropertyName, schema.ExclusiveMinimum);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.ExclusiveMaximumPropertyName, schema.ExclusiveMaximum);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.MinimumLengthPropertyName, schema.MinimumLength);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumLengthPropertyName, schema.MaximumLength);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.MinimumItemsPropertyName, schema.MinimumItems);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumItemsPropertyName, schema.MaximumItems);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.DivisibleByPropertyName, schema.DivisibleBy);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.FormatPropertyName, schema.Format);
              WritePropertyIfNotNull(_writer, JsonSchemaConstants.PatternPropertyName, schema.Pattern);
              if (schema.Enum != null)
              {
            _writer.WritePropertyName(JsonSchemaConstants.EnumPropertyName);
            _writer.WriteStartArray();
            foreach (JToken token in schema.Enum)
            {
              token.WriteTo(_writer);
            }
            _writer.WriteEndArray();
              }
              if (schema.Default != null)
              {
            _writer.WritePropertyName(JsonSchemaConstants.DefaultPropertyName);
            schema.Default.WriteTo(_writer);
              }
              if (schema.Options != null)
              {
            _writer.WritePropertyName(JsonSchemaConstants.OptionsPropertyName);
            _writer.WriteStartArray();
            foreach (KeyValuePair<JToken, string> option in schema.Options)
            {
              _writer.WriteStartObject();
              _writer.WritePropertyName(JsonSchemaConstants.OptionValuePropertyName);
              option.Key.WriteTo(_writer);
              if (option.Value != null)
              {
            _writer.WritePropertyName(JsonSchemaConstants.OptionLabelPropertyName);
            _writer.WriteValue(option.Value);
              }
              _writer.WriteEndObject();
            }
            _writer.WriteEndArray();
              }
              if (schema.Disallow != null)
            WriteType(JsonSchemaConstants.DisallowPropertyName, _writer, schema.Disallow.Value);
              if (schema.Extends != null)
              {
            _writer.WritePropertyName(JsonSchemaConstants.ExtendsPropertyName);
            ReferenceOrWriteSchema(schema.Extends);
              }
              _writer.WriteEndObject();
        }
Example #17
0
        private JsonSchema Pop()
        {
            JsonSchema poppedSchema = _currentSchema;
              _stack.RemoveAt(_stack.Count - 1);
              _currentSchema = _stack.LastOrDefault();

              return poppedSchema;
        }
Example #18
0
        public JsonSchemaNode AddSchema(JsonSchemaNode existingNode, JsonSchema schema)
        {
            string newId;
              if (existingNode != null)
              {
            if (existingNode.Schemas.Contains(schema))
              return existingNode;

            newId = JsonSchemaNode.GetId(existingNode.Schemas.Union(new[] { schema }));
              }
              else
              {
            newId = JsonSchemaNode.GetId(new[] { schema });
              }

              if (_nodes.Contains(newId))
            return _nodes[newId];

              JsonSchemaNode currentNode = (existingNode != null)
            ? existingNode.Combine(schema)
            : new JsonSchemaNode(schema);

              _nodes.Add(currentNode);

              AddProperties(schema.Properties, currentNode.Properties);

              AddProperties(schema.PatternProperties, currentNode.PatternProperties);

              if (schema.Items != null)
              {
            for (int i = 0; i < schema.Items.Count; i++)
            {
              AddItem(currentNode, i, schema.Items[i]);
            }
              }

              if (schema.AdditionalProperties != null)
            AddAdditionalProperties(currentNode, schema.AdditionalProperties);

              if (schema.Extends != null)
            currentNode = AddSchema(currentNode, schema.Extends);

              return currentNode;
        }
Example #19
0
 public JsonSchemaNode Combine(JsonSchema schema)
 {
     return new JsonSchemaNode(this, schema);
 }
Example #20
0
        private TypeSchema Pop()
        {
            TypeSchema popped = _stack[_stack.Count - 1];
              _stack.RemoveAt(_stack.Count - 1);
              TypeSchema newValue = _stack.LastOrDefault();
              if (newValue != null)
              {
            _currentSchema = newValue.Schema;
              }
              else
              {
            _currentSchema = null;
              }

              return popped;
        }
Example #21
0
 private void Push(TypeSchema typeSchema)
 {
     _currentSchema = typeSchema.Schema;
       _stack.Add(typeSchema);
       _resolver.LoadedSchemas.Add(typeSchema.Schema);
 }
Example #22
0
        public void WriteSchema(JsonSchema schema)
        {
            ValidationUtils.ArgumentNotNull(schema, "schema");

            if (!this._resolver.LoadedSchemas.Contains(schema))
            {
                this._resolver.LoadedSchemas.Add(schema);
            }

            this._writer.WriteStartObject();
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.IdPropertyName, schema.Id);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.TitlePropertyName, schema.Title);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.DescriptionPropertyName, schema.Description);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.RequiredPropertyName, schema.Required);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.ReadOnlyPropertyName, schema.ReadOnly);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.HiddenPropertyName, schema.Hidden);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.TransientPropertyName, schema.Transient);
            if (schema.Type != null)
            {
                this.WriteType(JsonSchemaConstants.TypePropertyName, _writer, schema.Type.Value);
            }
            if (!schema.AllowAdditionalProperties)
            {
                this._writer.WritePropertyName(JsonSchemaConstants.AdditionalPropertiesPropertyName);
                this._writer.WriteValue(schema.AllowAdditionalProperties);
            }
            else
            {
                if (schema.AdditionalProperties != null)
                {
                    this._writer.WritePropertyName(JsonSchemaConstants.AdditionalPropertiesPropertyName);
                    this.ReferenceOrWriteSchema(schema.AdditionalProperties);
                }
            }
            this.WriteSchemaDictionaryIfNotNull(_writer, JsonSchemaConstants.PropertiesPropertyName, schema.Properties);
            this.WriteSchemaDictionaryIfNotNull(_writer, JsonSchemaConstants.PatternPropertiesPropertyName, schema.PatternProperties);
            this.WriteItems(schema);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.MinimumPropertyName, schema.Minimum);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumPropertyName, schema.Maximum);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.ExclusiveMinimumPropertyName, schema.ExclusiveMinimum);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.ExclusiveMaximumPropertyName, schema.ExclusiveMaximum);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.MinimumLengthPropertyName, schema.MinimumLength);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumLengthPropertyName, schema.MaximumLength);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.MinimumItemsPropertyName, schema.MinimumItems);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumItemsPropertyName, schema.MaximumItems);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.DivisibleByPropertyName, schema.DivisibleBy);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.FormatPropertyName, schema.Format);
            this.WritePropertyIfNotNull(_writer, JsonSchemaConstants.PatternPropertyName, schema.Pattern);
            if (schema.Enum != null)
            {
                this._writer.WritePropertyName(JsonSchemaConstants.EnumPropertyName);
                this._writer.WriteStartArray();
                foreach (JToken token in schema.Enum)
                {
                    token.WriteTo(this._writer);
                }
                this._writer.WriteEndArray();
            }
            if (schema.Default != null)
            {
                this._writer.WritePropertyName(JsonSchemaConstants.DefaultPropertyName);
                schema.Default.WriteTo(this._writer);
            }
            if (schema.Options != null)
            {
                this._writer.WritePropertyName(JsonSchemaConstants.OptionsPropertyName);
                this._writer.WriteStartArray();
                foreach (KeyValuePair <JToken, string> option in schema.Options)
                {
                    this._writer.WriteStartObject();
                    this._writer.WritePropertyName(JsonSchemaConstants.OptionValuePropertyName);
                    option.Key.WriteTo(this._writer);
                    if (option.Value != null)
                    {
                        this._writer.WritePropertyName(JsonSchemaConstants.OptionLabelPropertyName);
                        this._writer.WriteValue(option.Value);
                    }
                    this._writer.WriteEndObject();
                }
                this._writer.WriteEndArray();
            }
            if (schema.Disallow != null)
            {
                this.WriteType(JsonSchemaConstants.DisallowPropertyName, _writer, schema.Disallow.Value);
            }
            if (schema.Extends != null)
            {
                this._writer.WritePropertyName(JsonSchemaConstants.ExtendsPropertyName);
                this.ReferenceOrWriteSchema(schema.Extends);
            }
            this._writer.WriteEndObject();
        }