Exemplo 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);
        }
        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]);
        }
Exemplo n.º 3
0
        public void CircularReference()
        {
            string json = @"{
  ""id"":""CircularReferenceArray"",
  ""description"":""CircularReference"",
  ""type"":[""array""],
  ""items"":{""$ref"":""CircularReferenceArray""}
}";

            JsonSchema schema = JsonSchema.Parse(json);

            JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();

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

            Assert.AreEqual(1, node.Schemas.Count);

            Assert.AreEqual(node, node.Items[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);
        }
        private void ValidateCurrentToken()
        {
            // first time validate has been called. build model
            if (_model == null)
            {
                JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
                _model = builder.Build(_schema);

                if (!JsonTokenUtils.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();
            }
        }
        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);
        }
        private void ValidateCurrentToken()
        {
            // first time validate has been called. build model
            if (_model == null)
            {
                JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
                _model = builder.Build(_schema);
            }

            //ValidateValueToken();

            switch (_reader.TokenType)
            {
            case JsonToken.StartObject:
                ProcessValue();
                IList <JsonSchemaModel> objectSchemas = CurrentMemberSchemas.Where(ValidateObject).ToList();
                Push(new SchemaScope(JTokenType.Object, objectSchemas));
                break;

            case JsonToken.StartArray:
                ProcessValue();
                IList <JsonSchemaModel> arraySchemas = CurrentMemberSchemas.Where(ValidateArray).ToList();
                Push(new SchemaScope(JTokenType.Array, arraySchemas));
                break;

            case JsonToken.StartConstructor:
                Push(new SchemaScope(JTokenType.Constructor, null));
                break;

            case JsonToken.PropertyName:
                foreach (JsonSchemaModel schema in CurrentSchemas)
                {
                    ValidatePropertyName(schema);
                }
                break;

            case JsonToken.Raw:
                break;

            case JsonToken.Integer:
                ProcessValue();
                foreach (JsonSchemaModel schema in CurrentMemberSchemas)
                {
                    ValidateInteger(schema);
                }
                break;

            case JsonToken.Float:
                ProcessValue();
                foreach (JsonSchemaModel schema in CurrentMemberSchemas)
                {
                    ValidateFloat(schema);
                }
                break;

            case JsonToken.String:
                ProcessValue();
                foreach (JsonSchemaModel schema in CurrentMemberSchemas)
                {
                    ValidateString(schema);
                }
                break;

            case JsonToken.Boolean:
                ProcessValue();
                foreach (JsonSchemaModel schema in CurrentMemberSchemas)
                {
                    ValidateBoolean(schema);
                }
                break;

            case JsonToken.Null:
                ProcessValue();
                foreach (JsonSchemaModel schema in CurrentMemberSchemas)
                {
                    ValidateNull(schema);
                }
                break;

            case JsonToken.Undefined:
                break;

            case JsonToken.EndObject:
                foreach (JsonSchemaModel schema in CurrentSchemas)
                {
                    ValidateEndObject(schema);
                }
                Pop();
                break;

            case JsonToken.EndArray:
                foreach (JsonSchemaModel schema in CurrentSchemas)
                {
                    ValidateEndArray(schema);
                }
                Pop();
                break;

            case JsonToken.EndConstructor:
                Pop();
                break;

            case JsonToken.Date:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 8
0
        private void ValidateCurrentToken()
        {
            if (this._model == null)
            {
                JsonSchemaModelBuilder jsonSchemaModelBuilder = new JsonSchemaModelBuilder();
                this._model = jsonSchemaModelBuilder.Build(this._schema);
            }
            switch (this._reader.TokenType)
            {
            case JsonToken.StartObject:
            {
                this.ProcessValue();
                IList <JsonSchemaModel> schemas = Enumerable.ToList <JsonSchemaModel>(Enumerable.Where <JsonSchemaModel>(this.CurrentMemberSchemas, new Func <JsonSchemaModel, bool>(this.ValidateObject)));
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Object, schemas));
                return;
            }

            case JsonToken.StartArray:
            {
                this.ProcessValue();
                IList <JsonSchemaModel> schemas2 = Enumerable.ToList <JsonSchemaModel>(Enumerable.Where <JsonSchemaModel>(this.CurrentMemberSchemas, new Func <JsonSchemaModel, bool>(this.ValidateArray)));
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Array, schemas2));
                return;
            }

            case JsonToken.StartConstructor:
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Constructor, null));
                return;

            case JsonToken.PropertyName:
                using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentSchemas.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        JsonSchemaModel current = enumerator.get_Current();
                        this.ValidatePropertyName(current);
                    }
                }
                return;

            case JsonToken.Raw:
                return;

            case JsonToken.Integer:
                this.ProcessValue();
                using (IEnumerator <JsonSchemaModel> enumerator2 = this.CurrentMemberSchemas.GetEnumerator())
                {
                    while (enumerator2.MoveNext())
                    {
                        JsonSchemaModel current2 = enumerator2.get_Current();
                        this.ValidateInteger(current2);
                    }
                }
                return;

            case JsonToken.Float:
                this.ProcessValue();
                using (IEnumerator <JsonSchemaModel> enumerator3 = this.CurrentMemberSchemas.GetEnumerator())
                {
                    while (enumerator3.MoveNext())
                    {
                        JsonSchemaModel current3 = enumerator3.get_Current();
                        this.ValidateFloat(current3);
                    }
                }
                return;

            case JsonToken.String:
                this.ProcessValue();
                using (IEnumerator <JsonSchemaModel> enumerator4 = this.CurrentMemberSchemas.GetEnumerator())
                {
                    while (enumerator4.MoveNext())
                    {
                        JsonSchemaModel current4 = enumerator4.get_Current();
                        this.ValidateString(current4);
                    }
                }
                return;

            case JsonToken.Boolean:
                this.ProcessValue();
                using (IEnumerator <JsonSchemaModel> enumerator5 = this.CurrentMemberSchemas.GetEnumerator())
                {
                    while (enumerator5.MoveNext())
                    {
                        JsonSchemaModel current5 = enumerator5.get_Current();
                        this.ValidateBoolean(current5);
                    }
                }
                return;

            case JsonToken.Null:
                this.ProcessValue();
                using (IEnumerator <JsonSchemaModel> enumerator6 = this.CurrentMemberSchemas.GetEnumerator())
                {
                    while (enumerator6.MoveNext())
                    {
                        JsonSchemaModel current6 = enumerator6.get_Current();
                        this.ValidateNull(current6);
                    }
                }
                return;

            case JsonToken.Undefined:
                return;

            case JsonToken.EndObject:
                using (IEnumerator <JsonSchemaModel> enumerator7 = this.CurrentSchemas.GetEnumerator())
                {
                    while (enumerator7.MoveNext())
                    {
                        JsonSchemaModel current7 = enumerator7.get_Current();
                        this.ValidateEndObject(current7);
                    }
                }
                this.Pop();
                return;

            case JsonToken.EndArray:
                using (IEnumerator <JsonSchemaModel> enumerator8 = this.CurrentSchemas.GetEnumerator())
                {
                    while (enumerator8.MoveNext())
                    {
                        JsonSchemaModel current8 = enumerator8.get_Current();
                        this.ValidateEndArray(current8);
                    }
                }
                this.Pop();
                return;

            case JsonToken.EndConstructor:
                this.Pop();
                return;

            case JsonToken.Date:
                return;
            }
            throw new ArgumentOutOfRangeException();
        }
