mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-10-25 19:57:54 -05:00
Add/Fix tests for UniJSON. Add ExplicitIgnorableValue option to ignore checks for validations
This commit is contained in:
@@ -57,6 +57,8 @@ namespace UniJSON
|
||||
/// </summary>
|
||||
public bool SkipComparison { get; set; }
|
||||
|
||||
public object ExplicitIgnorableValue { private get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("<{0}>", Title);
|
||||
@@ -175,7 +177,8 @@ namespace UniJSON
|
||||
Title = a.Title,
|
||||
Description = a.Description,
|
||||
Validator = validator,
|
||||
SkipComparison = skipComparison
|
||||
SkipComparison = skipComparison,
|
||||
ExplicitIgnorableValue = a.ExplicitIgnorableValue,
|
||||
};
|
||||
|
||||
return schema;
|
||||
@@ -404,6 +407,16 @@ namespace UniJSON
|
||||
Validator.ToJsonScheama(f);
|
||||
f.EndMap();
|
||||
}
|
||||
|
||||
public bool IsExplicitlyIgnorableValue<T>(T obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return ExplicitIgnorableValue == null;
|
||||
}
|
||||
|
||||
return obj.Equals(ExplicitIgnorableValue);
|
||||
}
|
||||
}
|
||||
|
||||
public static class JsonSchemaExtensions
|
||||
|
||||
@@ -55,6 +55,12 @@ namespace UniJSON
|
||||
/// </summary>
|
||||
public bool SkipSchemaComparison;
|
||||
|
||||
/// <summary>
|
||||
/// Suppress errors if a value of the field which is not required by a schema is matched to this value.
|
||||
/// This feature will be useful to ignore invalid value which is known.
|
||||
/// </summary>
|
||||
public object ExplicitIgnorableValue;
|
||||
|
||||
public void Merge(BaseJsonSchemaAttribute rhs)
|
||||
{
|
||||
if (rhs == null) return;
|
||||
|
||||
@@ -4,7 +4,6 @@ using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
|
||||
namespace UniJSON
|
||||
{
|
||||
/// <summary>
|
||||
@@ -302,18 +301,26 @@ namespace UniJSON
|
||||
{
|
||||
class ObjectValidator
|
||||
{
|
||||
delegate JsonSchemaValidationException FieldValidator(IJsonSchemaValidator v,
|
||||
JsonSchemaValidationContext c, T o);
|
||||
delegate JsonSchemaValidationException FieldValidator(
|
||||
JsonSchema s, JsonSchemaValidationContext c, T o, bool isRequired);
|
||||
|
||||
Dictionary<string, FieldValidator> m_validators = new Dictionary<string, FieldValidator>();
|
||||
|
||||
static FieldValidator CreteFieldValidator<U>(Func<T, U> getter, string name)
|
||||
{
|
||||
return (v, c, o) =>
|
||||
return (s, c, o, isRequired) =>
|
||||
{
|
||||
var v = s.Validator;
|
||||
using (c.Push(name))
|
||||
{
|
||||
return v.Validate(c, getter(o));
|
||||
var field = getter(o);
|
||||
var ex = v.Validate(c, field);
|
||||
if (ex != null && !isRequired && s.IsExplicitlyIgnorableValue(field))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return ex;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -354,11 +361,12 @@ namespace UniJSON
|
||||
FieldValidator fv;
|
||||
if (m_validators.TryGetValue(fieldName, out fv))
|
||||
{
|
||||
var ex = fv(schema.Validator, c, o);
|
||||
var isRequired = required.Contains(fieldName);
|
||||
var ex = fv(schema, c, o, isRequired);
|
||||
if (ex != null)
|
||||
{
|
||||
if (required.Contains(fieldName) // required fields must be checked
|
||||
|| c.EnableDiagnosisForNotRequiredFields)
|
||||
if (isRequired // required fields must be checked
|
||||
|| c.EnableDiagnosisForNotRequiredFields)
|
||||
{
|
||||
return ex;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user