Esempio n. 1
0
    public void AddSchema()
    {
      string first = @"{
  ""id"":""first"",
  ""type"":""object"",
  ""properties"":
  {
    ""firstproperty"":{""type"":""string"",""maxLength"":10},
    ""secondproperty"":{
      ""type"":""object"",
      ""properties"":
      {
        ""secondproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
      }
    }
  },
  ""additionalProperties"":{}
}";

      string second = @"{
  ""id"":""second"",
  ""type"":""object"",
  ""extends"":{""$ref"":""first""},
  ""properties"":
  {
    ""firstproperty"":{""type"":""string""},
    ""secondproperty"":{
      ""extends"":{
        ""properties"":
        {
          ""secondproperty_firstproperty"":{""maxLength"":9,""minLength"":6}
        }
      },
      ""type"":""object"",
      ""properties"":
      {
        ""secondproperty_firstproperty"":{}
      }
    },
    ""thirdproperty"":{""type"":""string""}
  },
  ""additionalProperties"":false
}";

      JsonSchemaResolver resolver = new JsonSchemaResolver();
      JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
      JsonSchema secondSchema = JsonSchema.Parse(second, resolver);

      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();

      JsonSchemaNode node = modelBuilder.AddSchema(null, secondSchema);

      Assert.AreEqual(2, node.Schemas.Count);
      Assert.AreEqual(2, node.Properties["firstproperty"].Schemas.Count);
      Assert.AreEqual(3, node.Properties["secondproperty"].Schemas.Count);
      Assert.AreEqual(3, node.Properties["secondproperty"].Properties["secondproperty_firstproperty"].Schemas.Count);
    }
Esempio n. 2
0
    /// <summary>
    /// Writes this schema to a <see cref="JsonWriter"/> using the specified <see cref="JsonSchemaResolver"/>.
    /// </summary>
    /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
    /// <param name="resolver">The resolver used.</param>
    public void WriteTo(JsonWriter writer, JsonSchemaResolver resolver)
    {
      ValidationUtils.ArgumentNotNull(writer, "writer");
      ValidationUtils.ArgumentNotNull(resolver, "resolver");

      JsonSchemaWriter schemaWriter = new JsonSchemaWriter(writer, resolver);
      schemaWriter.WriteSchema(this);
    }
Esempio n. 3
0
    /// <summary>
    /// Parses the specified json.
    /// </summary>
    /// <param name="json">The json.</param>
    /// <param name="resolver">The resolver.</param>
    /// <returns>A <see cref="JsonSchema"/> populated from the string that contains JSON.</returns>
    public static JsonSchema Parse(string json, JsonSchemaResolver resolver)
    {
      ValidationUtils.ArgumentNotNull(json, "json");

      JsonReader reader = new JsonTextReader(new StringReader(json));

      return Read(reader, resolver);
    }
Esempio n. 4
0
    /// <summary>
    /// Reads a <see cref="JsonSchema"/> from the specified <see cref="JsonReader"/>.
    /// </summary>
    /// <param name="reader">The <see cref="JsonReader"/> containing the JSON Schema to read.</param>
    /// <param name="resolver">The <see cref="JsonSchemaResolver"/> to use when resolving schema references.</param>
    /// <returns>The <see cref="JsonSchema"/> object representing the JSON Schema.</returns>
    public static JsonSchema Read(JsonReader reader, JsonSchemaResolver resolver)
    {
      ValidationUtils.ArgumentNotNull(reader, "reader");
      ValidationUtils.ArgumentNotNull(resolver, "resolver");

      JsonSchemaBuilder builder = new JsonSchemaBuilder(resolver);
      return builder.Parse(reader);
    }