Exemplo n.º 9
0
        private void ValidateCurrentToken()
        {
            if (_model == null)
            {
                JsonSchemaModelBuilder jsonSchemaModelBuilder = new JsonSchemaModelBuilder();
                _model = jsonSchemaModelBuilder.Build(_schema);
            }
            switch (_reader.TokenType)
            {
            case JsonToken.StartObject:
            {
                ProcessValue();
                IList <JsonSchemaModel> schemas2 = CurrentMemberSchemas.Where(ValidateObject).ToList();
                Push(new SchemaScope(JTokenType.Object, schemas2));
                break;
            }

            case JsonToken.StartArray:
            {
                ProcessValue();
                IList <JsonSchemaModel> schemas = CurrentMemberSchemas.Where(ValidateArray).ToList();
                Push(new SchemaScope(JTokenType.Array, schemas));
                break;
            }

            case JsonToken.StartConstructor:
                Push(new SchemaScope(JTokenType.Constructor, null));
                break;

            case JsonToken.PropertyName:
                foreach (JsonSchemaModel currentSchema in CurrentSchemas)
                {
                    ValidatePropertyName(currentSchema);
                }
                break;

            case JsonToken.Raw:
                break;

            case JsonToken.Integer:
                ProcessValue();
                foreach (JsonSchemaModel currentMemberSchema in CurrentMemberSchemas)
                {
                    ValidateInteger(currentMemberSchema);
                }
                break;

            case JsonToken.Float:
                ProcessValue();
                foreach (JsonSchemaModel currentMemberSchema2 in CurrentMemberSchemas)
                {
                    ValidateFloat(currentMemberSchema2);
                }
                break;

            case JsonToken.String:
                ProcessValue();
                foreach (JsonSchemaModel currentMemberSchema3 in CurrentMemberSchemas)
                {
                    ValidateString(currentMemberSchema3);
                }
                break;

            case JsonToken.Boolean:
                ProcessValue();
                foreach (JsonSchemaModel currentMemberSchema4 in CurrentMemberSchemas)
                {
                    ValidateBoolean(currentMemberSchema4);
                }
                break;

            case JsonToken.Null:
                ProcessValue();
                foreach (JsonSchemaModel currentMemberSchema5 in CurrentMemberSchemas)
                {
                    ValidateNull(currentMemberSchema5);
                }
                break;

            case JsonToken.Undefined:
                break;

            case JsonToken.EndObject:
                foreach (JsonSchemaModel currentSchema2 in CurrentSchemas)
                {
                    ValidateEndObject(currentSchema2);
                }
                Pop();
                break;

            case JsonToken.EndArray:
                foreach (JsonSchemaModel currentSchema3 in CurrentSchemas)
                {
                    ValidateEndArray(currentSchema3);
                }
                Pop();
                break;

            case JsonToken.EndConstructor:
                Pop();
                break;

            case JsonToken.Date:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Reads the next JSON token from the stream.
        /// </summary>
        /// <returns>
        /// true if the next token was read successfully; false if there are no more tokens to read.
        /// </returns>
        public override bool Read()
        {
            if (!_reader.Read())
            {
                return(false);
            }

            if (_reader.TokenType == JsonToken.Comment)
            {
                return(true);
            }

            // first time Read has been called. build model
            if (_model == null)
            {
                JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
                _model = builder.Build(_schema);
            }

            switch (_reader.TokenType)
            {
            case JsonToken.StartObject:
                ProcessValue();
                JsonSchemaModel objectSchema = (ValidateObject(CurrentMemberSchema))
            ? CurrentMemberSchema
            : null;
                Push(new SchemaScope(JTokenType.Object, objectSchema));
                break;

            case JsonToken.StartArray:
                ProcessValue();
                JsonSchemaModel arraySchema = (ValidateArray(CurrentMemberSchema))
            ? CurrentMemberSchema
            : null;
                Push(new SchemaScope(JTokenType.Array, arraySchema));
                break;

            case JsonToken.StartConstructor:
                Push(new SchemaScope(JTokenType.Constructor, null));
                break;

            case JsonToken.PropertyName:
                ValidatePropertyName(CurrentSchema);
                break;

            case JsonToken.Raw:
                break;

            case JsonToken.Integer:
                ProcessValue();
                ValidateInteger(CurrentMemberSchema);
                break;

            case JsonToken.Float:
                ProcessValue();
                ValidateFloat(CurrentMemberSchema);
                break;

            case JsonToken.String:
                ProcessValue();
                ValidateString(CurrentMemberSchema);
                break;

            case JsonToken.Boolean:
                ProcessValue();
                ValidateBoolean(CurrentMemberSchema);
                break;

            case JsonToken.Null:
                ProcessValue();
                ValidateNull(CurrentMemberSchema);
                break;

            case JsonToken.Undefined:
                break;

            case JsonToken.EndObject:
                ValidateEndObject(CurrentSchema);
                Pop();
                break;

            case JsonToken.EndArray:
                ValidateEndArray(CurrentSchema);
                Pop();
                break;

            case JsonToken.EndConstructor:
                Pop();
                break;

            case JsonToken.Date:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(true);
        }
Exemplo n.º 11
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();

            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.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("String 'secasecasecasecaseca' exceeds maximum length of 10. Line 3, position 41.", 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("String 'aaa' is less than minimum length of 7. Line 5, position 39.", errors[1]);
            Assert.AreEqual("String 'aaa' does not match regex pattern 'hi'. Line 5, position 39.", errors[2]);
            Assert.AreEqual("String 'aaa' does not match regex pattern 'hi2u'. Line 5, position 39.", 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("String 'three' is less than minimum length of 6. Line 6, position 24.", 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());
        }
Exemplo n.º 12
0
        private void ValidateCurrentToken()
        {
            if (this._model == null)
            {
                JsonSchemaModelBuilder jsonSchemaModelBuilder = new JsonSchemaModelBuilder();
                this._model = jsonSchemaModelBuilder.Build(this._schema);
            }
            switch (this._reader.TokenType)
            {
            case JsonToken.StartObject:
            {
                this.ProcessValue();
                IList <JsonSchemaModel> schemas = this.CurrentMemberSchemas.Where(new Func <JsonSchemaModel, bool>(this.ValidateObject)).ToList <JsonSchemaModel>();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Object, schemas));
                return;
            }

            case JsonToken.StartArray:
            {
                this.ProcessValue();
                IList <JsonSchemaModel> schemas2 = this.CurrentMemberSchemas.Where(new Func <JsonSchemaModel, bool>(this.ValidateArray)).ToList <JsonSchemaModel>();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Array, schemas2));
                return;
            }

            case JsonToken.StartConstructor:
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Constructor, null));
                return;

            case JsonToken.PropertyName:
                foreach (JsonSchemaModel schema in this.CurrentSchemas)
                {
                    this.ValidatePropertyName(schema);
                }
                return;

            case JsonToken.Raw:
                return;

            case JsonToken.Integer:
                this.ProcessValue();
                foreach (JsonSchemaModel schema2 in this.CurrentMemberSchemas)
                {
                    this.ValidateInteger(schema2);
                }
                return;

            case JsonToken.Float:
                this.ProcessValue();
                foreach (JsonSchemaModel schema3 in this.CurrentMemberSchemas)
                {
                    this.ValidateFloat(schema3);
                }
                return;

            case JsonToken.String:
                this.ProcessValue();
                foreach (JsonSchemaModel schema4 in this.CurrentMemberSchemas)
                {
                    this.ValidateString(schema4);
                }
                return;

            case JsonToken.Boolean:
                this.ProcessValue();
                foreach (JsonSchemaModel schema5 in this.CurrentMemberSchemas)
                {
                    this.ValidateBoolean(schema5);
                }
                return;

            case JsonToken.Null:
                this.ProcessValue();
                foreach (JsonSchemaModel schema6 in this.CurrentMemberSchemas)
                {
                    this.ValidateNull(schema6);
                }
                return;

            case JsonToken.Undefined:
                return;

            case JsonToken.EndObject:
                foreach (JsonSchemaModel schema7 in this.CurrentSchemas)
                {
                    this.ValidateEndObject(schema7);
                }
                this.Pop();
                return;

            case JsonToken.EndArray:
                foreach (JsonSchemaModel schema8 in this.CurrentSchemas)
                {
                    this.ValidateEndArray(schema8);
                }
                this.Pop();
                return;

            case JsonToken.EndConstructor:
                this.Pop();
                return;

            case JsonToken.Date:
                return;
            }
            throw new ArgumentOutOfRangeException();
        }
