public void JSchemaValidatingWriter() { string schemaJson = "{}"; #region JSchemaValidatingWriter Person person = new Person { Name = "James", Hobbies = new List<string> { ".NET", "Blogging", "Reading", "Xbox", "LOLCATS" } }; StringWriter stringWriter = new StringWriter(); JsonTextWriter writer = new JsonTextWriter(stringWriter); JSchemaValidatingWriter validatingWriter = new JSchemaValidatingWriter(writer); validatingWriter.Schema = JSchema.Parse(schemaJson); IList<string> messages = new List<string>(); validatingWriter.ValidationEventHandler += (o, a) => messages.Add(a.Message); JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(validatingWriter, person); #endregion Assert.AreEqual(0, messages.Count); }
public WriterValidator(JSchemaValidatingWriter writer) : base(writer) { _writer = writer; }
public void WriteSpecTest(SchemaSpecTest schemaSpecTest) { Console.WriteLine("Running writer JSON Schema {0} test {1}: {2}", schemaSpecTest.Version, schemaSpecTest.TestNumber, schemaSpecTest); IList<string> errorMessages = new List<string>(); JSchemaPreloadedResolver resolver = GetResolver(); JSchema s = JSchema.Load(schemaSpecTest.Schema.CreateReader(), resolver); JsonReader jsonReader = schemaSpecTest.Data.CreateReader(); StringWriter sw = new StringWriter(); JsonTextWriter writer = new JsonTextWriter(sw); using (JSchemaValidatingWriter validatingWriter = new JSchemaValidatingWriter(writer)) { validatingWriter.Schema = s; validatingWriter.ValidationEventHandler += (sender, args) => errorMessages.Add(args.Message); while (jsonReader.Read()) { validatingWriter.WriteToken(jsonReader.TokenType, jsonReader.Value); } } bool isValid = (errorMessages.Count == 0); Assert.AreEqual(schemaSpecTest.IsValid, isValid, schemaSpecTest.TestCaseDescription + " - " + schemaSpecTest.TestDescription + " - errors: " + StringHelpers.Join(", ", errorMessages)); }