Esempio n. 5
0
    /// <summary>
    /// Generate a <see cref="JsonSchema"/> from the specified type.
    /// </summary>
    /// <param name="type">The type to generate a <see cref="JsonSchema"/> from.</param>
    /// <param name="resolver">The <see cref="JsonSchemaResolver"/> used to resolve schema references.</param>
    /// <param name="rootSchemaNullable">Specify whether the generated root <see cref="JsonSchema"/> will be nullable.</param>
    /// <returns>A <see cref="JsonSchema"/> generated from the specified type.</returns>
    public JsonSchema Generate(Type type, JsonSchemaResolver resolver, bool rootSchemaNullable)
    {
      ValidationUtils.ArgumentNotNull(type, "type");
      ValidationUtils.ArgumentNotNull(resolver, "resolver");

      _resolver = resolver;

      return GenerateInternal(type, (!rootSchemaNullable) ? Required.Always : Required.Default, false);
    }
    private JsonSchema GetExtendedSchema()
    {
      string first = @"{
  ""id"":""first"",
  ""type"":""object"",
  ""properties"":
  {
    ""firstproperty"":{""type"":""string"",""required"":true}
  },
  ""additionalProperties"":{}
}";

      string second = @"{
  ""id"":""second"",
  ""type"":""object"",
  ""extends"":{""$ref"":""first""},
  ""properties"":
  {
    ""secondproperty"":{""type"":""string"",""required"":true}
  },
  ""additionalProperties"":false
}";

      JsonSchemaResolver resolver = new JsonSchemaResolver();
      JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
      JsonSchema secondSchema = JsonSchema.Parse(second, resolver);

      return secondSchema;
    }