Exemplo n.º 13
0
        // Token: 0x06000D88 RID: 3464 RVA: 0x00054628 File Offset: 0x00052828
        private void ValidateCurrentToken()
        {
            if (this._model == null)
            {
                JsonSchemaModelBuilder jsonSchemaModelBuilder = new JsonSchemaModelBuilder();
                this._model = jsonSchemaModelBuilder.Build(this._schema);
                if (!JsonTokenUtils.IsStartToken(this._reader.TokenType))
                {
                    this.Push(new JsonValidatingReader.SchemaScope(JTokenType.None, this.CurrentMemberSchemas));
                }
            }
            switch (this._reader.TokenType)
            {
            case JsonToken.None:
                return;

            case JsonToken.StartObject:
            {
                this.ProcessValue();
                IList <JsonSchemaModel> schemas = this.CurrentMemberSchemas.Where(new Func <JsonSchemaModel, bool>(this.ValidateObject)).ToList <JsonSchemaModel>();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Object, schemas));
                this.WriteToken(this.CurrentSchemas);
                return;
            }

            case JsonToken.StartArray:
            {
                this.ProcessValue();
                IList <JsonSchemaModel> schemas2 = this.CurrentMemberSchemas.Where(new Func <JsonSchemaModel, bool>(this.ValidateArray)).ToList <JsonSchemaModel>();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Array, schemas2));
                this.WriteToken(this.CurrentSchemas);
                return;
            }

            case JsonToken.StartConstructor:
                this.ProcessValue();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Constructor, null));
                this.WriteToken(this.CurrentSchemas);
                return;

            case JsonToken.PropertyName:
                this.WriteToken(this.CurrentSchemas);
                using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentSchemas.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        JsonSchemaModel schema = enumerator.Current;
                        this.ValidatePropertyName(schema);
                    }
                    return;
                }
                break;

            case JsonToken.Raw:
                this.ProcessValue();
                return;

            case JsonToken.Integer:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentMemberSchemas.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        JsonSchemaModel schema2 = enumerator.Current;
                        this.ValidateInteger(schema2);
                    }
                    return;
                }
                goto IL_1DE;

            case JsonToken.Float:
                goto IL_1DE;

            case JsonToken.String:
                goto IL_22B;

            case JsonToken.Boolean:
                goto IL_278;

            case JsonToken.Null:
                goto IL_2C2;

            case JsonToken.Undefined:
            case JsonToken.Date:
            case JsonToken.Bytes:
                this.WriteToken(this.CurrentMemberSchemas);
                return;

            case JsonToken.EndObject:
                this.WriteToken(this.CurrentSchemas);
                foreach (JsonSchemaModel schema3 in this.CurrentSchemas)
                {
                    this.ValidateEndObject(schema3);
                }
                this.Pop();
                return;

            case JsonToken.EndArray:
                this.WriteToken(this.CurrentSchemas);
                foreach (JsonSchemaModel schema4 in this.CurrentSchemas)
                {
                    this.ValidateEndArray(schema4);
                }
                this.Pop();
                return;

            case JsonToken.EndConstructor:
                this.WriteToken(this.CurrentSchemas);
                this.Pop();
                return;
            }
            throw new ArgumentOutOfRangeException();
