Add an option to check all fields by JsonSchema

This commit is contained in:
yutopp
2019-01-08 18:26:52 +09:00
parent 60041f70c3
commit 317699ef41
3 changed files with 58 additions and 24 deletions

View File

@@ -199,6 +199,41 @@ namespace UniJSON
Assert.True(c.IsEmpty());
}
class NotRequired
{
[JsonSchema(Minimum = 1)]
public int Value;
}
[Test]
public void ObjectValidatorForNotRequired()
{
{
var c = new JsonSchemaValidationContext("test")
{
EnableDiagnosisForNotRequiredFields = false, // Default behaviour
};
var s = JsonSchema.FromType<NotRequired>();
// An error is not returned because Value is not 'Required' and the diagnosis is not enabled
Assert.Null(s.Validator.Validate(c, new Hoge { Value = 0 }));
Assert.True(c.IsEmpty());
}
{
var c = new JsonSchemaValidationContext("test")
{
EnableDiagnosisForNotRequiredFields = true,
};
var s = JsonSchema.FromType<NotRequired>();
Assert.NotNull(s.Validator.Validate(c, new Hoge { Value = 0 }));
Assert.True(c.IsEmpty());
}
}
[Test]
public void DictionaryValidator()
{

View File

@@ -7,6 +7,8 @@ namespace UniJSON
{
Stack<string> m_stack = new Stack<string>();
public bool EnableDiagnosisForNotRequiredFields = false;
public JsonSchemaValidationContext(object o)
{
Push(o.GetType().Name);

View File

@@ -28,11 +28,11 @@ namespace UniJSON
get; set;
}
List<string> m_required = new List<string>();
HashSet<string> m_required = new HashSet<string>();
/// <summary>
/// http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.3
/// </summary>
public List<string> Required
public HashSet<string> Required
{
get { return m_required; }
}
@@ -302,7 +302,7 @@ namespace UniJSON
{
class ObjectValidator
{
delegate JsonSchemaValidationException FieldValidator(IJsonSchemaValidator v,
delegate JsonSchemaValidationException FieldValidator(IJsonSchemaValidator v,
JsonSchemaValidationContext c, T o);
Dictionary<string, FieldValidator> m_validators = new Dictionary<string, FieldValidator>();
@@ -341,32 +341,38 @@ namespace UniJSON
}
}
public JsonSchemaValidationException Validate(List<string> required, Dictionary<string, JsonSchema> properties,
public JsonSchemaValidationException Validate(
HashSet<string> required,
Dictionary<string, JsonSchema> properties,
JsonSchemaValidationContext c, T o)
{
foreach (var x in required)
foreach (var kv in properties)
{
JsonSchema s;
if(properties.TryGetValue(x, out s))
var fieldName = kv.Key;
var schema = kv.Value;
FieldValidator fv;
if (m_validators.TryGetValue(fieldName, out fv))
{
FieldValidator fv;
if (m_validators.TryGetValue(x, out fv))
var ex = fv(schema.Validator, c, o);
if (ex != null)
{
var ex = fv(s.Validator, c, o);
if (ex != null)
if (required.Contains(fieldName) // required fields must be checked
|| c.EnableDiagnosisForNotRequiredFields)
{
return ex;
}
}
}
}
return null;
}
}
static ObjectValidator s_validator;
public static JsonSchemaValidationException Validate(List<string> required,
public static JsonSchemaValidationException Validate(HashSet<string> required,
Dictionary<string, JsonSchema> properties,
JsonSchemaValidationContext c, T o)
{
@@ -390,16 +396,7 @@ namespace UniJSON
return new JsonSchemaValidationException(c, "no properties");
}
if (Required != null)
{
var ex = GenericValidator<T>.Validate(Required, Properties, c, o);
if (ex != null)
{
return ex;
}
}
return null;
return GenericValidator<T>.Validate(Required, Properties, c, o);
}
static class GenericSerializer<T>
@@ -511,7 +508,7 @@ namespace UniJSON
GenericSerializer<T>.Serialize(this, f, c, value);
}
static class GenericDeserializer<S, T>
static class GenericDeserializer<S, T>
where S : IListTreeItem, IValue<S>
{
delegate T Deserializer(ListTreeNode<S> src);
@@ -596,7 +593,7 @@ namespace UniJSON
}
}
public void Deserialize<T, U>(ListTreeNode<T> src, ref U dst)
public void Deserialize<T, U>(ListTreeNode<T> src, ref U dst)
where T : IListTreeItem, IValue<T>
{
GenericDeserializer<T, U>.Deserialize(src, ref dst, Properties);