예제 #1
0
    private void ValidateCurrentToken()
    {
      // first time validate has been called. build model
      if (_model == null)
      {
        JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
        _model = builder.Build(_schema);

        if (!JsonWriter.IsStartToken(_reader.TokenType))
          Push(new SchemaScope(JTokenType.None, CurrentMemberSchemas));
      }

      switch (_reader.TokenType)
      {
        case JsonToken.StartObject:
          ProcessValue();
          IList<JsonSchemaModel> objectSchemas = CurrentMemberSchemas.Where(ValidateObject).ToList();
          Push(new SchemaScope(JTokenType.Object, objectSchemas));
          WriteToken(CurrentSchemas);
          break;
        case JsonToken.StartArray:
          ProcessValue();
          IList<JsonSchemaModel> arraySchemas = CurrentMemberSchemas.Where(ValidateArray).ToList();
          Push(new SchemaScope(JTokenType.Array, arraySchemas));
          WriteToken(CurrentSchemas);
          break;
        case JsonToken.StartConstructor:
          ProcessValue();
          Push(new SchemaScope(JTokenType.Constructor, null));
          WriteToken(CurrentSchemas);
          break;
        case JsonToken.PropertyName:
          WriteToken(CurrentSchemas);
          foreach (JsonSchemaModel schema in CurrentSchemas)
          {
            ValidatePropertyName(schema);
          }
          break;
        case JsonToken.Raw:
          ProcessValue();
          break;
        case JsonToken.Integer:
          ProcessValue();
          WriteToken(CurrentMemberSchemas);
          foreach (JsonSchemaModel schema in CurrentMemberSchemas)
          {
            ValidateInteger(schema);
          }
          break;
        case JsonToken.Float:
          ProcessValue();
          WriteToken(CurrentMemberSchemas);
          foreach (JsonSchemaModel schema in CurrentMemberSchemas)
          {
            ValidateFloat(schema);
          }
          break;
        case JsonToken.String:
          ProcessValue();
          WriteToken(CurrentMemberSchemas);
          foreach (JsonSchemaModel schema in CurrentMemberSchemas)
          {
            ValidateString(schema);
          }
          break;
        case JsonToken.Boolean:
          ProcessValue();
          WriteToken(CurrentMemberSchemas);
          foreach (JsonSchemaModel schema in CurrentMemberSchemas)
          {
            ValidateBoolean(schema);
          }
          break;
        case JsonToken.Null:
          ProcessValue();
          WriteToken(CurrentMemberSchemas);
          foreach (JsonSchemaModel schema in CurrentMemberSchemas)
          {
            ValidateNull(schema);
          }
          break;
        case JsonToken.EndObject:
          WriteToken(CurrentSchemas);
          foreach (JsonSchemaModel schema in CurrentSchemas)
          {
            ValidateEndObject(schema);
          }
          Pop();
          break;
        case JsonToken.EndArray:
          WriteToken(CurrentSchemas);
          foreach (JsonSchemaModel schema in CurrentSchemas)
          {
            ValidateEndArray(schema);
          }
          Pop();
          break;
        case JsonToken.EndConstructor:
          WriteToken(CurrentSchemas);
          Pop();
          break;
        case JsonToken.Undefined:
        case JsonToken.Date:
        case JsonToken.Bytes:
          // these have no equivalent in JSON schema
          WriteToken(CurrentMemberSchemas);
          break;
        case JsonToken.None:
          // no content, do nothing
          break;
        default:
          throw new ArgumentOutOfRangeException();
      }
    }
예제 #2
0
    public void Required()
    {
      string schemaJson = @"{
  ""description"":""A person"",
  ""type"":""object"",
  ""properties"":
  {
    ""name"":{""type"":""string""},
    ""hobbies"":{""type"":""string"",required:true},
    ""age"":{""type"":""integer"",required:true}
  }
}";

      JsonSchema schema = JsonSchema.Parse(schemaJson);
      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
      JsonSchemaModel model = modelBuilder.Build(schema);

      Assert.AreEqual(JsonSchemaType.Object, model.Type);
      Assert.AreEqual(3, model.Properties.Count);
      Assert.AreEqual(false, model.Properties["name"].Required);
      Assert.AreEqual(true, model.Properties["hobbies"].Required);
      Assert.AreEqual(true, model.Properties["age"].Required);
    }
예제 #3
0
    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);
    }
예제 #4
0
    public void CircularReference()
    {
      string json = @"{
  ""id"":""CircularReferenceArray"",
  ""description"":""CircularReference"",
  ""type"":[""array""],
  ""items"":{""$ref"":""CircularReferenceArray""}
}";

      JsonSchema schema = JsonSchema.Parse(json);

      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();

      JsonSchemaModel model = modelBuilder.Build(schema);

      Assert.AreEqual(JsonSchemaType.Array, model.Type);

      Assert.AreEqual(model, model.Items[0]);
    }