From 4d01aff7e37e53d2085d9b1d5cae359d56d2eeee Mon Sep 17 00:00:00 2001 From: yutopp Date: Wed, 9 Jan 2019 22:28:58 +0900 Subject: [PATCH] Validator checks elements in Array/List. Validator allows Count == 0 --- .../Editor/Tests/Json/ValidatorTests.cs | 50 +++++++++++++++++++ .../JsonSchemaValidator/JsonArrayValidator.cs | 34 +++++++++++-- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/Assets/VRM/UniJSON/Editor/Tests/Json/ValidatorTests.cs b/Assets/VRM/UniJSON/Editor/Tests/Json/ValidatorTests.cs index fc50234b3..b5661c51b 100644 --- a/Assets/VRM/UniJSON/Editor/Tests/Json/ValidatorTests.cs +++ b/Assets/VRM/UniJSON/Editor/Tests/Json/ValidatorTests.cs @@ -330,5 +330,55 @@ namespace UniJSON Assert.True(c.IsEmpty()); } + + class HasArrayOBject + { + [ItemJsonSchema(Minimum = 0.0, Maximum = 1.0)] + public float[] xs; + } + + [Test] + public void HasArrayObjectValidator() + { + { + var c = new JsonSchemaValidationContext("test") + { + EnableDiagnosisForNotRequiredFields = true, + }; + + var s = JsonSchema.FromType(); + + 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()); + } + } + + class HasListObject + { + [ItemJsonSchema(Minimum = 0.0, Maximum = 1.0)] + public List xs; + } + + [Test] + public void HasListObjectValidator() + { + { + var c = new JsonSchemaValidationContext("test") + { + EnableDiagnosisForNotRequiredFields = true, + }; + + var s = JsonSchema.FromType(); + + Assert.Null(s.Validator.Validate(c, new HasListObject { xs = new List {} })); + Assert.Null(s.Validator.Validate(c, new HasListObject { xs = new List { 0.5f } })); + Assert.NotNull(s.Validator.Validate(c, new HasListObject { xs = new List { 1.5f } })); + + Assert.True(c.IsEmpty()); + } + } } } diff --git a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs index b162c4354..922d2375f 100644 --- a/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs +++ b/Assets/VRM/UniJSON/Scripts/JsonSchemaValidator/JsonArrayValidator.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; @@ -145,10 +146,12 @@ namespace UniJSON } var count = GenericCounter.Count(o); - if (count == 0) + + // Empty array is valid + /*if (count == 0) { return new JsonSchemaValidationException(context, "empty"); - } + }*/ if (MaxItems.HasValue && count > MaxItems.Value) { @@ -160,6 +163,31 @@ namespace UniJSON return new JsonSchemaValidationException(context, "minItems"); } + var v = Items.Validator; + var t = o.GetType(); + IEnumerable iter = null; + if (t.IsArray) + { + iter = o as Array; + } + else if (t.GetIsGenericList()) + { + iter = o as IList; + } + else + { + return new JsonSchemaValidationException(context, "non iterable object"); + } + + foreach(var e in iter) + { + var ex = v.Validate(context, e); + if (ex != null) + { + return ex; + } + }; + return null; } @@ -252,7 +280,7 @@ namespace UniJSON } } - public void Deserialize(ListTreeNode src, ref U dst) + public void Deserialize(ListTreeNode src, ref U dst) where T : IListTreeItem, IValue { src.Deserialize(ref dst);