IL_1DE:
            this.ProcessValue();
            this.WriteToken(this.CurrentMemberSchemas);
            using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentMemberSchemas.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    JsonSchemaModel schema5 = enumerator.Current;
                    this.ValidateFloat(schema5);
                }
                return;
            }
IL_22B:
            this.ProcessValue();
            this.WriteToken(this.CurrentMemberSchemas);
            using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentMemberSchemas.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    JsonSchemaModel schema6 = enumerator.Current;
                    this.ValidateString(schema6);
                }
                return;
            }
IL_278:
            this.ProcessValue();
            this.WriteToken(this.CurrentMemberSchemas);
            using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentMemberSchemas.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    JsonSchemaModel schema7 = enumerator.Current;
                    this.ValidateBoolean(schema7);
                }
                return;
            }
IL_2C2:
            this.ProcessValue();
            this.WriteToken(this.CurrentMemberSchemas);
            foreach (JsonSchemaModel schema8 in this.CurrentMemberSchemas)
            {
                this.ValidateNull(schema8);
            }
        }
Exemplo n.º 14
0
        private void ValidateCurrentToken()
        {
            if (this._model == null)
            {
                JsonSchemaModelBuilder jsonSchemaModelBuilder = new JsonSchemaModelBuilder();
                this._model = jsonSchemaModelBuilder.Build(this._schema);
                if (!JsonWriter.IsStartToken(this._reader.TokenType))
                {
                    this.Push(new JsonValidatingReader.SchemaScope(JTokenType.None, this.CurrentMemberSchemas));
                }
            }
            switch (this._reader.TokenType)
            {
            case JsonToken.None:
                return;

            case JsonToken.StartObject:
            {
                this.ProcessValue();
                IList <JsonSchemaModel> schemas = this.CurrentMemberSchemas.Where(new Func <JsonSchemaModel, bool>(this.ValidateObject)).ToList <JsonSchemaModel>();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Object, schemas));
                this.WriteToken(this.CurrentSchemas);
                return;
            }

            case JsonToken.StartArray:
            {
                this.ProcessValue();
                IList <JsonSchemaModel> schemas2 = this.CurrentMemberSchemas.Where(new Func <JsonSchemaModel, bool>(this.ValidateArray)).ToList <JsonSchemaModel>();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Array, schemas2));
                this.WriteToken(this.CurrentSchemas);
                return;
            }

            case JsonToken.StartConstructor:
                this.ProcessValue();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Constructor, null));
                this.WriteToken(this.CurrentSchemas);
                return;

            case JsonToken.PropertyName:
                this.WriteToken(this.CurrentSchemas);
                using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentSchemas.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        JsonSchemaModel current = enumerator.Current;
                        this.ValidatePropertyName(current);
                    }
                    return;
                }
                break;

            case JsonToken.Comment:
                goto IL_3F9;

            case JsonToken.Raw:
                break;

            case JsonToken.Integer:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                using (IEnumerator <JsonSchemaModel> enumerator2 = this.CurrentMemberSchemas.GetEnumerator())
                {
                    while (enumerator2.MoveNext())
                    {
                        JsonSchemaModel current2 = enumerator2.Current;
                        this.ValidateInteger(current2);
                    }
                    return;
                }
                goto IL_1E8;

            case JsonToken.Float:
                goto IL_1E8;

            case JsonToken.String:
                goto IL_23A;

            case JsonToken.Boolean:
                goto IL_28C;

            case JsonToken.Null:
                goto IL_2DE;

            case JsonToken.Undefined:
            case JsonToken.Date:
            case JsonToken.Bytes:
                this.WriteToken(this.CurrentMemberSchemas);
                return;

            case JsonToken.EndObject:
                goto IL_330;

            case JsonToken.EndArray:
                this.WriteToken(this.CurrentSchemas);
                foreach (JsonSchemaModel current3 in this.CurrentSchemas)
                {
                    this.ValidateEndArray(current3);
                }
                this.Pop();
                return;

            case JsonToken.EndConstructor:
                this.WriteToken(this.CurrentSchemas);
                this.Pop();
                return;

            default:
                goto IL_3F9;
            }
            this.ProcessValue();
            return;

