public static JsonSchemaValidationException Validate(HashSet <string> required, Dictionary <string, JsonSchema> properties, JsonSchemaValidationContext c, T o) { prepareValidator(); return(s_validator.Validate(required, properties, c, o)); }
public void Serialize(JsonObjectValidator objectValidator, IFormatter f, JsonSchemaValidationContext c, T o) { // Validates fields var validationResults = new Dictionary <string, ValidationResult>(); GenericValidator <T> .ValidationResults( objectValidator.Required, objectValidator.Properties, c, o, validationResults); // Serialize fields f.BeginMap(objectValidator.Properties.Count()); foreach (var property in objectValidator.Properties) { var fieldName = property.Key; var schema = property.Value; string[] deps = null; objectValidator.Dependencies.TryGetValue(fieldName, out deps); FieldSerializer fs; if (m_serializers.TryGetValue(fieldName, out fs)) { fs(schema, c, f, o, validationResults, deps); } } f.EndMap(); }
public JsonSchemaValidationException ValidateProperty( HashSet <string> required, KeyValuePair <string, JsonSchema> property, JsonSchemaValidationContext c, T o, out bool isIgnorable ) { var fieldName = property.Key; var schema = property.Value; isIgnorable = false; FieldValidator fv; if (m_validators.TryGetValue(fieldName, out fv)) { var isRequired = required != null && required.Contains(fieldName); bool isMemberIgnorable; var ex = fv(schema, c, o, out isMemberIgnorable); if (ex != null) { isIgnorable = !isRequired && isMemberIgnorable; if (isRequired || // required fields must be checked c.EnableDiagnosisForNotRequiredFields) { return(ex); } } } return(null); }
internal static void ValidationResults(HashSet <string> required, Dictionary <string, JsonSchema> properties, JsonSchemaValidationContext c, T o, Dictionary <string, ValidationResult> results) { prepareValidator(); s_validator.ValidationResults(required, properties, c, o, results); }
public static void Serialize(JsonObjectValidator objectValidator, IFormatter f, JsonSchemaValidationContext c, T value) { if (s_serializer == null) { s_serializer = new Serializer(); } s_serializer.Serialize(objectValidator, f, c, value); }
public JsonSchemaValidationException Validate <T>(JsonSchemaValidationContext c, T o) { if (Values.Contains(GenericCast <T, int> .Cast(o))) { return(null); } else { return(new JsonSchemaValidationException(c, string.Format("{0} is not valid enum", o))); } }
public void HasDictionaryObjectValidator() { var c = new JsonSchemaValidationContext("test"); { var s = JsonSchema.FromType <HasDictionary>(); Assert.Null(s.Validator.Validate(c, new HasDictionary())); } Assert.True(c.IsEmpty()); }
public void StringEnumValidator() { var c = new JsonSchemaValidationContext("test"); { var v = JsonStringEnumValidator.Create(new string[] { "a", "b" }, EnumSerializationType.AsString); Assert.Null(v.Validate(c, "a")); Assert.NotNull(v.Validate(c, "c")); } Assert.True(c.IsEmpty()); }
public void IntEnumValidator() { var c = new JsonSchemaValidationContext("test"); { var v = new JsonIntEnumValidator(); v.Values = new int[] { 1, 2 }; Assert.Null(v.Validate(c, 1)); Assert.NotNull(v.Validate(c, 3)); } Assert.True(c.IsEmpty()); }
public JsonSchemaValidationException Validate <T>(JsonSchemaValidationContext c, T o) { if (o == null) { return(new JsonSchemaValidationException(c, "null")); } if (Properties.Count < MinProperties) { return(new JsonSchemaValidationException(c, "no properties")); } return(GenericValidator <T> .Validate(Required, Properties, c, o)); }
private static void ListSerializer <U>(IJsonSchemaValidator v, IFormatter f, JsonSchemaValidationContext c, List <U> list) { f.BeginList(list.Count); { //int i = 0; foreach (var x in list) //using (c.Push(i++)) { v.Serialize(f, c, x); } } f.EndList(); }
public static void Serialize(JsonStringEnumValidator validator, IFormatter f, JsonSchemaValidationContext c, T o) { if (s_serializer == null) { var t = typeof(T); if (t.IsEnum) { s_serializer = (vv, ff, cc, oo) => { var value = Enum.GetName(t, oo); if (vv.SerializationType == EnumSerializationType.AsLowerString) { value = value.ToLower(); } else if (vv.SerializationType == EnumSerializationType.AsUpperString) { value = value.ToUpper(); } ff.Value(value); } } ; else if (t == typeof(string)) { s_serializer = (vv, ff, cc, oo) => { var value = GenericCast <T, string> .Cast(oo); if (vv.SerializationType == EnumSerializationType.AsLowerString) { value = value.ToLower(); } else if (vv.SerializationType == EnumSerializationType.AsUpperString) { value = value.ToUpper(); } ff.Value(value); } } ; else { throw new NotImplementedException(); } } s_serializer(validator, f, c, o); }
public void TestHasDeps() { var obj = new HasDepsTest(); var s = JsonSchema.FromType <HasDepsTest>(); { var c = new JsonSchemaValidationContext(obj); Assert.Null(s.Validator.Validate(c, s)); } var actual = s.Serialize(obj); var expected = @"{""X"":0,""Y"":0}"; Assert.AreEqual(expected, actual); }
public void TestObjectNestedWithNull() { var obj = new ObjectNestedTest(); var s = JsonSchema.FromType <ObjectNestedTest>(); { var c = new JsonSchemaValidationContext(obj); Assert.Null(s.Validator.Validate(c, s)); } var actual = s.Serialize(obj); var expected = @"{}"; Assert.AreEqual(expected, actual); }
public void ObjectValidator() { var c = new JsonSchemaValidationContext("test"); { var s = JsonSchema.FromType <Hoge>(); Assert.Null(s.Validator.Validate(c, new Hoge { Value = 1 })); Assert.NotNull(s.Validator.Validate(c, new Hoge { Value = 0 })); } Assert.True(c.IsEmpty()); }
public JsonSchemaValidationException Validate( HashSet <string> required, Dictionary <string, JsonSchema> properties, JsonSchemaValidationContext c, T o) { foreach (var kv in properties) { bool isIgnorable; var ex = ValidateProperty(required, kv, c, o, out isIgnorable); if (ex != null && !isIgnorable) { return(ex); } } return(null); }
public void ObjectValidatorForNotRequiredWithIgnorable() { { var c = new JsonSchemaValidationContext("test") { EnableDiagnosisForNotRequiredFields = false, // Default behaviour }; var s = JsonSchema.FromType <NotRequiredWithIgnorable>(); // An error is not returned because Value is not 'Required' and the diagnosis is not enabled Assert.Null(s.Validator.Validate(c, new NotRequiredWithIgnorable { Value = 0 })); Assert.True(c.IsEmpty()); } { var c = new JsonSchemaValidationContext("test") { EnableDiagnosisForNotRequiredFields = true, }; var s = JsonSchema.FromType <NotRequiredWithIgnorable>(); Assert.NotNull(s.Validator.Validate(c, new NotRequiredWithIgnorable { Value = 0 })); Assert.True(c.IsEmpty()); } { var c = new JsonSchemaValidationContext("test") { EnableDiagnosisForNotRequiredFields = true, }; var s = JsonSchema.FromType <NotRequiredWithIgnorable>(); // An error is NOT returned even though diagnosis is enabled because of an ignorable value is matched Assert.Null(s.Validator.Validate(c, new NotRequiredWithIgnorable { Value = -1 })); Assert.True(c.IsEmpty()); } }
public void HasRequiredStringObjectValidator() { { var c = new JsonSchemaValidationContext("test") { EnableDiagnosisForNotRequiredFields = true, }; var s = JsonSchema.FromType <HasRequiredStringObject>(); Assert.NotNull(s.Validator.Validate(c, new HasRequiredStringObject())); Assert.Null(s.Validator.Validate(c, new HasRequiredStringObject { s = "" })); Assert.True(c.IsEmpty()); } }
public void ValidationResults (HashSet <string> required, Dictionary <string, JsonSchema> properties, JsonSchemaValidationContext c, T o, Dictionary <string, ValidationResult> results) { foreach (var kv in properties) { bool isIgnorable; var ex = ValidateProperty(required, kv, c, o, out isIgnorable); results.Add(kv.Key, new ValidationResult { IsIgnorable = isIgnorable, Ex = ex, }); } }
public void TestNestedRequired() { var obj = new NestedRequiredTestParent() { C = new NestedRequiredTestChild(), }; var s = JsonSchema.FromType <NestedRequiredTestParent>(); { var c = new JsonSchemaValidationContext(obj); Assert.Null(s.Validator.Validate(c, s)); } var actual = s.Serialize(obj); var expected = @"{""C"":{}}"; Assert.AreEqual(expected, actual); }
public void TestObjectNested() { var obj = new ObjectNestedTest() { C = new CheckConstraintsTest(), }; var s = JsonSchema.FromType <ObjectNestedTest>(); { var c = new JsonSchemaValidationContext(obj); Assert.Null(s.Validator.Validate(c, s)); } var actual = s.Serialize(obj); var expected = @"{""C"":{""X"":0}}"; Assert.AreEqual(expected, actual); }
public void TestCheckConstraints() { var obj = new CheckConstraintsTest() { X = 0, Y = 0, // Will be excluded because 0 doesn't satisfy a requirement of "Minimum = 10" }; var s = JsonSchema.FromType <CheckConstraintsTest>(); { var c = new JsonSchemaValidationContext(obj); Assert.Null(s.Validator.Validate(c, s)); } var actual = s.Serialize(obj); var expected = @"{""X"":0}"; Assert.AreEqual(expected, actual); }
public void IntValidator() { var c = new JsonSchemaValidationContext("test"); { var v = new JsonIntValidator(); v.Maximum = 0; Assert.NotNull(v.Validate(c, 1)); Assert.Null(v.Validate(c, 0)); Assert.Null(v.Validate(c, -1)); } { var v = new JsonIntValidator(); v.Maximum = 0; v.ExclusiveMaximum = true; Assert.NotNull(v.Validate(c, 1)); Assert.NotNull(v.Validate(c, 0)); Assert.Null(v.Validate(c, -1)); } { var v = new JsonIntValidator(); v.Minimum = 0; Assert.Null(v.Validate(c, 1)); Assert.Null(v.Validate(c, 0)); Assert.NotNull(v.Validate(c, -1)); } { var v = new JsonIntValidator(); v.Minimum = 0; v.ExclusiveMinimum = true; Assert.Null(v.Validate(c, 1)); Assert.NotNull(v.Validate(c, 0)); Assert.NotNull(v.Validate(c, -1)); } { var v = new JsonIntValidator(); v.MultipleOf = 4; Assert.Null(v.Validate(c, 4)); Assert.NotNull(v.Validate(c, 5)); } Assert.True(c.IsEmpty()); }
public JsonSchemaValidationException Validate <S>(JsonSchemaValidationContext c, S o) { if (o == null) { return(new JsonSchemaValidationException(c, "null")); } var d = o as IDictionary <string, T>; if (d == null) { return(new JsonSchemaValidationException(c, "not dictionary")); } if (Required != null) { foreach (var x in Required) { using (c.Push(x)) { // ToDo } } } if (AdditionalProperties != null) { foreach (var kv in d) { using (c.Push(kv.Key)) { var result = AdditionalProperties.Validator.Validate(c, kv.Value); if (result != null) { return(result); } } } } return(null); }
public void Serialize <T>(IFormatter f, T o, JsonSchemaValidationContext c = null) { if (c == null) { c = new JsonSchemaValidationContext(o) { EnableDiagnosisForNotRequiredFields = true, } } ; var ex = Validator.Validate(c, o); if (ex != null) { throw ex; } Validator.Serialize(f, c, o); }
public void ArrayValidator() { var c = new JsonSchemaValidationContext("test"); { var v = new JsonArrayValidator(); v.MaxItems = 1; Assert.Null(v.Validate(c, new object[] { 0 })); Assert.NotNull(v.Validate(c, new object[] { 0, 1 })); } { var v = new JsonArrayValidator(); v.MinItems = 1; Assert.Null(v.Validate(c, new object[] { 0 })); Assert.NotNull(v.Validate(c, new object[] { })); } Assert.True(c.IsEmpty()); }
public static void Serialize(IJsonSchemaValidator v, IFormatter f, JsonSchemaValidationContext c, T o) { if (s_serializer == null) { var t = typeof(T); MethodInfo g = null; if (t.IsArray) { var mi = typeof(JsonArrayValidator).GetMethod("ArraySerializer", BindingFlags.Static | BindingFlags.NonPublic); g = mi.MakeGenericMethod(t.GetElementType()); } else if (t.GetIsGenericList()) { // ToDo: IList var mi = typeof(JsonArrayValidator).GetMethod("ListSerializer", BindingFlags.Static | BindingFlags.NonPublic); g = mi.MakeGenericMethod(t.GetGenericArguments()); } else { throw new NotImplementedException(); } var compiled = (Action < IJsonSchemaValidator, IFormatter, JsonSchemaValidationContext, T>) GenericInvokeCallFactory.StaticAction < IJsonSchemaValidator, IFormatter, JsonSchemaValidationContext, T>(g); s_serializer = new Serializer(compiled); } s_serializer(v, f, c, o); }
public void StringValidator() { var c = new JsonSchemaValidationContext("test"); { var v = new JsonStringValidator(); Assert.Null(v.Validate(c, "")); Assert.Null(v.Validate(c, "a")); } { var v = new JsonStringValidator(); v.MinLength = 1; Assert.Null(v.Validate(c, "a")); Assert.NotNull(v.Validate(c, "")); } { var v = new JsonStringValidator(); v.MaxLength = 1; Assert.Null(v.Validate(c, "a")); Assert.NotNull(v.Validate(c, "ab")); } { var v = new JsonStringValidator(); v.Pattern = new System.Text.RegularExpressions.Regex("abc"); Assert.Null(v.Validate(c, "abc")); Assert.NotNull(v.Validate(c, "ab")); } { var v = new JsonStringValidator(); v.Pattern = new System.Text.RegularExpressions.Regex("ab+"); Assert.Null(v.Validate(c, "abb")); Assert.Null(v.Validate(c, "ab")); Assert.NotNull(v.Validate(c, "a")); } Assert.True(c.IsEmpty()); }
public void Serialize <S>(IFormatter f, JsonSchemaValidationContext c, S o) { // validate properties m_validValueMap.Clear(); var dict = o as Dictionary <string, T>; f.BeginMap(dict.Count); { foreach (var kv in dict) { // key f.Key(kv.Key); // value //using (c.Push(kv.Key)) { AdditionalProperties.Validator.Serialize(f, c, kv.Value); } } } f.EndMap(); }
public void HasArrayObjectValidator() { { var c = new JsonSchemaValidationContext("test") { EnableDiagnosisForNotRequiredFields = true, }; var s = JsonSchema.FromType <HasArrayOBject>(); Assert.Null(s.Validator.Validate(c, new HasArrayOBject { xs = new float[] { } })); Assert.Null(s.Validator.Validate(c, new HasArrayOBject { xs = new float[] { 0.5f } })); Assert.NotNull(s.Validator.Validate(c, new HasArrayOBject { xs = new float[] { 1.5f } })); Assert.True(c.IsEmpty()); } }