Esempio n. 7
0
 /// <summary>
 /// Generate a <see cref="JsonSchema"/> from the specified type.
 /// </summary>
 /// <param name="type">The type to generate a <see cref="JsonSchema"/> from.</param>
 /// <param name="resolver">The <see cref="JsonSchemaResolver"/> used to resolve schema references.</param>
 /// <returns>A <see cref="JsonSchema"/> generated from the specified type.</returns>
 public JsonSchema Generate(Type type, JsonSchemaResolver resolver)
 {
   return Generate(type, resolver, false);
 }
    public void ExtendedComplex()
    {
      string first = @"{
  ""id"":""first"",
  ""type"":""object"",
  ""properties"":
  {
    ""firstproperty"":{""type"":""string""},
    ""secondproperty"":{""type"":""string"",""maxLength"":10},
    ""thirdproperty"":{
      ""type"":""object"",
      ""properties"":
      {
        ""thirdproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
      }
    }
  },
  ""additionalProperties"":{}
}";

      string second = @"{
  ""id"":""second"",
  ""type"":""object"",
  ""extends"":{""$ref"":""first""},
  ""properties"":
  {
    ""secondproperty"":{""type"":""any""},
    ""thirdproperty"":{
      ""extends"":{
        ""properties"":
        {
          ""thirdproperty_firstproperty"":{""maxLength"":9,""minLength"":6,""pattern"":""hi2u""}
        },
        ""additionalProperties"":{""maxLength"":9,""minLength"":6,""enum"":[""one"",""two""]}
      },
      ""type"":""object"",
      ""properties"":
      {
        ""thirdproperty_firstproperty"":{""pattern"":""hi""}
      },
      ""additionalProperties"":{""type"":""string"",""enum"":[""two"",""three""]}
    },
    ""fourthproperty"":{""type"":""string""}
  },
  ""additionalProperties"":false
}";

      JsonSchemaResolver resolver = new JsonSchemaResolver();
      JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
      JsonSchema secondSchema = JsonSchema.Parse(second, resolver);

      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();

      JsonSchemaModel model = modelBuilder.Build(secondSchema);

      Assert.AreEqual(4, model.Properties.Count);

      Assert.AreEqual(JsonSchemaType.String, model.Properties["firstproperty"].Type);

      Assert.AreEqual(JsonSchemaType.String, model.Properties["secondproperty"].Type);
      Assert.AreEqual(10, model.Properties["secondproperty"].MaximumLength);
      Assert.AreEqual(null, model.Properties["secondproperty"].Enum);
      Assert.AreEqual(null, model.Properties["secondproperty"].Patterns);

      Assert.AreEqual(JsonSchemaType.Object, model.Properties["thirdproperty"].Type);
      Assert.AreEqual(3, model.Properties["thirdproperty"].AdditionalProperties.Enum.Count);
      Assert.AreEqual("two", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[0]);
      Assert.AreEqual("three", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[1]);
      Assert.AreEqual("one", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[2]);

      Assert.AreEqual(JsonSchemaType.String, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Type);
      Assert.AreEqual(9, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MaximumLength);
      Assert.AreEqual(7, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MinimumLength);
      Assert.AreEqual(2, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns.Count);
      Assert.AreEqual("hi", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[0]);
      Assert.AreEqual("hi2u", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[1]);
      Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Properties);
      Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Items);
      Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].AdditionalProperties);
    }
    public void ExtendedComplex()
    {
      string first = @"{
  ""id"":""first"",
  ""type"":""object"",
  ""properties"":
  {
    ""firstproperty"":{""type"":""string""},
    ""secondproperty"":{""type"":""string"",""maxLength"":10},
    ""thirdproperty"":{
      ""type"":""object"",
      ""properties"":
      {
        ""thirdproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
      }
    }
  },
  ""additionalProperties"":{}
}";

      string second = @"{
  ""id"":""second"",
  ""type"":""object"",
  ""extends"":{""$ref"":""first""},
  ""properties"":
  {
    ""secondproperty"":{""type"":""any""},
    ""thirdproperty"":{
      ""extends"":{
        ""properties"":
        {
          ""thirdproperty_firstproperty"":{""maxLength"":9,""minLength"":6,""pattern"":""hi2u""}
        },
        ""additionalProperties"":{""maxLength"":9,""minLength"":6,""enum"":[""one"",""two""]}
      },
      ""type"":""object"",
      ""properties"":
      {
        ""thirdproperty_firstproperty"":{""pattern"":""hi""}
      },
      ""additionalProperties"":{""type"":""string"",""enum"":[""two"",""three""]}
    },
    ""fourthproperty"":{""type"":""string""}
  },
  ""additionalProperties"":false
}";

      JsonSchemaResolver resolver = new JsonSchemaResolver();
      JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
      JsonSchema secondSchema = JsonSchema.Parse(second, resolver);

      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();

      string json = @"{
  'firstproperty':'blahblahblahblahblahblah',
  'secondproperty':'secasecasecasecaseca',
  'thirdproperty':{
    'thirdproperty_firstproperty':'aaa',
    'additional':'three'
  }
}";

      Json.Schema.ValidationEventArgs validationEventArgs = null;
      List<string> errors = new List<string>();

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) =>
        {
          validationEventArgs = args;
          errors.Add(validationEventArgs.Path + " - " + validationEventArgs.Message);
        };
      reader.Schema = secondSchema;

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("firstproperty", reader.Value.ToString());
      Assert.AreEqual(null, validationEventArgs);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("blahblahblahblahblahblah", reader.Value.ToString());

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("secondproperty", reader.Value.ToString());

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("secasecasecasecaseca", reader.Value.ToString());
      Assert.AreEqual(1, errors.Count);
      Assert.AreEqual("secondproperty - String 'secasecasecasecaseca' exceeds maximum length of 10. Line 3, position 42.", errors[0]);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("thirdproperty", reader.Value.ToString());
      Assert.AreEqual(1, errors.Count);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
      Assert.AreEqual(1, errors.Count);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("thirdproperty_firstproperty", reader.Value.ToString());
      Assert.AreEqual(1, errors.Count);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("aaa", reader.Value.ToString());
      Assert.AreEqual(4, errors.Count);
      Assert.AreEqual("thirdproperty.thirdproperty_firstproperty - String 'aaa' is less than minimum length of 7. Line 5, position 40.", errors[1]);
      Assert.AreEqual("thirdproperty.thirdproperty_firstproperty - String 'aaa' does not match regex pattern 'hi'. Line 5, position 40.", errors[2]);
      Assert.AreEqual("thirdproperty.thirdproperty_firstproperty - String 'aaa' does not match regex pattern 'hi2u'. Line 5, position 40.", errors[3]);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("additional", reader.Value.ToString());
      Assert.AreEqual(4, errors.Count);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("three", reader.Value.ToString());
      Assert.AreEqual(5, errors.Count);
      Assert.AreEqual("thirdproperty.additional - String 'three' is less than minimum length of 6. Line 6, position 25.", errors[4]);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

      Assert.IsFalse(reader.Read());
    }