IL_1E8:
            this.ProcessValue();
            this.WriteToken(this.CurrentMemberSchemas);
            using (IEnumerator <JsonSchemaModel> enumerator4 = this.CurrentMemberSchemas.GetEnumerator())
            {
                while (enumerator4.MoveNext())
                {
                    JsonSchemaModel current4 = enumerator4.Current;
                    this.ValidateFloat(current4);
                }
                return;
            }
IL_23A:
            this.ProcessValue();
            this.WriteToken(this.CurrentMemberSchemas);
            using (IEnumerator <JsonSchemaModel> enumerator5 = this.CurrentMemberSchemas.GetEnumerator())
            {
                while (enumerator5.MoveNext())
                {
                    JsonSchemaModel current5 = enumerator5.Current;
                    this.ValidateString(current5);
                }
                return;
            }
IL_28C:
            this.ProcessValue();
            this.WriteToken(this.CurrentMemberSchemas);
            using (IEnumerator <JsonSchemaModel> enumerator6 = this.CurrentMemberSchemas.GetEnumerator())
            {
                while (enumerator6.MoveNext())
                {
                    JsonSchemaModel current6 = enumerator6.Current;
                    this.ValidateBoolean(current6);
                }
                return;
            }
IL_2DE:
            this.ProcessValue();
            this.WriteToken(this.CurrentMemberSchemas);
            using (IEnumerator <JsonSchemaModel> enumerator7 = this.CurrentMemberSchemas.GetEnumerator())
            {
                while (enumerator7.MoveNext())
                {
                    JsonSchemaModel current7 = enumerator7.Current;
                    this.ValidateNull(current7);
                }
                return;
            }
