Add optional validation rule
This commit is contained in:
parent
da7ff701ef
commit
062dd56767
@ -29,6 +29,7 @@ namespace ChaosBot.WebServer.Services
|
|||||||
|
|
||||||
ValidationResult result = ruleType switch
|
ValidationResult result = ruleType switch
|
||||||
{
|
{
|
||||||
|
"optional" => CheckOptional(key, value),
|
||||||
"required" => CheckRequired(key, value),
|
"required" => CheckRequired(key, value),
|
||||||
"type" => CheckType(key, value, ruleParts),
|
"type" => CheckType(key, value, ruleParts),
|
||||||
"min" => CheckMin(key, value, ruleParts),
|
"min" => CheckMin(key, value, ruleParts),
|
||||||
@ -36,6 +37,9 @@ namespace ChaosBot.WebServer.Services
|
|||||||
_ => new ValidationResult.Unknown()
|
_ => new ValidationResult.Unknown()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (result.DoesForceSuccess())
|
||||||
|
break;
|
||||||
|
|
||||||
if (result.GetError() != null)
|
if (result.GetError() != null)
|
||||||
{
|
{
|
||||||
errorBuilder.AppendLine($"[{result.GetKey()}] {result.GetError()}");
|
errorBuilder.AppendLine($"[{result.GetKey()}] {result.GetError()}");
|
||||||
@ -48,6 +52,13 @@ namespace ChaosBot.WebServer.Services
|
|||||||
return error.Length == 0;
|
return error.Length == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ValidationResult CheckOptional(string key, dynamic value)
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
return new ValidationResult.SuperSuccess();
|
||||||
|
return new ValidationResult.Success();
|
||||||
|
}
|
||||||
|
|
||||||
private ValidationResult CheckRequired(string key, dynamic value)
|
private ValidationResult CheckRequired(string key, dynamic value)
|
||||||
{
|
{
|
||||||
if (value != null)
|
if (value != null)
|
||||||
@ -154,7 +165,15 @@ namespace ChaosBot.WebServer.Services
|
|||||||
_errorMessage = errorMessage;
|
_errorMessage = errorMessage;
|
||||||
_key = key;
|
_key = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal class SuperSuccess : ValidationResult
|
||||||
|
{
|
||||||
|
public override bool DoesForceSuccess()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal class Success : ValidationResult {}
|
internal class Success : ValidationResult {}
|
||||||
|
|
||||||
internal class Failure : ValidationResult
|
internal class Failure : ValidationResult
|
||||||
@ -172,5 +191,10 @@ namespace ChaosBot.WebServer.Services
|
|||||||
{
|
{
|
||||||
return this._key;
|
return this._key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual bool DoesForceSuccess()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user