Esempio n. 10
0
 /// <summary>
 /// Generate a <see cref="JsonSchema"/> from the specified type.
 /// </summary>
 /// <param name="type">The type to generate a <see cref="JsonSchema"/> from.</param>
 /// <param name="resolver">The <see cref="JsonSchemaResolver"/> used to resolve schema references.</param>
 /// <returns>A <see cref="JsonSchema"/> generated from the specified type.</returns>
 public JsonSchema Generate(Type type, JsonSchemaResolver resolver)
 {
     return(Generate(type, resolver, false));
 }
Esempio n. 11
0
 public JsonSchemaBuilder(JsonSchemaResolver resolver)
 {
     _stack    = new List <JsonSchema>();
     _resolver = resolver;
 }
Esempio n. 12
0
    public void Extends()
    {
      string json;
      JsonSchemaResolver resolver = new JsonSchemaResolver();

      json = @"{
  ""id"":""first"",
  ""type"":""object"",
  ""additionalProperties"":{}
}";

      JsonSchema first = JsonSchema.Parse(json, resolver);

      json =
        @"{
  ""id"":""second"",
  ""type"":""object"",
  ""extends"":{""$ref"":""first""},
  ""additionalProperties"":{""type"":""string""}
}";

      JsonSchema second = JsonSchema.Parse(json, resolver);
      Assert.AreEqual(first, second.Extends);

      json =
        @"{
  ""id"":""third"",
  ""type"":""object"",
  ""extends"":{""$ref"":""second""},
  ""additionalProperties"":false
}";

      JsonSchema third = JsonSchema.Parse(json, resolver);
      Assert.AreEqual(second, third.Extends);
      Assert.AreEqual(first, third.Extends.Extends);

      StringWriter writer = new StringWriter();
      JsonTextWriter jsonWriter = new JsonTextWriter(writer);
      jsonWriter.Formatting = Formatting.Indented;

      third.WriteTo(jsonWriter, resolver);

      string writtenJson = writer.ToString();
      Assert.AreEqual(@"{
  ""id"": ""third"",
  ""type"": ""object"",
  ""additionalProperties"": false,
  ""extends"": {
    ""$ref"": ""second""
  }
}", writtenJson);

      StringWriter writer1 = new StringWriter();
      JsonTextWriter jsonWriter1 = new JsonTextWriter(writer1);
      jsonWriter1.Formatting = Formatting.Indented;

      third.WriteTo(jsonWriter1);

      writtenJson = writer1.ToString();
      Assert.AreEqual(@"{
  ""id"": ""third"",
  ""type"": ""object"",
  ""additionalProperties"": false,
  ""extends"": {
    ""id"": ""second"",
    ""type"": ""object"",
    ""additionalProperties"": {
      ""type"": ""string""
    },
    ""extends"": {
      ""id"": ""first"",
      ""type"": ""object"",
      ""additionalProperties"": {}
    }
  }
}", writtenJson);
    }
Esempio n. 13
0
 public JsonSchemaBuilder(JsonSchemaResolver resolver)
 {
   _stack = new List<JsonSchema>();
   _documentSchemas = new Dictionary<string, JsonSchema>();
   _resolver = resolver;
 }
Esempio n. 14
0
 public JsonSchemaWriter(JsonWriter writer, JsonSchemaResolver resolver)
 {
     ValidationUtils.ArgumentNotNull(writer, "writer");
     _writer   = writer;
     _resolver = resolver;
 }
Esempio n. 15
0
 public JsonSchemaBuilder(JsonSchemaResolver resolver)
 {
   _stack = new List<JsonSchema>();
   _resolver = resolver;
 }
Esempio n. 16
0
 public JsonSchemaWriter(JsonWriter writer, JsonSchemaResolver resolver)
 {
     ValidationUtils.ArgumentNotNull(writer, "writer");
     _writer = writer;
     _resolver = resolver;
 }