IL_330:
            this.WriteToken(this.CurrentSchemas);
            foreach (JsonSchemaModel current8 in this.CurrentSchemas)
            {
                this.ValidateEndObject(current8);
            }
            this.Pop();
            return;

IL_3F9:
            throw new ArgumentOutOfRangeException();
        }
Exemplo n.º 15
0
        private void ValidateCurrentToken()
        {
            // first time validate has been called. build model
            if (this._model == null)
            {
                JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
                this._model = builder.Build(this._schema);
            }

            switch (this.Reader.TokenType)
            {
            case JsonToken.StartObject:
                this.ProcessValue();
                IList <JsonSchemaModel> objectSchemas = this.CurrentMemberSchemas.Where(this.ValidateObject).ToList();
                this.Push(new SchemaScope(JTokenType.Object, objectSchemas));
                break;

            case JsonToken.StartArray:
                this.ProcessValue();
                IList <JsonSchemaModel> arraySchemas = this.CurrentMemberSchemas.Where(this.ValidateArray).ToList();
                this.Push(new SchemaScope(JTokenType.Array, arraySchemas));
                break;

            case JsonToken.StartConstructor:
                this.Push(new SchemaScope(JTokenType.Constructor, null));
                break;

            case JsonToken.PropertyName:
                foreach (JsonSchemaModel schema in this.CurrentSchemas)
                {
                    this.ValidatePropertyName(schema);
                }
                break;

            case JsonToken.Raw:
                break;

            case JsonToken.Integer:
                this.ProcessValue();
                foreach (JsonSchemaModel schema in this.CurrentMemberSchemas)
                {
                    this.ValidateInteger(schema);
                }
                break;

            case JsonToken.Float:
                this.ProcessValue();
                foreach (JsonSchemaModel schema in this.CurrentMemberSchemas)
                {
                    this.ValidateFloat(schema);
                }
                break;

            case JsonToken.String:
                this.ProcessValue();
                foreach (JsonSchemaModel schema in this.CurrentMemberSchemas)
                {
                    this.ValidateString(schema);
                }
                break;

            case JsonToken.Boolean:
                this.ProcessValue();
                foreach (JsonSchemaModel schema in this.CurrentMemberSchemas)
                {
                    this.ValidateBoolean(schema);
                }
                break;

            case JsonToken.Null:
                this.ProcessValue();
                foreach (JsonSchemaModel schema in this.CurrentMemberSchemas)
                {
                    this.ValidateNull(schema);
                }
                break;

            case JsonToken.Undefined:
                break;

            case JsonToken.EndObject:
                foreach (JsonSchemaModel schema in this.CurrentSchemas)
                {
                    this.ValidateEndObject(schema);
                }
                this.Pop();
                break;

            case JsonToken.EndArray:
                foreach (JsonSchemaModel schema in this.CurrentSchemas)
                {
                    this.ValidateEndArray(schema);
                }
                this.Pop();
                break;

            case JsonToken.EndConstructor:
                this.Pop();
                break;

            case JsonToken.Date:
            case JsonToken.Bytes:
                // these have no equivalent in JSON schema
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }